def chat_message_to_gemini(message: ChatMessage) -> types.Content:
    """Convert ChatMessages to Gemini-specific history, including ImageDocuments."""
    parts = []
    for block in message.blocks:
        if isinstance(block, TextBlock):
            if block.text:
                parts.append(types.Part.from_text(text=block.text))
        elif isinstance(block, ImageBlock):
            base64_bytes = block.resolve_image(as_base64=False).read()
            if not block.image_mimetype:
                # TODO: fail ?
                block.image_mimetype = "image/png"

            parts.append(
                types.Part.from_bytes(
                    data=base64_bytes,
                    mime_type=block.image_mimetype,
                )
            )
        elif isinstance(block, DocumentBlock):
            file_buffer = block.resolve_document()
            file_bytes = file_buffer.read()
            mimetype = (
                block.document_mimetype
                if block.document_mimetype is not None
                else "application/pdf"
            )
            parts.append(types.Part.from_bytes(data=file_bytes, mime_type=mimetype))
        else:
            msg = f"Unsupported content block type: {type(block).__name__}"
            raise ValueError(msg)

    for tool_call in message.additional_kwargs.get("tool_calls", []):
        if isinstance(tool_call, dict):
            parts.append(
                types.Part.from_function_call(
                    name=tool_call.get("name"), args=tool_call.get("args")
                )
            )
        else:
            parts.append(
                types.Part.from_function_call(name=tool_call.name, args=tool_call.args)
            )

    # the tool call id is the name of the tool
    # the tool call response is the content of the message, overriding the existing content
    # (the only content before this should be the tool call)
    if message.additional_kwargs.get("tool_call_id"):
        function_response_part = types.Part.from_function_response(
            name=message.additional_kwargs.get("tool_call_id"),
            response={"result": message.content},
        )
        return types.Content(
            role=ROLES_TO_GEMINI[message.role], parts=[function_response_part]
        )

    return types.Content(
        role=ROLES_TO_GEMINI[message.role],
        parts=parts,
    )
Convert ChatMessages to Gemini-specific history, including ImageDocuments.
llama-index-integrations/llms/llama-index-llms-google-genai/llama_index/llms/google_genai/utils.py