Home / Function/ loads() — langchain Function Reference

loads() — langchain Function Reference

Architecture documentation for the loads() function in load.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  04c46210_2f06_371b_9374_109532581bd7["loads()"]
  05e0e20a_4425_e962_37a4_c6c6f08a700d["load.py"]
  04c46210_2f06_371b_9374_109532581bd7 -->|defined in| 05e0e20a_4425_e962_37a4_c6c6f08a700d
  1910dab6_e109_80ec_5bdc_6188e60ebf46["default_init_validator()"]
  1910dab6_e109_80ec_5bdc_6188e60ebf46 -->|calls| 04c46210_2f06_371b_9374_109532581bd7
  00eb90c5_a3e3_3bb4_5533_671836ff8211["load()"]
  04c46210_2f06_371b_9374_109532581bd7 -->|calls| 00eb90c5_a3e3_3bb4_5533_671836ff8211
  style 04c46210_2f06_371b_9374_109532581bd7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/load/load.py lines 474–552

def loads(
    text: str,
    *,
    allowed_objects: Iterable[AllowedObject] | Literal["all", "core"] = "core",
    secrets_map: dict[str, str] | None = None,
    valid_namespaces: list[str] | None = None,
    secrets_from_env: bool = False,
    additional_import_mappings: dict[tuple[str, ...], tuple[str, ...]] | None = None,
    ignore_unserializable_fields: bool = False,
    init_validator: InitValidator | None = default_init_validator,
) -> Any:
    """Revive a LangChain class from a JSON string.

    Equivalent to `load(json.loads(text))`.

    Only classes in the allowlist can be instantiated. The default allowlist includes
    core LangChain types (messages, prompts, documents, etc.). See
    `langchain_core.load.mapping` for the full list.

    !!! warning "Beta feature"

        This is a beta feature. Please be wary of deploying experimental code to
        production unless you've taken appropriate precautions.

    Args:
        text: The string to load.
        allowed_objects: Allowlist of classes that can be deserialized.

            - `'core'` (default): Allow classes defined in the serialization mappings
                for `langchain_core`.
            - `'all'`: Allow classes defined in the serialization mappings.

                This includes core LangChain types (messages, prompts, documents, etc.)
                and trusted partner integrations. See `langchain_core.load.mapping` for
                the full list.

            - Explicit list of classes: Only those specific classes are allowed.
            - `[]`: Disallow all deserialization (will raise on any object).
        secrets_map: A map of secrets to load.

            If a secret is not found in the map, it will be loaded from the environment
            if `secrets_from_env` is `True`.
        valid_namespaces: Additional namespaces (modules) to allow during
            deserialization, beyond the default trusted namespaces.
        secrets_from_env: Whether to load secrets from the environment.
        additional_import_mappings: A dictionary of additional namespace mappings.

            You can use this to override default mappings or add new mappings.

            When `allowed_objects` is `None` (using defaults), paths from these
            mappings are also added to the allowed class paths.
        ignore_unserializable_fields: Whether to ignore unserializable fields.
        init_validator: Optional callable to validate kwargs before instantiation.

            If provided, this function is called with `(class_path, kwargs)` where
            `class_path` is the class path tuple and `kwargs` is the kwargs dict.
            The validator should raise an exception if the object should not be
            deserialized, otherwise return `None`.

            Defaults to `default_init_validator` which blocks jinja2 templates.

    Returns:
        Revived LangChain objects.

    Raises:
        ValueError: If an object's class path is not in the `allowed_objects` allowlist.
    """
    # Parse JSON and delegate to load() for proper escape handling
    raw_obj = json.loads(text)
    return load(
        raw_obj,
        allowed_objects=allowed_objects,
        secrets_map=secrets_map,
        valid_namespaces=valid_namespaces,
        secrets_from_env=secrets_from_env,
        additional_import_mappings=additional_import_mappings,
        ignore_unserializable_fields=ignore_unserializable_fields,
        init_validator=init_validator,
    )

Subdomains

Calls

Frequently Asked Questions

What does loads() do?
loads() is a function in the langchain codebase, defined in libs/core/langchain_core/load/load.py.
Where is loads() defined?
loads() is defined in libs/core/langchain_core/load/load.py at line 474.
What does loads() call?
loads() calls 1 function(s): load.
What calls loads()?
loads() is called by 1 function(s): default_init_validator.

Analyze Your Own Codebase

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

Try Supermodel Free