Home / Function/ _convert_to_v1_from_anthropic() — langchain Function Reference

_convert_to_v1_from_anthropic() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  44bffc7c_3088_f46b_2008_6c1f0b3fe7d5["_convert_to_v1_from_anthropic()"]
  57106cb4_1f55_a208_a37b_084bce5fa252["anthropic.py"]
  44bffc7c_3088_f46b_2008_6c1f0b3fe7d5 -->|defined in| 57106cb4_1f55_a208_a37b_084bce5fa252
  7f500ae3_e206_45d9_9776_3d6a26b64a5d["translate_content()"]
  7f500ae3_e206_45d9_9776_3d6a26b64a5d -->|calls| 44bffc7c_3088_f46b_2008_6c1f0b3fe7d5
  afb2d759_a6d6_4a41_3f4e_39ad5939e2ca["translate_content_chunk()"]
  afb2d759_a6d6_4a41_3f4e_39ad5939e2ca -->|calls| 44bffc7c_3088_f46b_2008_6c1f0b3fe7d5
  53f37939_6fda_5d72_b397_fd42dc3a7524["_convert_citation_to_v1()"]
  44bffc7c_3088_f46b_2008_6c1f0b3fe7d5 -->|calls| 53f37939_6fda_5d72_b397_fd42dc3a7524
  f31b1154_2d7b_ce14_91aa_25e02bb2423c["_populate_extras()"]
  44bffc7c_3088_f46b_2008_6c1f0b3fe7d5 -->|calls| f31b1154_2d7b_ce14_91aa_25e02bb2423c
  style 44bffc7c_3088_f46b_2008_6c1f0b3fe7d5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/messages/block_translators/anthropic.py lines 198–460

def _convert_to_v1_from_anthropic(message: AIMessage) -> list[types.ContentBlock]:
    """Convert Anthropic message content to v1 format."""
    if isinstance(message.content, str):
        content: list[str | dict] = [{"type": "text", "text": message.content}]
    else:
        content = message.content

    def _iter_blocks() -> Iterator[types.ContentBlock]:
        for block in content:
            if not isinstance(block, dict):
                continue
            block_type = block.get("type")

            if block_type == "text":
                if citations := block.get("citations"):
                    text_block: types.TextContentBlock = {
                        "type": "text",
                        "text": block.get("text", ""),
                        "annotations": [_convert_citation_to_v1(a) for a in citations],
                    }
                else:
                    text_block = {"type": "text", "text": block["text"]}
                if "index" in block:
                    text_block["index"] = block["index"]
                yield text_block

            elif block_type == "thinking":
                reasoning_block: types.ReasoningContentBlock = {
                    "type": "reasoning",
                    "reasoning": block.get("thinking", ""),
                }
                if "index" in block:
                    reasoning_block["index"] = block["index"]
                known_fields = {"type", "thinking", "index", "extras"}
                for key in block:
                    if key not in known_fields:
                        if "extras" not in reasoning_block:
                            reasoning_block["extras"] = {}
                        reasoning_block["extras"][key] = block[key]
                yield reasoning_block

            elif block_type == "tool_use":
                if (
                    isinstance(message, AIMessageChunk)
                    and len(message.tool_call_chunks) == 1
                    and message.chunk_position != "last"
                ):
                    # Isolated chunk
                    chunk = message.tool_call_chunks[0]

                    tool_call_chunk = types.ToolCallChunk(
                        name=chunk.get("name"),
                        id=chunk.get("id"),
                        args=chunk.get("args"),
                        type="tool_call_chunk",
                    )
                    if "caller" in block:
                        tool_call_chunk["extras"] = {"caller": block["caller"]}

                    index = chunk.get("index")
                    if index is not None:
                        tool_call_chunk["index"] = index
                    yield tool_call_chunk
                else:
                    tool_call_block: types.ToolCall | None = None
                    # Non-streaming or gathered chunk
                    if len(message.tool_calls) == 1:
                        tool_call_block = {
                            "type": "tool_call",
                            "name": message.tool_calls[0]["name"],
                            "args": message.tool_calls[0]["args"],
                            "id": message.tool_calls[0].get("id"),
                        }
                    elif call_id := block.get("id"):
                        for tc in message.tool_calls:
                            if tc.get("id") == call_id:
                                tool_call_block = {
                                    "type": "tool_call",
                                    "name": tc["name"],
                                    "args": tc["args"],
                                    "id": tc.get("id"),

Domain

Subdomains

Frequently Asked Questions

What does _convert_to_v1_from_anthropic() do?
_convert_to_v1_from_anthropic() 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() defined?
_convert_to_v1_from_anthropic() is defined in libs/core/langchain_core/messages/block_translators/anthropic.py at line 198.
What does _convert_to_v1_from_anthropic() call?
_convert_to_v1_from_anthropic() calls 2 function(s): _convert_citation_to_v1, _populate_extras.
What calls _convert_to_v1_from_anthropic()?
_convert_to_v1_from_anthropic() 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