Home / Function/ get_function_nonlocals() — langchain Function Reference

get_function_nonlocals() — langchain Function Reference

Architecture documentation for the get_function_nonlocals() function in utils.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  4fdaed9e_1394_e322_9099_a2ef566ec1a8["get_function_nonlocals()"]
  ca66092c_447c_d201_0d3c_cfa6ca2cc9d3["utils.py"]
  4fdaed9e_1394_e322_9099_a2ef566ec1a8 -->|defined in| ca66092c_447c_d201_0d3c_cfa6ca2cc9d3
  style 4fdaed9e_1394_e322_9099_a2ef566ec1a8 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/runnables/utils.py lines 408–447

def get_function_nonlocals(func: Callable) -> list[Any]:
    """Get the nonlocal variables accessed by a function.

    Args:
        func: The function to check.

    Returns:
        The nonlocal variables accessed by the function.
    """
    try:
        code = inspect.getsource(func)
        tree = ast.parse(textwrap.dedent(code))
        visitor = FunctionNonLocals()
        visitor.visit(tree)
        values: list[Any] = []
        closure = (
            inspect.getclosurevars(func.__wrapped__)
            if hasattr(func, "__wrapped__") and callable(func.__wrapped__)
            else inspect.getclosurevars(func)
        )
        candidates = {**closure.globals, **closure.nonlocals}
        for k, v in candidates.items():
            if k in visitor.nonlocals:
                values.append(v)
            for kk in visitor.nonlocals:
                if "." in kk and kk.startswith(k):
                    vv = v
                    for part in kk.split(".")[1:]:
                        if vv is None:
                            break
                        try:
                            vv = getattr(vv, part)
                        except AttributeError:
                            break
                    else:
                        values.append(vv)
    except (SyntaxError, TypeError, OSError, SystemError):
        return []

    return values

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free