usage.py — langchain Source File
Architecture documentation for usage.py, a python file in the langchain codebase. 1 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR a92edc37_9805_e57d_c407_de78e000ce20["usage.py"] 2bf6d401_816d_d011_3b05_a6114f55ff58["collections.abc"] a92edc37_9805_e57d_c407_de78e000ce20 --> 2bf6d401_816d_d011_3b05_a6114f55ff58 style a92edc37_9805_e57d_c407_de78e000ce20 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
"""Usage utilities."""
from collections.abc import Callable
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
Functions
Dependencies
- collections.abc
Source
Frequently Asked Questions
What does usage.py do?
usage.py is a source file in the langchain codebase, written in python. It belongs to the Observability domain, TelemetryUtilities subdomain.
What functions are defined in usage.py?
usage.py defines 1 function(s): _dict_int_op.
What does usage.py depend on?
usage.py imports 1 module(s): collections.abc.
Where is usage.py in the architecture?
usage.py is located at libs/core/langchain_core/utils/usage.py (domain: Observability, subdomain: TelemetryUtilities, 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