)
                parts.append(part)
        else:
            # TODO: Maybe some of Google's native stuff
            # would hit this branch.
            raise ChatGoogleGenerativeAIError(
                "Gemini only supports text and inline_data parts."
            )
    return parts
def _messages_to_genai_contents(
    input_messages: Sequence[BaseMessage],
) -> List[genai.types.ContentDict]:
    """Converts a list of messages into a Gemini API google content dicts."""
    messages: List[genai.types.MessageDict] = []
    for i, message in enumerate(input_messages):
        role = _get_role(message)
        if isinstance(message.content, str):
            parts = [message.content]
        else:
            parts = _convert_to_parts(message.content)
        messages.append({"role": role, "parts": parts})
        if i > 0:
            # Cannot have multiple messages from the same role in a row.
            if role == messages[-2]["role"]:
                raise ChatGoogleGenerativeAIError(
                    "Cannot have multiple messages from the same role in a row."
                    " Consider merging them into a single message with multiple"
                    f" parts.\nReceived: {messages}"
                )
    return messages
def _parts_to_content(parts: List[genai.types.PartType]) -> Union[List[dict], str]:
    """Converts a list of Gemini API Part objects into a list of LangChain messages."""
    if len(parts) == 1 and parts[0].text is not None and not parts[0].inline_data:
        # Simple text response. The typical response
        return parts[0].text
    elif not parts:
        logger.warning("Gemini produced an empty response.")
        return ""
    messages = []