Home / Function/ _convert_to_v1_from_anthropic_input() — langchain Function Reference

_convert_to_v1_from_anthropic_input() — langchain Function Reference

Architecture documentation for the _convert_to_v1_from_anthropic_input() function in anthropic.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  2e642f1b_7f57_e383_f86f_66bbd57a9c14["_convert_to_v1_from_anthropic_input()"]
  57106cb4_1f55_a208_a37b_084bce5fa252["anthropic.py"]
  2e642f1b_7f57_e383_f86f_66bbd57a9c14 -->|defined in| 57106cb4_1f55_a208_a37b_084bce5fa252
  f31b1154_2d7b_ce14_91aa_25e02bb2423c["_populate_extras()"]
  2e642f1b_7f57_e383_f86f_66bbd57a9c14 -->|calls| f31b1154_2d7b_ce14_91aa_25e02bb2423c
  style 2e642f1b_7f57_e383_f86f_66bbd57a9c14 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/messages/block_translators/anthropic.py lines 29–139

def _convert_to_v1_from_anthropic_input(
    content: list[types.ContentBlock],
) -> list[types.ContentBlock]:
    """Convert Anthropic format 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 blocks that
    might be Anthropic 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 Anthropic 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:
            block_type = block.get("type")

            if (
                block_type == "document"
                and "source" in block
                and "type" in block["source"]
            ):
                if block["source"]["type"] == "base64":
                    file_block: types.FileContentBlock = {
                        "type": "file",
                        "base64": block["source"]["data"],
                        "mime_type": block["source"]["media_type"],
                    }
                    _populate_extras(file_block, block, {"type", "source"})
                    yield file_block

                elif block["source"]["type"] == "url":
                    file_block = {
                        "type": "file",
                        "url": block["source"]["url"],
                    }
                    _populate_extras(file_block, block, {"type", "source"})
                    yield file_block

                elif block["source"]["type"] == "file":
                    file_block = {
                        "type": "file",
                        "id": block["source"]["file_id"],
                    }
                    _populate_extras(file_block, block, {"type", "source"})
                    yield file_block

                elif block["source"]["type"] == "text":
                    plain_text_block: types.PlainTextContentBlock = {
                        "type": "text-plain",
                        "text": block["source"]["data"],
                        "mime_type": block.get("media_type", "text/plain"),
                    }
                    _populate_extras(plain_text_block, block, {"type", "source"})
                    yield plain_text_block

                else:
                    yield {"type": "non_standard", "value": block}

            elif (
                block_type == "image"
                and "source" in block
                and "type" in block["source"]
            ):
                if block["source"]["type"] == "base64":
                    image_block: types.ImageContentBlock = {
                        "type": "image",
                        "base64": block["source"]["data"],
                        "mime_type": block["source"]["media_type"],

Domain

Subdomains

Frequently Asked Questions

What does _convert_to_v1_from_anthropic_input() do?
_convert_to_v1_from_anthropic_input() is a function in the langchain codebase, defined in libs/core/langchain_core/messages/block_translators/anthropic.py.
Where is _convert_to_v1_from_anthropic_input() defined?
_convert_to_v1_from_anthropic_input() is defined in libs/core/langchain_core/messages/block_translators/anthropic.py at line 29.
What does _convert_to_v1_from_anthropic_input() call?
_convert_to_v1_from_anthropic_input() calls 1 function(s): _populate_extras.

Analyze Your Own Codebase

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

Try Supermodel Free