langchain-ai/langchain · error · ValueError
Trying to deserialize something that cannot be deserialized
Error message
Trying to deserialize something that cannot be deserialized in current version of langchain-core: {mapping_key}. What it means
Raised when a serialized object's namespace root appears in DISALLOW_LOAD_FROM_PATH: namespaces (like the legacy 'langchain' root) whose modules cannot be imported by path in current langchain-core. Even if the namespace passes validation, these legacy roots deliberately refuse path-based imports, so deserialization fails.
Source
Thrown at libs/core/langchain_core/load/load.py:538
if (
namespace[0] not in self.valid_namespaces
# The root namespace ["langchain"] is not a valid identifier.
or namespace == ["langchain"]
):
msg = f"Invalid namespace: {value}"
raise ValueError(msg)
# Determine explicit import path
if mapping_key in self.import_mappings:
import_path = self.import_mappings[mapping_key]
# Split into module and name
import_dir, name = import_path[:-1], import_path[-1]
elif namespace[0] in DISALLOW_LOAD_FROM_PATH:
msg = (
"Trying to deserialize something that cannot "
"be deserialized in current version of langchain-core: "
f"{mapping_key}."
)
raise ValueError(msg)
else:
# Otherwise, treat namespace as path.
import_dir = namespace
# Validate import path is in trusted namespaces before importing
if import_dir[0] not in self.valid_namespaces:
msg = f"Invalid namespace: {value}"
raise ValueError(msg)
# We don't need to recurse on kwargs
# as json.loads will do that for us.
kwargs = value.get("kwargs", {})
# Run the init_validator (e.g., jinja2 blocking) before importing
# to fail fast on security violations.
if self.init_validator is not None:
self.init_validator(mapping_key, kwargs)
View on GitHub (pinned to e32fa9a52e)
Solutions
- Rebuild the object in the current version instead of loading the legacy payload
- Provide an import mapping redirect: loads(text, import_mappings={('langchain','chains','LLMChain'): ('langchain_classic','chains','llm','LLMChain')})
- Check langchain_core.load.mapping for existing compat mappings before adding your own
Example fix
# before
obj = loads(legacy_payload)
# after
obj = loads(legacy_payload, import_mappings={('langchain','chains','LLMChain'): ('langchain_classic','chains','llm','LLMChain')}) Defensive patterns
Strategy: fallback
Validate before calling
from langchain_core.load.load import DISALLOW_LOAD_FROM_PATH
import json
payload = json.loads(text)
banned = {n['id'][0] for n in iter_constructor_nodes(payload)} & set(DISALLOW_LOAD_FROM_PATH)
if banned:
raise ValueError(f'legacy roots {banned} need import_mappings redirects') Try / catch
try:
obj = loads(text)
except ValueError as e:
if 'cannot be deserialized' in str(e):
obj = loads(text, import_mappings=LEGACY_REDIRECTS)
else:
raise Prevention
- Rebuild legacy chains from code instead of loading old JSON where possible
- Maintain a versioned import_mappings table for legacy class paths you must support
- Migrate archives once and re-dump with the current version
When it happens
Trigger: Loading old payloads with ids like ['langchain', 'chains', 'LLMChain'] where no import_mapping entry covers the class — the fallback import_dir = namespace path is disallowed. Typically seen with pre-0.1 serialized chains not present in the compat import_mappings.
Common situations: Restoring chains serialized under langchain <=0.0.x; loading exports from old LangSmith runs; class was removed or renamed so no mapping redirects it.
Related errors
- Invalid namespace: {value}
- Tracing using LangChainTracerV1 is no longer supported. Plea
- Jinja2 templates are not allowed during deserialization for
- allowed_objects must contain Serializable subclasses.
- Trying to load an object that doesn't implement serializatio
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/eae6a72cfbdf70b9.
Report an issue: GitHub.