Home / Function/ _get_key() — langchain Function Reference

_get_key() — langchain Function Reference

Architecture documentation for the _get_key() function in mustache.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  fef86d61_ac5c_9959_034b_78139e1c1dcc["_get_key()"]
  88ae0419_8f6b_8b31_dd8f_a3bc6bbcb7b6["mustache.py"]
  fef86d61_ac5c_9959_034b_78139e1c1dcc -->|defined in| 88ae0419_8f6b_8b31_dd8f_a3bc6bbcb7b6
  0d68eb8b_8003_f5a3_d717_126f0919d1af["render()"]
  0d68eb8b_8003_f5a3_d717_126f0919d1af -->|calls| fef86d61_ac5c_9959_034b_78139e1c1dcc
  style fef86d61_ac5c_9959_034b_78139e1c1dcc fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/utils/mustache.py lines 345–442

def _get_key(
    key: str,
    scopes: Scopes,
    *,
    warn: bool,
    keep: bool,
    def_ldel: str,
    def_rdel: str,
) -> Any:
    """Retrieve a value from the current scope using a dot-separated key path.

    Traverses through nested dictionaries and lists using dot notation.

    Supports special key `'.'` to return the current scope.

    Args:
        key: Dot-separated key path (e.g., `'user.name'` or `'.'` for current scope).
        scopes: List of scope dictionaries to search through.
        warn: Whether to log a warning when a key is not found.
        keep: Whether to return the original template tag when key is not found.
        def_ldel: Left delimiter for template (used when keep is `True`).
        def_rdel: Right delimiter for template (used when keep is `True`).

    Returns:
        The value found at the key path.

            If not found, returns the original template tag when keep is `True`,
            otherwise returns an empty string.
    """
    # If the key is a dot
    if key == ".":
        # Then just return the current scope
        return scopes[0]

    # Loop through the scopes
    for scope in scopes:
        try:
            # Return an empty string if falsy, with two exceptions
            # 0 should return 0, and False should return False
            if scope in (0, False):
                return scope

            resolved_scope = scope
            # For every dot separated key
            for child in key.split("."):
                # Return an empty string if falsy, with two exceptions
                # 0 should return 0, and False should return False
                if resolved_scope in (0, False):
                    return resolved_scope
                # Move into the scope
                if isinstance(resolved_scope, dict):
                    try:
                        resolved_scope = resolved_scope[child]
                    except (KeyError, TypeError):
                        # Key not found - will be caught by outer try-except
                        msg = f"Key {child!r} not found in dict"
                        raise KeyError(msg) from None
                elif isinstance(resolved_scope, (list, tuple)):
                    try:
                        resolved_scope = resolved_scope[int(child)]
                    except (ValueError, IndexError, TypeError):
                        # Invalid index - will be caught by outer try-except
                        msg = f"Invalid index {child!r} for list/tuple"
                        raise IndexError(msg) from None
                else:
                    # Reject everything else for security
                    # This prevents traversing into arbitrary Python objects
                    msg = (
                        f"Cannot traverse into {type(resolved_scope).__name__}. "
                        "Mustache templates only support dict, list, and tuple. "
                        f"Got: {type(resolved_scope)}"
                    )
                    raise TypeError(msg)  # noqa: TRY301

            try:
                # This allows for custom falsy data types
                # https://github.com/noahmorrison/chevron/issues/35
                if resolved_scope._CHEVRON_return_scope_when_falsy:  # type: ignore[union-attr] # noqa: SLF001
                    return resolved_scope
            except AttributeError:
                if resolved_scope in (0, False):

Domain

Subdomains

Called By

Frequently Asked Questions

What does _get_key() do?
_get_key() is a function in the langchain codebase, defined in libs/core/langchain_core/utils/mustache.py.
Where is _get_key() defined?
_get_key() is defined in libs/core/langchain_core/utils/mustache.py at line 345.
What calls _get_key()?
_get_key() is called by 1 function(s): render.

Analyze Your Own Codebase

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

Try Supermodel Free