langchain-ai/langchain · error · TypeError

allowed_objects must contain Serializable subclasses.

Error message

allowed_objects must contain Serializable subclasses.

What it means

Raised by _compute_allowed_class_paths when the allowed_objects argument to loads contains something that is not a class or not a subclass of Serializable. The allowlist mechanism works on Serializable class identities (via lc_id()), so instances or unrelated types are rejected with a TypeError.

Source

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

        # Allow a specific class
        _compute_allowed_class_paths([MyPrompt], {}) ->
            {("langchain_core", "prompts", "MyPrompt")}

        # Include legacy paths that map to the same class
        import_mappings = {("old", "Prompt"): ("langchain_core", "prompts", "MyPrompt")}
        _compute_allowed_class_paths([MyPrompt], import_mappings) ->
            {("langchain_core", "prompts", "MyPrompt"), ("old", "Prompt")}
        ```
    """
    allowed_objects_list = list(allowed_objects)

    allowed_class_paths: set[tuple[str, ...]] = set()
    for allowed_obj in allowed_objects_list:
        if not isinstance(allowed_obj, type) or not issubclass(
            allowed_obj, Serializable
        ):
            msg = "allowed_objects must contain Serializable subclasses."  # type: ignore[unreachable]
            raise TypeError(msg)

        class_path = tuple(allowed_obj.lc_id())
        allowed_class_paths.add(class_path)
        # Add legacy paths that map to the same class.
        for mapping_key, mapping_value in import_mappings.items():
            if tuple(mapping_value) == class_path:
                allowed_class_paths.add(mapping_key)
    return allowed_class_paths


class Reviver:
    """Reviver for JSON objects.

    Used as the `object_hook` for `json.loads` to reconstruct LangChain objects from
    their serialized JSON representation.

    Only classes in the allowlist can be instantiated.
    """

View on GitHub (pinned to e32fa9a52e)

Solutions

  1. Pass classes, not instances: allowed_objects=[FakeMessagesList] (no parentheses)
  2. Verify each entry is a subclass of langchain_core.load.serializable.Serializable
  3. For module-path strings, import the class first and pass the class object

Example fix

# before
loads(text, allowed_objects=[my_prompt_instance])
# after
loads(text, allowed_objects=[type(my_prompt_instance)])
Defensive patterns

Strategy: type-guard

Validate before calling

from langchain_core.load.serializable import Serializable
assert all(isinstance(o, type) and issubclass(o, Serializable) for o in allowed_objects), 'allowed_objects must be Serializable classes'

Type guard

from langchain_core.load.serializable import Serializable
def is_valid_allowed_objects(objs: list) -> bool:
    return all(isinstance(o, type) and issubclass(o, Serializable) for o in objs)

Try / catch

try:
    loads(text, allowed_objects=objs)
except TypeError as e:
    if 'Serializable subclasses' in str(e):
        loads(text, allowed_objects=[o if isinstance(o, type) else type(o) for o in objs])
    else:
        raise

Prevention

When it happens

Trigger: loads(text, allowed_objects=[FakeMessagesList()]) — an instance instead of the class; allowed_objects=[SomePydanticModel] that is not Serializable; allowed_objects=['langchain_core.prompts'] as a string module path.

Common situations: Writing an allowlist for the (beta) restricted deserialization API and passing instances or names instead of classes; copying class references from older examples that used SERIALIZABLE_MAPPING values.

Related errors


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