_convert_to_v03_ai_message() — langchain Function Reference
Architecture documentation for the _convert_to_v03_ai_message() function in _compat.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD b63a2019_568b_0012_66ba_698a4b244f63["_convert_to_v03_ai_message()"] 92333051_7f77_b57b_d874_abb7bac2bbe0["_compat.py"] b63a2019_568b_0012_66ba_698a4b244f63 -->|defined in| 92333051_7f77_b57b_d874_abb7bac2bbe0 style b63a2019_568b_0012_66ba_698a4b244f63 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/partners/openai/langchain_openai/chat_models/_compat.py lines 81–150
def _convert_to_v03_ai_message(
message: AIMessage, has_reasoning: bool = False
) -> AIMessage:
"""Mutate an `AIMessage` to the old-style v0.3 format."""
if isinstance(message.content, list):
new_content: list[dict | str] = []
for block in message.content:
if isinstance(block, dict):
if block.get("type") == "reasoning":
# Store a reasoning item in additional_kwargs (overwriting as in
# v0.3)
_ = block.pop("index", None)
if has_reasoning:
_ = block.pop("id", None)
_ = block.pop("type", None)
message.additional_kwargs["reasoning"] = block
elif block.get("type") in (
"web_search_call",
"file_search_call",
"computer_call",
"code_interpreter_call",
"mcp_call",
"mcp_list_tools",
"mcp_approval_request",
"image_generation_call",
):
# Store built-in tool calls in additional_kwargs
if "tool_outputs" not in message.additional_kwargs:
message.additional_kwargs["tool_outputs"] = []
message.additional_kwargs["tool_outputs"].append(block)
elif block.get("type") == "function_call":
# Store function call item IDs in additional_kwargs, otherwise
# discard function call items.
if _FUNCTION_CALL_IDS_MAP_KEY not in message.additional_kwargs:
message.additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY] = {}
if (call_id := block.get("call_id")) and (
function_call_id := block.get("id")
):
message.additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY][
call_id
] = function_call_id
elif (block.get("type") == "refusal") and (
refusal := block.get("refusal")
):
# Store a refusal item in additional_kwargs (overwriting as in
# v0.3)
message.additional_kwargs["refusal"] = refusal
elif block.get("type") == "text":
# Store a message item ID on AIMessage.id
if "id" in block:
message.id = block["id"]
new_content.append({k: v for k, v in block.items() if k != "id"})
elif (
set(block.keys()) == {"id", "index"}
and isinstance(block["id"], str)
and block["id"].startswith("msg_")
):
# Drop message IDs in streaming case
new_content.append({"index": block["index"]})
else:
new_content.append(block)
else:
new_content.append(block)
message.content = new_content
if isinstance(message.id, str) and message.id.startswith("resp_"):
message.id = None
else:
pass
return message
Domain
Subdomains
Source
Frequently Asked Questions
What does _convert_to_v03_ai_message() do?
_convert_to_v03_ai_message() is a function in the langchain codebase, defined in libs/partners/openai/langchain_openai/chat_models/_compat.py.
Where is _convert_to_v03_ai_message() defined?
_convert_to_v03_ai_message() is defined in libs/partners/openai/langchain_openai/chat_models/_compat.py at line 81.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free