Home / File/ _merge.py — langchain Source File

_merge.py — langchain Source File

Architecture documentation for _merge.py, a python file in the langchain codebase. 1 imports, 0 dependents.

File python Observability CallbackManager 1 imports 3 functions

Entity Profile

Dependency Diagram

graph LR
  c118f9bd_45f4_d81d_4bb1_d6bae8563551["_merge.py"]
  feec1ec4_6917_867b_d228_b134d0ff8099["typing"]
  c118f9bd_45f4_d81d_4bb1_d6bae8563551 --> feec1ec4_6917_867b_d228_b134d0ff8099
  style c118f9bd_45f4_d81d_4bb1_d6bae8563551 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from __future__ import annotations

from typing import Any


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"}
// ... (134 more lines)

Domain

Subdomains

Dependencies

  • typing

Frequently Asked Questions

What does _merge.py do?
_merge.py is a source file in the langchain codebase, written in python. It belongs to the Observability domain, CallbackManager subdomain.
What functions are defined in _merge.py?
_merge.py defines 3 function(s): merge_dicts, merge_lists, merge_obj.
What does _merge.py depend on?
_merge.py imports 1 module(s): typing.
Where is _merge.py in the architecture?
_merge.py is located at libs/core/langchain_core/utils/_merge.py (domain: Observability, subdomain: CallbackManager, directory: libs/core/langchain_core/utils).

Analyze Your Own Codebase

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

Try Supermodel Free