Home / Function/ _convert_legacy_v0_content_block_to_v1() — langchain Function Reference

_convert_legacy_v0_content_block_to_v1() — langchain Function Reference

Architecture documentation for the _convert_legacy_v0_content_block_to_v1() function in langchain_v0.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  8ade2f32_356b_a815_6dbc_b5db06efc5a6["_convert_legacy_v0_content_block_to_v1()"]
  8223fdf4_081a_3a66_acfc_b7410ab1f7d3["langchain_v0.py"]
  8ade2f32_356b_a815_6dbc_b5db06efc5a6 -->|defined in| 8223fdf4_081a_3a66_acfc_b7410ab1f7d3
  4c09246e_0e01_1626_13ad_9e75fabacbb7["_convert_v0_multimodal_input_to_v1()"]
  4c09246e_0e01_1626_13ad_9e75fabacbb7 -->|calls| 8ade2f32_356b_a815_6dbc_b5db06efc5a6
  style 8ade2f32_356b_a815_6dbc_b5db06efc5a6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/messages/block_translators/langchain_v0.py lines 46–301

def _convert_legacy_v0_content_block_to_v1(
    block: dict,
) -> types.ContentBlock | dict:
    """Convert a LangChain v0 content block to v1 format.

    Preserves unknown keys as extras to avoid data loss.

    Returns the original block unchanged if it's not in v0 format.
    """

    def _extract_v0_extras(block_dict: dict, known_keys: set[str]) -> dict[str, Any]:
        """Extract unknown keys from v0 block to preserve as extras.

        Args:
            block_dict: The original v0 block dictionary.
            known_keys: Set of keys known to be part of the v0 format for this block.

        Returns:
            A dictionary of extra keys not part of the known v0 format.
        """
        return {k: v for k, v in block_dict.items() if k not in known_keys}

    # Check if this is actually a v0 format block
    block_type = block.get("type")
    if block_type not in {"image", "audio", "file"} or "source_type" not in block:
        # Not a v0 format block, return unchanged
        return block

    if block.get("type") == "image":
        source_type = block.get("source_type")
        if source_type == "url":
            # image-url
            known_keys = {"mime_type", "type", "source_type", "url"}
            extras = _extract_v0_extras(block, known_keys)
            if "id" in block:
                return types.create_image_block(
                    url=block["url"],
                    mime_type=block.get("mime_type"),
                    id=block["id"],
                    **extras,
                )

            # Don't construct with an ID if not present in original block
            v1_image_url = types.ImageContentBlock(type="image", url=block["url"])
            if block.get("mime_type"):
                v1_image_url["mime_type"] = block["mime_type"]

            v1_image_url["extras"] = {}
            for key, value in extras.items():
                if value is not None:
                    v1_image_url["extras"][key] = value
            if v1_image_url["extras"] == {}:
                del v1_image_url["extras"]

            return v1_image_url
        if source_type == "base64":
            # image-base64
            known_keys = {"mime_type", "type", "source_type", "data"}
            extras = _extract_v0_extras(block, known_keys)
            if "id" in block:
                return types.create_image_block(
                    base64=block["data"],
                    mime_type=block.get("mime_type"),
                    id=block["id"],
                    **extras,
                )

            v1_image_base64 = types.ImageContentBlock(
                type="image", base64=block["data"]
            )
            if block.get("mime_type"):
                v1_image_base64["mime_type"] = block["mime_type"]

            v1_image_base64["extras"] = {}
            for key, value in extras.items():
                if value is not None:
                    v1_image_base64["extras"][key] = value
            if v1_image_base64["extras"] == {}:
                del v1_image_base64["extras"]

            return v1_image_base64

Domain

Subdomains

Frequently Asked Questions

What does _convert_legacy_v0_content_block_to_v1() do?
_convert_legacy_v0_content_block_to_v1() is a function in the langchain codebase, defined in libs/core/langchain_core/messages/block_translators/langchain_v0.py.
Where is _convert_legacy_v0_content_block_to_v1() defined?
_convert_legacy_v0_content_block_to_v1() is defined in libs/core/langchain_core/messages/block_translators/langchain_v0.py at line 46.
What calls _convert_legacy_v0_content_block_to_v1()?
_convert_legacy_v0_content_block_to_v1() is called by 1 function(s): _convert_v0_multimodal_input_to_v1.

Analyze Your Own Codebase

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

Try Supermodel Free