Home / Function/ _convert_to_v1_from_converse() — langchain Function Reference

_convert_to_v1_from_converse() — langchain Function Reference

Architecture documentation for the _convert_to_v1_from_converse() function in bedrock_converse.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  58049150_fae0_b4d0_089a_5bc619fc4036["_convert_to_v1_from_converse()"]
  4f830f44_a777_e9e4_dc82_3b3b61033802["bedrock_converse.py"]
  58049150_fae0_b4d0_089a_5bc619fc4036 -->|defined in| 4f830f44_a777_e9e4_dc82_3b3b61033802
  fb254d8d_a16f_a23f_aa8b_dbd5eabe3cbf["translate_content()"]
  fb254d8d_a16f_a23f_aa8b_dbd5eabe3cbf -->|calls| 58049150_fae0_b4d0_089a_5bc619fc4036
  f2cce475_f7e4_8a9e_b35a_c9d1300b818b["translate_content_chunk()"]
  f2cce475_f7e4_8a9e_b35a_c9d1300b818b -->|calls| 58049150_fae0_b4d0_089a_5bc619fc4036
  7e89bd6a_6f27_c7ee_0b44_fbbf15c1d7d0["_convert_citation_to_v1()"]
  58049150_fae0_b4d0_089a_5bc619fc4036 -->|calls| 7e89bd6a_6f27_c7ee_0b44_fbbf15c1d7d0
  style 58049150_fae0_b4d0_089a_5bc619fc4036 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/messages/block_translators/bedrock_converse.py lines 152–280

def _convert_to_v1_from_converse(message: AIMessage) -> list[types.ContentBlock]:
    """Convert Bedrock Converse message content to v1 format."""
    if (
        message.content == ""
        and not message.additional_kwargs
        and not message.tool_calls
    ):
        # Converse outputs multiple chunks containing response metadata
        return []

    if isinstance(message.content, str):
        message.content = [{"type": "text", "text": message.content}]

    def _iter_blocks() -> Iterator[types.ContentBlock]:
        for block in message.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 == "reasoning_content":
                reasoning_block: types.ReasoningContentBlock = {"type": "reasoning"}
                if reasoning_content := block.get("reasoning_content"):
                    if reasoning := reasoning_content.get("text"):
                        reasoning_block["reasoning"] = reasoning
                    if signature := reasoning_content.get("signature"):
                        if "extras" not in reasoning_block:
                            reasoning_block["extras"] = {}
                        reasoning_block["extras"]["signature"] = signature

                if "index" in block:
                    reasoning_block["index"] = block["index"]

                known_fields = {"type", "reasoning_content", "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",
                    )
                    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"),
                        }

Domain

Subdomains

Frequently Asked Questions

What does _convert_to_v1_from_converse() do?
_convert_to_v1_from_converse() is a function in the langchain codebase, defined in libs/core/langchain_core/messages/block_translators/bedrock_converse.py.
Where is _convert_to_v1_from_converse() defined?
_convert_to_v1_from_converse() is defined in libs/core/langchain_core/messages/block_translators/bedrock_converse.py at line 152.
What does _convert_to_v1_from_converse() call?
_convert_to_v1_from_converse() calls 1 function(s): _convert_citation_to_v1.
What calls _convert_to_v1_from_converse()?
_convert_to_v1_from_converse() 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