async def _achat_with_retry(**kwargs: Any) -> Any:
        try:
            return await generation_method(**kwargs)
        except InvalidArgument as e:
            # Do not retry for these errors.
            raise ChatGoogleGenerativeAIError(
                f"Invalid argument provided to Gemini: {e}"
            ) from e
        except Exception as e:
            raise e
    return await _achat_with_retry(**kwargs)
def _get_role(message: BaseMessage) -> str:
    if isinstance(message, ChatMessage):
        if message.role not in ("user", "model"):
            raise ChatGoogleGenerativeAIError(
                "Gemini only supports user and model roles when"
                " providing it with Chat messages."
            )
        return message.role
    elif isinstance(message, HumanMessage):
        return "user"
    elif isinstance(message, AIMessage):
        return "model"
    else:
        # TODO: Gemini doesn't seem to have a concept of system messages yet.
        raise ChatGoogleGenerativeAIError(
            f"Message of '{message.type}' type not supported by Gemini."
            " Please only provide it with Human or AI (user/assistant) messages."
        )
def _is_openai_parts_format(part: dict) -> bool:
    return "type" in part
def _is_vision_model(model: str) -> bool:
    return "vision" in model
def _is_url(s: str) -> bool:
    try:
        result = urlparse(s)
        return all([result.scheme, result.netloc])
    except Exception as e:
        logger.debug(f"Unable to parse URL: {e}")
        return False
def _is_b64(s: str) -> bool:
    return s.startswith("data:image")