Home / Function/ _convert_from_v03_ai_message() — langchain Function Reference

_convert_from_v03_ai_message() — langchain Function Reference

Architecture documentation for the _convert_from_v03_ai_message() function in openai.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  6dd0c2f6_197f_5200_610f_82ef7e9b8132["_convert_from_v03_ai_message()"]
  3f58992c_878c_57d1_7412_5e64743aa7ba["openai.py"]
  6dd0c2f6_197f_5200_610f_82ef7e9b8132 -->|defined in| 3f58992c_878c_57d1_7412_5e64743aa7ba
  af54f3fb_5bfb_6940_293f_4c3b25c7f24f["translate_content()"]
  af54f3fb_5bfb_6940_293f_4c3b25c7f24f -->|calls| 6dd0c2f6_197f_5200_610f_82ef7e9b8132
  108d3f87_48ae_3513_b686_eca17b966d18["translate_content_chunk()"]
  108d3f87_48ae_3513_b686_eca17b966d18 -->|calls| 6dd0c2f6_197f_5200_610f_82ef7e9b8132
  style 6dd0c2f6_197f_5200_610f_82ef7e9b8132 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/messages/block_translators/openai.py lines 288–420

def _convert_from_v03_ai_message(message: AIMessage) -> AIMessage:
    """Convert v0 AIMessage into `output_version="responses/v1"` format."""
    # Only update ChatOpenAI v0.3 AIMessages
    is_chatopenai_v03 = (
        isinstance(message.content, list)
        and all(isinstance(b, dict) for b in message.content)
    ) and (
        any(
            item in message.additional_kwargs
            for item in [
                "reasoning",
                "tool_outputs",
                "refusal",
                _FUNCTION_CALL_IDS_MAP_KEY,
            ]
        )
        or (
            isinstance(message.id, str)
            and message.id.startswith("msg_")
            and (response_id := message.response_metadata.get("id"))
            and isinstance(response_id, str)
            and response_id.startswith("resp_")
        )
    )
    if not is_chatopenai_v03:
        return message

    content_order = [
        "reasoning",
        "code_interpreter_call",
        "mcp_call",
        "image_generation_call",
        "text",
        "refusal",
        "function_call",
        "computer_call",
        "mcp_list_tools",
        "mcp_approval_request",
        # N. B. "web_search_call" and "file_search_call" were not passed back in
        # in v0.3
    ]

    # Build a bucket for every known block type
    buckets: dict[str, list] = {key: [] for key in content_order}
    unknown_blocks = []

    # Reasoning
    if reasoning := message.additional_kwargs.get("reasoning"):
        if isinstance(message, AIMessageChunk) and message.chunk_position != "last":
            buckets["reasoning"].append({**reasoning, "type": "reasoning"})
        else:
            buckets["reasoning"].append(reasoning)

    # Refusal
    if refusal := message.additional_kwargs.get("refusal"):
        buckets["refusal"].append({"type": "refusal", "refusal": refusal})

    # Text
    for block in message.content:
        if isinstance(block, dict) and block.get("type") == "text":
            block_copy = block.copy()
            if isinstance(message.id, str) and message.id.startswith("msg_"):
                block_copy["id"] = message.id
            buckets["text"].append(block_copy)
        else:
            unknown_blocks.append(block)

    # Function calls
    function_call_ids = message.additional_kwargs.get(_FUNCTION_CALL_IDS_MAP_KEY)
    if (
        isinstance(message, AIMessageChunk)
        and len(message.tool_call_chunks) == 1
        and message.chunk_position != "last"
    ):
        # Isolated chunk
        tool_call_chunk = message.tool_call_chunks[0]
        function_call = {
            "type": "function_call",
            "name": tool_call_chunk.get("name"),
            "arguments": tool_call_chunk.get("args"),
            "call_id": tool_call_chunk.get("id"),

Domain

Subdomains

Frequently Asked Questions

What does _convert_from_v03_ai_message() do?
_convert_from_v03_ai_message() is a function in the langchain codebase, defined in libs/core/langchain_core/messages/block_translators/openai.py.
Where is _convert_from_v03_ai_message() defined?
_convert_from_v03_ai_message() is defined in libs/core/langchain_core/messages/block_translators/openai.py at line 288.
What calls _convert_from_v03_ai_message()?
_convert_from_v03_ai_message() 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