Home / Function/ _convert_to_v1_from_genai() — langchain Function Reference

_convert_to_v1_from_genai() — langchain Function Reference

Architecture documentation for the _convert_to_v1_from_genai() function in google_genai.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  fd689bd2_1377_f6be_db90_7e66dcadfcce["_convert_to_v1_from_genai()"]
  d24bd624_4204_2ea9_e307_7937b89182bc["google_genai.py"]
  fd689bd2_1377_f6be_db90_7e66dcadfcce -->|defined in| d24bd624_4204_2ea9_e307_7937b89182bc
  eca8c6c8_6110_e88d_fe5f_673f61f70bb5["translate_content()"]
  eca8c6c8_6110_e88d_fe5f_673f61f70bb5 -->|calls| fd689bd2_1377_f6be_db90_7e66dcadfcce
  f4f1d3a2_f4db_b446_8619_acdeb99b255f["translate_content_chunk()"]
  f4f1d3a2_f4db_b446_8619_acdeb99b255f -->|calls| fd689bd2_1377_f6be_db90_7e66dcadfcce
  67c4b453_8508_9252_4f0c_d47520aaf200["_bytes_to_b64_str()"]
  fd689bd2_1377_f6be_db90_7e66dcadfcce -->|calls| 67c4b453_8508_9252_4f0c_d47520aaf200
  5cac5fba_c2ec_94d4_5ae4_6b0256515269["translate_grounding_metadata_to_citations()"]
  fd689bd2_1377_f6be_db90_7e66dcadfcce -->|calls| 5cac5fba_c2ec_94d4_5ae4_6b0256515269
  style fd689bd2_1377_f6be_db90_7e66dcadfcce fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/messages/block_translators/google_genai.py lines 299–527

def _convert_to_v1_from_genai(message: AIMessage) -> list[types.ContentBlock]:
    """Convert Google GenAI message content to v1 format.

    Calling `.content_blocks` on an `AIMessage` where `response_metadata.model_provider`
    is set to `'google_genai'` will invoke this function to parse the content into
    standard content blocks for returning.

    Args:
        message: The `AIMessage` or `AIMessageChunk` to convert.

    Returns:
        List of standard content blocks derived from the message content.
    """
    if isinstance(message.content, str):
        # String content -> TextContentBlock (only add if non-empty in case of audio)
        string_blocks: list[types.ContentBlock] = []
        if message.content:
            string_blocks.append({"type": "text", "text": message.content})

        # Add any missing tool calls from message.tool_calls field
        content_tool_call_ids = {
            block.get("id")
            for block in string_blocks
            if isinstance(block, dict) and block.get("type") == "tool_call"
        }
        for tool_call in message.tool_calls:
            id_ = tool_call.get("id")
            if id_ and id_ not in content_tool_call_ids:
                string_tool_call_block: types.ToolCall = {
                    "type": "tool_call",
                    "id": id_,
                    "name": tool_call["name"],
                    "args": tool_call["args"],
                }
                string_blocks.append(string_tool_call_block)

        # Handle audio from additional_kwargs if present (for empty content cases)
        audio_data = message.additional_kwargs.get("audio")
        if audio_data and isinstance(audio_data, bytes):
            audio_block: types.AudioContentBlock = {
                "type": "audio",
                "base64": _bytes_to_b64_str(audio_data),
                "mime_type": "audio/wav",  # Default to WAV for Google GenAI
            }
            string_blocks.append(audio_block)

        grounding_metadata = message.response_metadata.get("grounding_metadata")
        if grounding_metadata:
            citations = translate_grounding_metadata_to_citations(grounding_metadata)

            for block in string_blocks:
                if block["type"] == "text" and citations:
                    # Add citations to the first text block only
                    block["annotations"] = cast("list[types.Annotation]", citations)
                    break

        return string_blocks

    if not isinstance(message.content, list):
        # Unexpected content type, attempt to represent as text
        return [{"type": "text", "text": str(message.content)}]

    converted_blocks: list[types.ContentBlock] = []

    for item in message.content:
        if isinstance(item, str):
            # Conversation history strings

            # Citations are handled below after all blocks are converted
            converted_blocks.append({"type": "text", "text": item})  # TextContentBlock

        elif isinstance(item, dict):
            item_type = item.get("type")
            if item_type == "image_url":
                # Convert image_url to standard image block (base64)
                # (since the original implementation returned as url-base64 CC style)
                image_url = item.get("image_url", {})
                url = image_url.get("url", "")
                if url:
                    # Extract base64 data
                    match = re.match(r"data:([^;]+);base64,(.+)", url)

Domain

Subdomains

Frequently Asked Questions

What does _convert_to_v1_from_genai() do?
_convert_to_v1_from_genai() is a function in the langchain codebase, defined in libs/core/langchain_core/messages/block_translators/google_genai.py.
Where is _convert_to_v1_from_genai() defined?
_convert_to_v1_from_genai() is defined in libs/core/langchain_core/messages/block_translators/google_genai.py at line 299.
What does _convert_to_v1_from_genai() call?
_convert_to_v1_from_genai() calls 2 function(s): _bytes_to_b64_str, translate_grounding_metadata_to_citations.
What calls _convert_to_v1_from_genai()?
_convert_to_v1_from_genai() is called by 2 function(s): translate_content, translate_content_chunk.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free