Home / Function/ merge_obj() — langchain Function Reference

merge_obj() — langchain Function Reference

Architecture documentation for the merge_obj() function in _merge.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  8d8ca17c_07f0_a964_a165_8710319a9927["merge_obj()"]
  c118f9bd_45f4_d81d_4bb1_d6bae8563551["_merge.py"]
  8d8ca17c_07f0_a964_a165_8710319a9927 -->|defined in| c118f9bd_45f4_d81d_4bb1_d6bae8563551
  ecab3788_6d79_e81f_eeed_6ee38656c1d7["merge_dicts()"]
  8d8ca17c_07f0_a964_a165_8710319a9927 -->|calls| ecab3788_6d79_e81f_eeed_6ee38656c1d7
  fb0c3c36_ad00_663d_0e1a_2f6d8fe4af76["merge_lists()"]
  8d8ca17c_07f0_a964_a165_8710319a9927 -->|calls| fb0c3c36_ad00_663d_0e1a_2f6d8fe4af76
  style 8d8ca17c_07f0_a964_a165_8710319a9927 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/utils/_merge.py lines 155–193

def merge_obj(left: Any, right: Any) -> Any:
    """Merge two objects.

    It handles specific scenarios where a key exists in both dictionaries but has a
    value of `None` in `'left'`. In such cases, the method uses the value from `'right'`
    for that key in the merged dictionary.

    Args:
        left: The first object to merge.
        right: The other object to merge.

    Returns:
        The merged object.

    Raises:
        TypeError: If the key exists in both dictionaries but has a different type.
        ValueError: If the two objects cannot be merged.
    """
    if left is None or right is None:
        return left if left is not None else right
    if type(left) is not type(right):
        msg = (
            f"left and right are of different types. Left type:  {type(left)}. Right "
            f"type: {type(right)}."
        )
        raise TypeError(msg)
    if isinstance(left, str):
        return left + right
    if isinstance(left, dict):
        return merge_dicts(left, right)
    if isinstance(left, list):
        return merge_lists(left, right)
    if left == right:
        return left
    msg = (
        f"Unable to merge {left=} and {right=}. Both must be of type str, dict, or "
        f"list, or else be two equal objects."
    )
    raise ValueError(msg)

Domain

Subdomains

Frequently Asked Questions

What does merge_obj() do?
merge_obj() is a function in the langchain codebase, defined in libs/core/langchain_core/utils/_merge.py.
Where is merge_obj() defined?
merge_obj() is defined in libs/core/langchain_core/utils/_merge.py at line 155.
What does merge_obj() call?
merge_obj() calls 2 function(s): merge_dicts, merge_lists.

Analyze Your Own Codebase

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

Try Supermodel Free