_convert_message_to_dict() — langchain Function Reference
Architecture documentation for the _convert_message_to_dict() function in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD fd643003_13df_3a67_6bdc_07576981e414["_convert_message_to_dict()"] 2b046911_ea21_8e2e_ba0d_9d03da8d7bda["base.py"] fd643003_13df_3a67_6bdc_07576981e414 -->|defined in| 2b046911_ea21_8e2e_ba0d_9d03da8d7bda 36b15b48_0822_029c_4a53_8243405e5a5e["_get_request_payload()"] 36b15b48_0822_029c_4a53_8243405e5a5e -->|calls| fd643003_13df_3a67_6bdc_07576981e414 3d7b9c9e_fae0_940a_5e96_75a84e2ad11c["get_num_tokens_from_messages()"] 3d7b9c9e_fae0_940a_5e96_75a84e2ad11c -->|calls| fd643003_13df_3a67_6bdc_07576981e414 b988bc7d_ceff_06f1_193c_a22abc7a149f["_construct_responses_api_input()"] b988bc7d_ceff_06f1_193c_a22abc7a149f -->|calls| fd643003_13df_3a67_6bdc_07576981e414 1f060587_17a0_0b26_d72a_02b23bd5460f["_format_message_content()"] fd643003_13df_3a67_6bdc_07576981e414 -->|calls| 1f060587_17a0_0b26_d72a_02b23bd5460f 61a7b00c_e050_782c_9b96_1375acca1541["_lc_tool_call_to_openai_tool_call()"] fd643003_13df_3a67_6bdc_07576981e414 -->|calls| 61a7b00c_e050_782c_9b96_1375acca1541 c5d6c2a5_bdf3_8608_6524_003dc628d0ed["_lc_invalid_tool_call_to_openai_tool_call()"] fd643003_13df_3a67_6bdc_07576981e414 -->|calls| c5d6c2a5_bdf3_8608_6524_003dc628d0ed style fd643003_13df_3a67_6bdc_07576981e414 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/partners/openai/langchain_openai/chat_models/base.py lines 289–366
def _convert_message_to_dict(
message: BaseMessage,
api: Literal["chat/completions", "responses"] = "chat/completions",
) -> dict:
"""Convert a LangChain message to dictionary format expected by OpenAI."""
message_dict: dict[str, Any] = {
"content": _format_message_content(message.content, api=api, role=message.type)
}
if (name := message.name or message.additional_kwargs.get("name")) is not None:
message_dict["name"] = name
# populate role and additional message data
if isinstance(message, ChatMessage):
message_dict["role"] = message.role
elif isinstance(message, HumanMessage):
message_dict["role"] = "user"
elif isinstance(message, AIMessage):
message_dict["role"] = "assistant"
if message.tool_calls or message.invalid_tool_calls:
message_dict["tool_calls"] = [
_lc_tool_call_to_openai_tool_call(tc) for tc in message.tool_calls
] + [
_lc_invalid_tool_call_to_openai_tool_call(tc)
for tc in message.invalid_tool_calls
]
elif "tool_calls" in message.additional_kwargs:
message_dict["tool_calls"] = message.additional_kwargs["tool_calls"]
tool_call_supported_props = {"id", "type", "function"}
message_dict["tool_calls"] = [
{k: v for k, v in tool_call.items() if k in tool_call_supported_props}
for tool_call in message_dict["tool_calls"]
]
elif "function_call" in message.additional_kwargs:
# OpenAI raises 400 if both function_call and tool_calls are present in the
# same message.
message_dict["function_call"] = message.additional_kwargs["function_call"]
else:
pass
# If tool calls present, content null value should be None not empty string.
if "function_call" in message_dict or "tool_calls" in message_dict:
message_dict["content"] = message_dict["content"] or None
audio: dict[str, Any] | None = None
for block in message.content:
if (
isinstance(block, dict)
and block.get("type") == "audio"
and (id_ := block.get("id"))
and api != "responses"
):
# openai doesn't support passing the data back - only the id
# https://platform.openai.com/docs/guides/audio/multi-turn-conversations
audio = {"id": id_}
if not audio and "audio" in message.additional_kwargs:
raw_audio = message.additional_kwargs["audio"]
audio = (
{"id": message.additional_kwargs["audio"]["id"]}
if "id" in raw_audio
else raw_audio
)
if audio:
message_dict["audio"] = audio
elif isinstance(message, SystemMessage):
message_dict["role"] = message.additional_kwargs.get(
"__openai_role__", "system"
)
elif isinstance(message, FunctionMessage):
message_dict["role"] = "function"
elif isinstance(message, ToolMessage):
message_dict["role"] = "tool"
message_dict["tool_call_id"] = message.tool_call_id
supported_props = {"content", "role", "tool_call_id"}
message_dict = {k: v for k, v in message_dict.items() if k in supported_props}
else:
msg = f"Got unknown type {message}"
raise TypeError(msg)
return message_dict
Domain
Subdomains
Calls
Source
Frequently Asked Questions
What does _convert_message_to_dict() do?
_convert_message_to_dict() is a function in the langchain codebase, defined in libs/partners/openai/langchain_openai/chat_models/base.py.
Where is _convert_message_to_dict() defined?
_convert_message_to_dict() is defined in libs/partners/openai/langchain_openai/chat_models/base.py at line 289.
What does _convert_message_to_dict() call?
_convert_message_to_dict() calls 3 function(s): _format_message_content, _lc_invalid_tool_call_to_openai_tool_call, _lc_tool_call_to_openai_tool_call.
What calls _convert_message_to_dict()?
_convert_message_to_dict() is called by 3 function(s): _construct_responses_api_input, _get_request_payload, get_num_tokens_from_messages.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free