Home / Function/ merge_dicts() — langchain Function Reference

merge_dicts() — langchain Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

libs/core/langchain_core/utils/_merge.py lines 6–79

def merge_dicts(left: dict[str, Any], *others: dict[str, Any]) -> dict[str, Any]:
    r"""Merge dictionaries.

    Merge many dicts, handling 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 dictionary to merge.
        others: The other dictionaries to merge.

    Returns:
        The merged dictionary.

    Raises:
        TypeError: If the key exists in both dictionaries but has a different type.
        TypeError: If the value has an unsupported type.

    Example:
        If `left = {"function_call": {"arguments": None}}` and
        `right = {"function_call": {"arguments": "{\n"}}`, then, after merging, for the
        key `'function_call'`, the value from `'right'` is used, resulting in
        `merged = {"function_call": {"arguments": "{\n"}}`.
    """
    merged = left.copy()
    for right in others:
        for right_k, right_v in right.items():
            if right_k not in merged or (
                right_v is not None and merged[right_k] is None
            ):
                merged[right_k] = right_v
            elif right_v is None:
                continue
            elif type(merged[right_k]) is not type(right_v):
                msg = (
                    f'additional_kwargs["{right_k}"] already exists in this message,'
                    " but with a different type."
                )
                raise TypeError(msg)
            elif isinstance(merged[right_k], str):
                # TODO: Add below special handling for 'type' key in 0.3 and remove
                # merge_lists 'type' logic.
                #
                # if right_k == "type":
                #     if merged[right_k] == right_v:
                #         continue
                #     else:
                #         raise ValueError(
                #             "Unable to merge. Two different values seen for special "
                #             f"key 'type': {merged[right_k]} and {right_v}. 'type' "
                #             "should either occur once or have the same value across "
                #             "all dicts."
                #         )
                if (right_k == "index" and merged[right_k].startswith("lc_")) or (
                    right_k in {"id", "output_version", "model_provider"}
                    and merged[right_k] == right_v
                ):
                    continue
                merged[right_k] += right_v
            elif isinstance(merged[right_k], dict):
                merged[right_k] = merge_dicts(merged[right_k], right_v)
            elif isinstance(merged[right_k], list):
                merged[right_k] = merge_lists(merged[right_k], right_v)
            elif merged[right_k] == right_v:
                continue
            elif isinstance(merged[right_k], int):
                merged[right_k] += right_v
            else:
                msg = (
                    f"Additional kwargs key {right_k} already exists in left dict and "
                    f"value has unsupported type {type(merged[right_k])}."
                )
                raise TypeError(msg)
    return merged

Domain

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free