Home / File/ langchain_v0.py — langchain Source File

langchain_v0.py — langchain Source File

Architecture documentation for langchain_v0.py, a python file in the langchain codebase. 2 imports, 0 dependents.

File python CoreAbstractions MessageSchema 2 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  6202583c_37a6_480c_818f_b5208f8a139b["langchain_v0.py"]
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  6202583c_37a6_480c_818f_b5208f8a139b --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"]
  6202583c_37a6_480c_818f_b5208f8a139b --> d758344f_537f_649e_f467_b9d7442e86df
  style 6202583c_37a6_480c_818f_b5208f8a139b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Derivations of standard content blocks from LangChain v0 multimodal content."""

from typing import Any, cast

from langchain_core.messages import content as types


def _convert_v0_multimodal_input_to_v1(
    content: list[types.ContentBlock],
) -> list[types.ContentBlock]:
    """Convert v0 multimodal blocks to v1 format.

    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 v0 format
    blocks to v1 format.

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

    Args:
        content: List of content blocks to process.

    Returns:
        v1 content blocks.
    """
    converted_blocks = []
    unpacked_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 unpacked_blocks:
        if block.get("type") in {"image", "audio", "file"} and "source_type" in block:
            converted_block = _convert_legacy_v0_content_block_to_v1(block)
            converted_blocks.append(cast("types.ContentBlock", converted_block))
        elif block.get("type") in types.KNOWN_BLOCK_TYPES:
            # Guard in case this function is used outside of the .content_blocks flow
            converted_blocks.append(cast("types.ContentBlock", block))
        else:
            converted_blocks.append({"type": "non_standard", "value": block})

    return converted_blocks


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.
// ... (242 more lines)

Subdomains

Dependencies

  • langchain_core.messages
  • typing

Frequently Asked Questions

What does langchain_v0.py do?
langchain_v0.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What functions are defined in langchain_v0.py?
langchain_v0.py defines 2 function(s): _convert_legacy_v0_content_block_to_v1, _convert_v0_multimodal_input_to_v1.
What does langchain_v0.py depend on?
langchain_v0.py imports 2 module(s): langchain_core.messages, typing.
Where is langchain_v0.py in the architecture?
langchain_v0.py is located at libs/core/langchain_core/messages/block_translators/langchain_v0.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/core/langchain_core/messages/block_translators).

Analyze Your Own Codebase

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

Try Supermodel Free