Home / Function/ _dereference_refs_helper() — langchain Function Reference

_dereference_refs_helper() — langchain Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

libs/core/langchain_core/utils/json_schema.py lines 83–185

def _dereference_refs_helper(
    obj: Any,
    full_schema: dict[str, Any],
    processed_refs: set[str] | None,
    skip_keys: Sequence[str],
    *,
    shallow_refs: bool,
) -> Any:
    """Dereference JSON Schema $ref objects, handling both pure and mixed references.

    This function processes JSON Schema objects containing $ref properties by resolving
    the references and merging any additional properties. It handles:

    - Pure `$ref` objects: `{"$ref": "#/path/to/definition"}`
    - Mixed `$ref` objects: `{"$ref": "#/path", "title": "Custom Title", ...}`
    - Circular references by breaking cycles and preserving non-ref properties

    Args:
        obj: The object to process (can be dict, list, or primitive)
        full_schema: The complete schema containing all definitions
        processed_refs: Set tracking currently processing refs (for cycle detection)
        skip_keys: Keys under which to skip recursion
        shallow_refs: If `True`, only break cycles; if `False`, deep-inline all refs

    Returns:
        The object with `$ref` properties resolved and merged with other properties.
    """
    if processed_refs is None:
        processed_refs = set()

    # Case 1: Object contains a $ref property (pure or mixed with additional properties)
    if isinstance(obj, dict) and "$ref" in obj:
        ref_path = obj["$ref"]
        additional_properties = {
            key: value for key, value in obj.items() if key != "$ref"
        }

        # Detect circular reference: if we're already processing this $ref,
        # return only the additional properties to break the cycle
        if ref_path in processed_refs:
            return _process_dict_properties(
                additional_properties,
                full_schema,
                processed_refs,
                skip_keys,
                shallow_refs=shallow_refs,
            )

        # Mark this reference as being processed (for cycle detection)
        processed_refs.add(ref_path)

        # Fetch and recursively resolve the referenced object
        referenced_object = deepcopy(_retrieve_ref(ref_path, full_schema))
        resolved_reference = _dereference_refs_helper(
            referenced_object,
            full_schema,
            processed_refs,
            skip_keys,
            shallow_refs=shallow_refs,
        )

        # Clean up: remove from processing set before returning
        processed_refs.remove(ref_path)

        # Pure $ref case: no additional properties, return resolved reference directly
        if not additional_properties:
            return resolved_reference

        # Mixed $ref case: merge resolved reference with additional properties
        # Additional properties take precedence over resolved properties
        merged_result = {}
        if isinstance(resolved_reference, dict):
            merged_result.update(resolved_reference)

        # Process additional properties and merge them (they override resolved ones)
        processed_additional = _process_dict_properties(
            additional_properties,
            full_schema,
            processed_refs,
            skip_keys,
            shallow_refs=shallow_refs,

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free