Home / Function/ merge_content() — langchain Function Reference

merge_content() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  b2afdd92_252e_1996_8bb5_0f9fe3a5a3e1["merge_content()"]
  323271a6_dbeb_1ac5_e224_a66bfb56191b["base.py"]
  b2afdd92_252e_1996_8bb5_0f9fe3a5a3e1 -->|defined in| 323271a6_dbeb_1ac5_e224_a66bfb56191b
  c8d6f5a3_7591_846c_3f4f_2dbb710f44eb["__add__()"]
  c8d6f5a3_7591_846c_3f4f_2dbb710f44eb -->|calls| b2afdd92_252e_1996_8bb5_0f9fe3a5a3e1
  style b2afdd92_252e_1996_8bb5_0f9fe3a5a3e1 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/messages/base.py lines 366–406

def merge_content(
    first_content: str | list[str | dict],
    *contents: str | list[str | dict],
) -> str | list[str | dict]:
    """Merge multiple message contents.

    Args:
        first_content: The first `content`. Can be a string or a list.
        contents: The other `content`s. Can be a string or a list.

    Returns:
        The merged content.

    """
    merged: str | list[str | dict]
    merged = "" if first_content is None else first_content

    for content in contents:
        # If current is a string
        if isinstance(merged, str):
            # If the next chunk is also a string, then merge them naively
            if isinstance(content, str):
                merged += content
            # If the next chunk is a list, add the current to the start of the list
            else:
                merged = [merged, *content]
        elif isinstance(content, list):
            # If both are lists
            merged = merge_lists(cast("list", merged), content)  # type: ignore[assignment]
        # If the first content is a list, and the second content is a string
        # If the last element of the first content is a string
        # Add the second content to the last element
        elif merged and isinstance(merged[-1], str):
            merged[-1] += content
        # If second content is an empty string, treat as a no-op
        elif content == "":
            pass
        # Otherwise, add the second content as a new element of the list
        elif merged:
            merged.append(content)
    return merged

Subdomains

Called By

Frequently Asked Questions

What does merge_content() do?
merge_content() is a function in the langchain codebase, defined in libs/core/langchain_core/messages/base.py.
Where is merge_content() defined?
merge_content() is defined in libs/core/langchain_core/messages/base.py at line 366.
What calls merge_content()?
merge_content() is called by 1 function(s): __add__.

Analyze Your Own Codebase

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

Try Supermodel Free