Home / Function/ _retrieve_ref() — langchain Function Reference

_retrieve_ref() — langchain Function Reference

Architecture documentation for the _retrieve_ref() function in json_schema.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  e7e60ba1_e3e1_fdff_3530_2a299a36080c["_retrieve_ref()"]
  e949a363_87d5_a34f_ea76_18a6a6268498["json_schema.py"]
  e7e60ba1_e3e1_fdff_3530_2a299a36080c -->|defined in| e949a363_87d5_a34f_ea76_18a6a6268498
  f0fb6db9_b0d7_49d4_8b5e_33f7376daf2a["_dereference_refs_helper()"]
  f0fb6db9_b0d7_49d4_8b5e_33f7376daf2a -->|calls| e7e60ba1_e3e1_fdff_3530_2a299a36080c
  style e7e60ba1_e3e1_fdff_3530_2a299a36080c fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/utils/json_schema.py lines 12–55

def _retrieve_ref(path: str, schema: dict) -> list | dict:
    """Retrieve a referenced object from a JSON schema using a path.

    Resolves JSON schema references (e.g., `'#/definitions/MyType'`) by traversing the
    schema structure.

    Args:
        path: Reference path starting with `'#'` (e.g., `'#/definitions/MyType'`).
        schema: The JSON schema dictionary to search in.

    Returns:
        A deep copy of the referenced object (dict or list).

    Raises:
        ValueError: If the path does not start with `'#'`.
        KeyError: If the reference path is not found in the schema.
    """
    components = path.split("/")
    if components[0] != "#":
        msg = (
            "ref paths are expected to be URI fragments, meaning they should start "
            "with #."
        )
        raise ValueError(msg)
    out: list | dict = schema
    for component in components[1:]:
        if component in out:
            if isinstance(out, list):
                msg = f"Reference '{path}' not found."
                raise KeyError(msg)
            out = out[component]
        elif component.isdigit():
            index = int(component)
            if (isinstance(out, list) and 0 <= index < len(out)) or (
                isinstance(out, dict) and index in out
            ):
                out = out[index]
            else:
                msg = f"Reference '{path}' not found."
                raise KeyError(msg)
        else:
            msg = f"Reference '{path}' not found."
            raise KeyError(msg)
    return deepcopy(out)

Subdomains

Frequently Asked Questions

What does _retrieve_ref() do?
_retrieve_ref() is a function in the langchain codebase, defined in libs/core/langchain_core/utils/json_schema.py.
Where is _retrieve_ref() defined?
_retrieve_ref() is defined in libs/core/langchain_core/utils/json_schema.py at line 12.
What calls _retrieve_ref()?
_retrieve_ref() is called by 1 function(s): _dereference_refs_helper.

Analyze Your Own Codebase

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

Try Supermodel Free