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

Entity Profile

Dependency Diagram

graph TD
  917b18be_e70d_9247_df2c_672c2650d845["_convert_dict_to_message()"]
  d84d1503_7d4c_e393_0d48_409c5faa5e2d["huggingface.py"]
  917b18be_e70d_9247_df2c_672c2650d845 -->|defined in| d84d1503_7d4c_e393_0d48_409c5faa5e2d
  65387b9f_2b3b_0088_ea1b_1b80c0d70ec0["_create_chat_result()"]
  65387b9f_2b3b_0088_ea1b_1b80c0d70ec0 -->|calls| 917b18be_e70d_9247_df2c_672c2650d845
  style 917b18be_e70d_9247_df2c_672c2650d845 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py lines 183–233

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")
    if role == "user":
        return HumanMessage(content=_dict.get("content", ""))
    if role == "assistant":
        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"):
            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:
                    invalid_tool_calls.append(
                        dict(make_invalid_tool_call(raw_tool_call, str(e)))
                    )
        return AIMessage(
            content=content,
            additional_kwargs=additional_kwargs,
            tool_calls=tool_calls,
            invalid_tool_calls=invalid_tool_calls,
        )
    if role == "system":
        return SystemMessage(content=_dict.get("content", ""))
    if role == "function":
        return FunctionMessage(
            content=_dict.get("content", ""), name=_dict.get("name", "")
        )
    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 or "")

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/huggingface/langchain_huggingface/chat_models/huggingface.py.
Where is _convert_dict_to_message() defined?
_convert_dict_to_message() is defined in libs/partners/huggingface/langchain_huggingface/chat_models/huggingface.py at line 183.
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