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 chat_models.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  fd171c90_ac0f_ba1e_69ee_481dcec39d70["_convert_dict_to_message()"]
  9591e0fc_6d18_28d3_cea9_e4a5cf7f2b59["chat_models.py"]
  fd171c90_ac0f_ba1e_69ee_481dcec39d70 -->|defined in| 9591e0fc_6d18_28d3_cea9_e4a5cf7f2b59
  64320ac7_229e_69ac_e55f_504a7f3a1ee4["_create_chat_result()"]
  64320ac7_229e_69ac_e55f_504a7f3a1ee4 -->|calls| fd171c90_ac0f_ba1e_69ee_481dcec39d70
  style fd171c90_ac0f_ba1e_69ee_481dcec39d70 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/groq/langchain_groq/chat_models.py lines 1450–1513

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.

    """
    id_ = _dict.get("id")
    role = _dict.get("role")
    if role == "user":
        return HumanMessage(content=_dict.get("content", ""))
    if role == "assistant":
        content = _dict.get("content", "") or ""
        additional_kwargs: dict = {}
        if reasoning := _dict.get("reasoning"):
            additional_kwargs["reasoning_content"] = reasoning
        if executed_tools := _dict.get("executed_tools"):
            additional_kwargs["executed_tools"] = executed_tools
        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"):
            # Groq sends 'null' (JSON null) for tools with no arguments, but we
            # expect '{}' (empty JSON object) to represent empty arguments
            for raw_tool_call in raw_tool_calls:
                if (
                    raw_tool_call.get("function")
                    and raw_tool_call["function"].get("arguments") == "null"
                ):
                    raw_tool_call["function"]["arguments"] = "{}"
            additional_kwargs["tool_calls"] = raw_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:  # pylint: disable=broad-except
                    invalid_tool_calls.append(
                        make_invalid_tool_call(raw_tool_call, str(e))
                    )
        return AIMessage(
            content=content,
            id=id_,
            additional_kwargs=additional_kwargs,
            tool_calls=tool_calls,
            invalid_tool_calls=invalid_tool_calls,
            response_metadata={"model_provider": "groq"},
        )
    if role == "system":
        return SystemMessage(content=_dict.get("content", ""))
    if role == "function":
        return FunctionMessage(content=_dict.get("content", ""), name=_dict.get("name"))  # type: ignore[arg-type]
    if role == "tool":
        additional_kwargs = {}
        if "name" in _dict:
            additional_kwargs["name"] = _dict["name"]
        return ToolMessage(
            content=_dict.get("content", ""),
            tool_call_id=_dict.get("tool_call_id"),
            additional_kwargs=additional_kwargs,
        )
    return ChatMessage(content=_dict.get("content", ""), role=role)  # 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/groq/langchain_groq/chat_models.py.
Where is _convert_dict_to_message() defined?
_convert_dict_to_message() is defined in libs/partners/groq/langchain_groq/chat_models.py at line 1450.
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