Home / Function/ _convert_delta_to_message_chunk() — langchain Function Reference

_convert_delta_to_message_chunk() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  125c7e66_41f8_c4be_7430_7f650e0e9678["_convert_delta_to_message_chunk()"]
  2b046911_ea21_8e2e_ba0d_9d03da8d7bda["base.py"]
  125c7e66_41f8_c4be_7430_7f650e0e9678 -->|defined in| 2b046911_ea21_8e2e_ba0d_9d03da8d7bda
  9dd73ff5_bb27_7bf2_5124_b82e93cd60f6["_convert_chunk_to_generation_chunk()"]
  9dd73ff5_bb27_7bf2_5124_b82e93cd60f6 -->|calls| 125c7e66_41f8_c4be_7430_7f650e0e9678
  style 125c7e66_41f8_c4be_7430_7f650e0e9678 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/partners/openai/langchain_openai/chat_models/base.py lines 369–422

def _convert_delta_to_message_chunk(
    _dict: Mapping[str, Any], default_class: type[BaseMessageChunk]
) -> BaseMessageChunk:
    """Convert to a LangChain message chunk."""
    id_ = _dict.get("id")
    role = cast(str, _dict.get("role"))
    content = cast(str, _dict.get("content") or "")
    additional_kwargs: dict = {}
    if _dict.get("function_call"):
        function_call = dict(_dict["function_call"])
        if "name" in function_call and function_call["name"] is None:
            function_call["name"] = ""
        additional_kwargs["function_call"] = function_call
    tool_call_chunks = []
    if raw_tool_calls := _dict.get("tool_calls"):
        try:
            tool_call_chunks = [
                tool_call_chunk(
                    name=rtc["function"].get("name"),
                    args=rtc["function"].get("arguments"),
                    id=rtc.get("id"),
                    index=rtc["index"],
                )
                for rtc in raw_tool_calls
            ]
        except KeyError:
            pass

    if role == "user" or default_class == HumanMessageChunk:
        return HumanMessageChunk(content=content, id=id_)
    if role == "assistant" or default_class == AIMessageChunk:
        return AIMessageChunk(
            content=content,
            additional_kwargs=additional_kwargs,
            id=id_,
            tool_call_chunks=tool_call_chunks,  # type: ignore[arg-type]
        )
    if role in ("system", "developer") or default_class == SystemMessageChunk:
        if role == "developer":
            additional_kwargs = {"__openai_role__": "developer"}
        else:
            additional_kwargs = {}
        return SystemMessageChunk(
            content=content, id=id_, additional_kwargs=additional_kwargs
        )
    if role == "function" or default_class == FunctionMessageChunk:
        return FunctionMessageChunk(content=content, name=_dict["name"], id=id_)
    if role == "tool" or default_class == ToolMessageChunk:
        return ToolMessageChunk(
            content=content, tool_call_id=_dict["tool_call_id"], id=id_
        )
    if role or default_class == ChatMessageChunk:
        return ChatMessageChunk(content=content, role=role, id=id_)
    return default_class(content=content, id=id_)  # type: ignore[call-arg]

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free