Home / Function/ _convert_to_v1_from_genai_input() — langchain Function Reference

_convert_to_v1_from_genai_input() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  033ef6d5_126f_ff7b_a5d2_07234ffb1322["_convert_to_v1_from_genai_input()"]
  d24bd624_4204_2ea9_e307_7937b89182bc["google_genai.py"]
  033ef6d5_126f_ff7b_a5d2_07234ffb1322 -->|defined in| d24bd624_4204_2ea9_e307_7937b89182bc
  67c4b453_8508_9252_4f0c_d47520aaf200["_bytes_to_b64_str()"]
  033ef6d5_126f_ff7b_a5d2_07234ffb1322 -->|calls| 67c4b453_8508_9252_4f0c_d47520aaf200
  style 033ef6d5_126f_ff7b_a5d2_07234ffb1322 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/messages/block_translators/google_genai.py lines 122–296

def _convert_to_v1_from_genai_input(
    content: list[types.ContentBlock],
) -> list[types.ContentBlock]:
    """Convert Google GenAI format blocks to v1 format.

    Called when message isn't an `AIMessage` or `model_provider` isn't set on
    `response_metadata`.

    During the `content_blocks` parsing process, we wrap blocks not recognized as a v1
    block as a `'non_standard'` block with the original block stored in the `value`
    field. This function attempts to unpack those blocks and convert any blocks that
    might be GenAI format to v1 ContentBlocks.

    If conversion fails, the block is left as a `'non_standard'` block.

    Args:
        content: List of content blocks to process.

    Returns:
        Updated list with GenAI blocks converted to v1 format.
    """

    def _iter_blocks() -> Iterator[types.ContentBlock]:
        blocks: list[dict[str, Any]] = [
            cast("dict[str, Any]", block)
            if block.get("type") != "non_standard"
            else block["value"]  # type: ignore[typeddict-item]  # this is only non-standard blocks
            for block in content
        ]
        for block in blocks:
            num_keys = len(block)
            block_type = block.get("type")

            if num_keys == 1 and (text := block.get("text")):
                # This is probably a TextContentBlock
                yield {"type": "text", "text": text}

            elif (
                num_keys == 1
                and (document := block.get("document"))
                and isinstance(document, dict)
                and "format" in document
            ):
                # Handle document format conversion
                doc_format = document.get("format")
                source = document.get("source", {})

                if doc_format == "pdf" and "bytes" in source:
                    # PDF document with byte data
                    file_block: types.FileContentBlock = {
                        "type": "file",
                        "base64": source["bytes"]
                        if isinstance(source["bytes"], str)
                        else _bytes_to_b64_str(source["bytes"]),
                        "mime_type": "application/pdf",
                    }
                    # Preserve extra fields
                    extras = {
                        key: value
                        for key, value in document.items()
                        if key not in {"format", "source"}
                    }
                    if extras:
                        file_block["extras"] = extras
                    yield file_block

                elif doc_format == "txt" and "text" in source:
                    # Text document
                    plain_text_block: types.PlainTextContentBlock = {
                        "type": "text-plain",
                        "text": source["text"],
                        "mime_type": "text/plain",
                    }
                    # Preserve extra fields
                    extras = {
                        key: value
                        for key, value in document.items()
                        if key not in {"format", "source"}
                    }
                    if extras:
                        plain_text_block["extras"] = extras

Domain

Subdomains

Frequently Asked Questions

What does _convert_to_v1_from_genai_input() do?
_convert_to_v1_from_genai_input() 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_input() defined?
_convert_to_v1_from_genai_input() is defined in libs/core/langchain_core/messages/block_translators/google_genai.py at line 122.
What does _convert_to_v1_from_genai_input() call?
_convert_to_v1_from_genai_input() calls 1 function(s): _bytes_to_b64_str.

Analyze Your Own Codebase

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

Try Supermodel Free