Load LangChain objects from JSON strings or objects.
How it works
Each Serializable LangChain object has a unique identifier (its "class path"),
which is a list of strings representing the module path and class name. For example:
When deserializing, the class path is validated against supported namespaces.
Security model
The secretsFromEnv parameter controls whether secrets can be loaded from environment
variables:
false (default): Secrets must be provided in secretsMap. If a secret is not
found, null is returned instead of loading from environment variables.
true: If a secret is not found in secretsMap, it will be loaded from
environment variables. Use this only in trusted environments.
Injection protection (escape-based)
During serialization, plain objects that contain an 'lc' key are escaped by wrapping
them: {"__lc_escaped__": {...}}. During deserialization, escaped objects are unwrapped
and returned as plain objects, NOT instantiated as LC objects.
This is an allowlist approach: only objects explicitly produced by
Serializable.toJSON() (which are NOT escaped) are treated as LC objects;
everything else is user data.
Load LangChain objects from JSON strings or objects.
How it works
Each
SerializableLangChain object has a unique identifier (its "class path"), which is a list of strings representing the module path and class name. For example:AIMessage->["langchain_core", "messages", "ai", "AIMessage"]ChatPromptTemplate->["langchain_core", "prompts", "chat", "ChatPromptTemplate"]When deserializing, the class path is validated against supported namespaces.
Security model
The
secretsFromEnvparameter controls whether secrets can be loaded from environment variables:false(default): Secrets must be provided insecretsMap. If a secret is not found,nullis returned instead of loading from environment variables.true: If a secret is not found insecretsMap, it will be loaded from environment variables. Use this only in trusted environments.Injection protection (escape-based)
During serialization, plain objects that contain an
'lc'key are escaped by wrapping them:{"__lc_escaped__": {...}}. During deserialization, escaped objects are unwrapped and returned as plain objects, NOT instantiated as LC objects.This is an allowlist approach: only objects explicitly produced by
Serializable.toJSON()(which are NOT escaped) are treated as LC objects; everything else is user data.