Home / Function/ _convert_dict_to_message() — langchain Function Reference

_convert_dict_to_message() — langchain Function Reference

Architecture documentation for the _convert_dict_to_message() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  a917de1a_4c3d_a1f9_75da_69038ccf1450["_convert_dict_to_message()"]
  2b046911_ea21_8e2e_ba0d_9d03da8d7bda["base.py"]
  a917de1a_4c3d_a1f9_75da_69038ccf1450 -->|defined in| 2b046911_ea21_8e2e_ba0d_9d03da8d7bda
  64924e61_d0ac_6bbf_4818_aeb51576556d["_create_chat_result()"]
  64924e61_d0ac_6bbf_4818_aeb51576556d -->|calls| a917de1a_4c3d_a1f9_75da_69038ccf1450
  style a917de1a_4c3d_a1f9_75da_69038ccf1450 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/openai/langchain_openai/chat_models/base.py lines 161–225

def _convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage:
    """Convert a dictionary to a LangChain message.

    Args:
        _dict: The dictionary.

    Returns:
        The LangChain message.
    """
    role = _dict.get("role")
    name = _dict.get("name")
    id_ = _dict.get("id")
    if role == "user":
        return HumanMessage(content=_dict.get("content", ""), id=id_, name=name)
    if role == "assistant":
        # Fix for azure
        # Also OpenAI returns None for tool invocations
        content = _dict.get("content", "") or ""
        additional_kwargs: dict = {}
        if function_call := _dict.get("function_call"):
            additional_kwargs["function_call"] = dict(function_call)
        tool_calls = []
        invalid_tool_calls = []
        if raw_tool_calls := _dict.get("tool_calls"):
            for raw_tool_call in raw_tool_calls:
                try:
                    tool_calls.append(parse_tool_call(raw_tool_call, return_id=True))
                except Exception as e:
                    invalid_tool_calls.append(
                        make_invalid_tool_call(raw_tool_call, str(e))
                    )
        if audio := _dict.get("audio"):
            additional_kwargs["audio"] = audio
        return AIMessage(
            content=content,
            additional_kwargs=additional_kwargs,
            name=name,
            id=id_,
            tool_calls=tool_calls,
            invalid_tool_calls=invalid_tool_calls,
        )
    if role in ("system", "developer"):
        additional_kwargs = {"__openai_role__": role} if role == "developer" else {}
        return SystemMessage(
            content=_dict.get("content", ""),
            name=name,
            id=id_,
            additional_kwargs=additional_kwargs,
        )
    if role == "function":
        return FunctionMessage(
            content=_dict.get("content", ""), name=cast(str, _dict.get("name")), id=id_
        )
    if role == "tool":
        additional_kwargs = {}
        if "name" in _dict:
            additional_kwargs["name"] = _dict["name"]
        return ToolMessage(
            content=_dict.get("content", ""),
            tool_call_id=cast(str, _dict.get("tool_call_id")),
            additional_kwargs=additional_kwargs,
            name=name,
            id=id_,
        )
    return ChatMessage(content=_dict.get("content", ""), role=role, id=id_)  # type: ignore[arg-type]

Domain

Subdomains

Frequently Asked Questions

What does _convert_dict_to_message() do?
_convert_dict_to_message() is a function in the langchain codebase, defined in libs/partners/openai/langchain_openai/chat_models/base.py.
Where is _convert_dict_to_message() defined?
_convert_dict_to_message() is defined in libs/partners/openai/langchain_openai/chat_models/base.py at line 161.
What calls _convert_dict_to_message()?
_convert_dict_to_message() is called by 1 function(s): _create_chat_result.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free