Home / Function/ _dict_int_op() — langchain Function Reference

_dict_int_op() — langchain Function Reference

Architecture documentation for the _dict_int_op() function in usage.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  89e28fd7_8637_6672_7287_bd30163e8f21["_dict_int_op()"]
  a92edc37_9805_e57d_c407_de78e000ce20["usage.py"]
  89e28fd7_8637_6672_7287_bd30163e8f21 -->|defined in| a92edc37_9805_e57d_c407_de78e000ce20
  style 89e28fd7_8637_6672_7287_bd30163e8f21 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/utils/usage.py lines 6–60

def _dict_int_op(
    left: dict,
    right: dict,
    op: Callable[[int, int], int],
    *,
    default: int = 0,
    depth: int = 0,
    max_depth: int = 100,
) -> dict:
    """Apply an integer operation to corresponding values in two dictionaries.

    Recursively combines two dictionaries by applying the given operation to integer
    values at matching keys.

    Supports nested dictionaries.

    Args:
        left: First dictionary to combine.
        right: Second dictionary to combine.
        op: Binary operation function to apply to integer values.
        default: Default value to use when a key is missing from a dictionary.
        depth: Current recursion depth (used internally).
        max_depth: Maximum recursion depth (to prevent infinite loops).

    Returns:
        A new dictionary with combined values.

    Raises:
        ValueError: If `max_depth` is exceeded or if value types are not supported.
    """
    if depth >= max_depth:
        msg = f"{max_depth=} exceeded, unable to combine dicts."
        raise ValueError(msg)
    combined: dict = {}
    for k in set(left).union(right):
        if isinstance(left.get(k, default), int) and isinstance(
            right.get(k, default), int
        ):
            combined[k] = op(left.get(k, default), right.get(k, default))
        elif isinstance(left.get(k, {}), dict) and isinstance(right.get(k, {}), dict):
            combined[k] = _dict_int_op(
                left.get(k, {}),
                right.get(k, {}),
                op,
                default=default,
                depth=depth + 1,
                max_depth=max_depth,
            )
        else:
            types = [type(d[k]) for d in (left, right) if k in d]
            msg = (
                f"Unknown value types: {types}. Only dict and int values are supported."
            )
            raise ValueError(msg)  # noqa: TRY004
    return combined

Domain

Subdomains

Frequently Asked Questions

What does _dict_int_op() do?
_dict_int_op() is a function in the langchain codebase, defined in libs/core/langchain_core/utils/usage.py.
Where is _dict_int_op() defined?
_dict_int_op() is defined in libs/core/langchain_core/utils/usage.py at line 6.

Analyze Your Own Codebase

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

Try Supermodel Free