Skip to content

Serialization

dumpd

dumpd(obj: Any) -> Any

Return a dict representation of an object.

Note

Plain dicts containing an 'lc' key are automatically escaped to prevent confusion with LC serialization format. The escape marker is removed during deserialization.

PARAMETER DESCRIPTION
obj

The object to dump.

TYPE: Any

RETURNS DESCRIPTION
Any

Dictionary that can be serialized to json using json.dumps.

dumps

dumps(obj: Any, *, pretty: bool = False, **kwargs: Any) -> str

Return a JSON string representation of an object.

Note

Plain dicts containing an 'lc' key are automatically escaped to prevent confusion with LC serialization format. The escape marker is removed during deserialization.

PARAMETER DESCRIPTION
obj

The object to dump.

TYPE: Any

pretty

Whether to pretty print the json.

If True, the json will be indented by either 2 spaces or the amount provided in the indent kwarg.

TYPE: bool DEFAULT: False

**kwargs

Additional arguments to pass to json.dumps

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
str

A JSON string representation of the object.

RAISES DESCRIPTION
ValueError

If default is passed as a kwarg.

load

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.

Beta feature

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

PARAMETER DESCRIPTION
obj

The object to load.

TYPE: Any

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).

TYPE: Iterable[AllowedObject] | Literal['all', 'core'] DEFAULT: 'core'

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.

TYPE: dict[str, str] | None DEFAULT: None

valid_namespaces

Additional namespaces (modules) to allow during deserialization, beyond the default trusted namespaces.

TYPE: list[str] | None DEFAULT: None

secrets_from_env

Whether to load secrets from the environment.

TYPE: bool DEFAULT: False

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.

TYPE: dict[tuple[str, ...], tuple[str, ...]] | None DEFAULT: None

ignore_unserializable_fields

Whether to ignore unserializable fields.

TYPE: bool DEFAULT: False

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.

TYPE: InitValidator | None DEFAULT: default_init_validator

RETURNS DESCRIPTION
Any

Revived LangChain objects.

RAISES DESCRIPTION
ValueError

If an object's class path is not in the allowed_objects allowlist.

Example
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])

# Or extend defaults with additional mappings
loaded = load(
    data,
    additional_import_mappings={
        ("my_pkg", "MyClass"): ("my_pkg", "module", "MyClass"),
    },
)

loads

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.

Beta feature

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

PARAMETER DESCRIPTION
text

The string to load.

TYPE: str

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).

TYPE: Iterable[AllowedObject] | Literal['all', 'core'] DEFAULT: 'core'

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.

TYPE: dict[str, str] | None DEFAULT: None

valid_namespaces

Additional namespaces (modules) to allow during deserialization, beyond the default trusted namespaces.

TYPE: list[str] | None DEFAULT: None

secrets_from_env

Whether to load secrets from the environment.

TYPE: bool DEFAULT: False

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.

TYPE: dict[tuple[str, ...], tuple[str, ...]] | None DEFAULT: None

ignore_unserializable_fields

Whether to ignore unserializable fields.

TYPE: bool DEFAULT: False

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.

TYPE: InitValidator | None DEFAULT: default_init_validator

RETURNS DESCRIPTION
Any

Revived LangChain objects.

RAISES DESCRIPTION
ValueError

If an object's class path is not in the allowed_objects allowlist.

Serializable

Bases: BaseModel, ABC

Serializable base class.

This class is used to serialize objects to JSON.

It relies on the following methods and properties:

  • is_lc_serializable: Is this class serializable?

    By design, even if a class inherits from Serializable, it is not serializable by default. This is to prevent accidental serialization of objects that should not be serialized. - get_lc_namespace: Get the namespace of the LangChain object.

    During deserialization, this namespace is used to identify the correct class to instantiate.

    Please see the Reviver class in langchain_core.load.load for more details. During deserialization an additional mapping is handle classes that have moved or been renamed across package versions.

  • lc_secrets: A map of constructor argument names to secret ids.

  • lc_attributes: List of additional attribute names that should be included as part of the serialized representation.
METHOD DESCRIPTION
__init__
is_lc_serializable

Is this class serializable?

get_lc_namespace

Get the namespace of the LangChain object.

lc_id

Return a unique identifier for this class for serialization purposes.

to_json

Serialize the object to JSON.

to_json_not_implemented

Serialize a "not implemented" object.

lc_secrets property

lc_secrets: dict[str, str]

A map of constructor argument names to secret ids.

For example, {"openai_api_key": "OPENAI_API_KEY"}

lc_attributes property

lc_attributes: dict

List of attribute names that should be included in the serialized kwargs.

These attributes must be accepted by the constructor.

Default is an empty dictionary.

__init__

__init__(*args: Any, **kwargs: Any) -> None

is_lc_serializable classmethod

is_lc_serializable() -> bool

Is this class serializable?

By design, even if a class inherits from Serializable, it is not serializable by default. This is to prevent accidental serialization of objects that should not be serialized.

RETURNS DESCRIPTION
bool

Whether the class is serializable. Default is False.

get_lc_namespace classmethod

get_lc_namespace() -> list[str]

Get the namespace of the LangChain object.

For example, if the class is langchain.llms.openai.OpenAI, then the namespace is ["langchain", "llms", "openai"]

RETURNS DESCRIPTION
list[str]

The namespace.

lc_id classmethod

lc_id() -> list[str]

Return a unique identifier for this class for serialization purposes.

The unique identifier is a list of strings that describes the path to the object.

For example, for the class langchain.llms.openai.OpenAI, the id is ["langchain", "llms", "openai", "OpenAI"].

to_json

to_json() -> SerializedConstructor | SerializedNotImplemented

Serialize the object to JSON.

RAISES DESCRIPTION
ValueError

If the class has deprecated attributes.

RETURNS DESCRIPTION
SerializedConstructor | SerializedNotImplemented

A JSON serializable object or a SerializedNotImplemented object.

to_json_not_implemented

to_json_not_implemented() -> SerializedNotImplemented

Serialize a "not implemented" object.

RETURNS DESCRIPTION
SerializedNotImplemented

SerializedNotImplemented.