Home / Function/ load() — langchain Function Reference

load() — langchain Function Reference

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

Entity Profile

Dependency Diagram

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

Relationship Graph

Source Code

libs/core/langchain_core/load/load.py lines 556–671

def load(
    obj: Any,
    *,
    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 object.

    Use this if you already have a parsed JSON object, eg. from `json.load` or
    `orjson.loads`.

    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:
        obj: The object 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.

    Example:
        ```python
        from langchain_core.load import load, dumpd
        from langchain_core.messages import AIMessage

        msg = AIMessage(content="Hello")
        data = dumpd(msg)

        # Deserialize using default allowlist
        loaded = load(data)

        # Or with explicit allowlist
        loaded = load(data, allowed_objects=[AIMessage])

Subdomains

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free