langchain-ai/langchain · critical · ValueError

Invalid namespace: {value}

Error message

Invalid namespace: {value}

What it means

Namespace validation error from the Reviver: the first element of the serialized object's id (its namespace, e.g. ['langchain_core', 'prompts', ...]) is not in valid_namespaces, or the namespace is exactly ['langchain'] (the legacy root, which is not importable). This guards against importing modules from unapproved top-level namespaces.

Source

Thrown at libs/core/langchain_core/load/load.py:526

                and mapping_key not in self.allowed_class_paths
            ):
                msg = (
                    f"Deserialization of {mapping_key!r} is not allowed. "
                    "The default (allowed_objects='core') only permits core "
                    "langchain-core classes. To allow trusted partner integrations, "
                    "use allowed_objects='all'. Alternatively, pass an explicit list "
                    "of allowed classes via allowed_objects=[...]. "
                    "See langchain_core.load.mapping for the full allowlist."
                )
                raise ValueError(msg)

            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:

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. If you trust the payload's namespace, pass it through: loads(text, valid_namespaces=['mycompany', 'langchain_core'])
  2. Re-serialize the object in the current version so its lc_id matches an importable namespace
  3. For custom classes, override lc_id()/lc_namespace to align with an approved import path

Example fix

# before
obj = loads(payload)  # id starts with 'mycompany'
# after
obj = loads(payload, valid_namespaces=['langchain_core', 'langchain', 'mycompany'])
Defensive patterns

Strategy: validation

Validate before calling

import json
payload = json.loads(text)
roots = {node['id'][0] for node in iter_constructor_nodes(payload)}
allowed_roots = {'langchain_core'}
if not roots <= allowed_roots:
    # pass valid_namespaces=roots | allowed_roots, or reject
    ...

Try / catch

try:
    obj = loads(text)
except ValueError as e:
    if 'Invalid namespace' in str(e) and payload_is_trusted:
        obj = loads(text, valid_namespaces=['langchain_core', *extra_roots])
    else:
        raise

Prevention

When it happens

Trigger: loads of a payload whose id starts with an unexpected root, e.g. ['mycompany', 'tools', 'CustomTool'], when valid_namespaces defaults to {'langchain_core', ...}; legacy payloads with id ['langchain', 'chain', 'LLMChain']. Distinct from error 117, which fires later on the resolved import path.

Common situations: Loading serialized objects produced by custom subclasses that use a private namespace; very old serialized chains from the pre-langchain-core 'langchain' namespace era; interop with payloads from forks that renamed top-level packages.

Related errors


AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14). Data as JSON: /api/errors/26e33085e952b62e. Report an issue: GitHub.