{"record":{"id":"cd53717c80705893","repo":"langchain-ai/langchain","slug":"allowed-objects-must-contain-serializable-subclass","errorCode":null,"errorMessage":"allowed_objects must contain Serializable subclasses.","messagePattern":"allowed_objects must contain Serializable subclasses\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/load/load.py","lineNumber":325,"sourceCode":"        # Allow a specific class\n        _compute_allowed_class_paths([MyPrompt], {}) ->\n            {(\"langchain_core\", \"prompts\", \"MyPrompt\")}\n\n        # Include legacy paths that map to the same class\n        import_mappings = {(\"old\", \"Prompt\"): (\"langchain_core\", \"prompts\", \"MyPrompt\")}\n        _compute_allowed_class_paths([MyPrompt], import_mappings) ->\n            {(\"langchain_core\", \"prompts\", \"MyPrompt\"), (\"old\", \"Prompt\")}\n        ```\n    \"\"\"\n    allowed_objects_list = list(allowed_objects)\n\n    allowed_class_paths: set[tuple[str, ...]] = set()\n    for allowed_obj in allowed_objects_list:\n        if not isinstance(allowed_obj, type) or not issubclass(\n            allowed_obj, Serializable\n        ):\n            msg = \"allowed_objects must contain Serializable subclasses.\"  # type: ignore[unreachable]\n            raise TypeError(msg)\n\n        class_path = tuple(allowed_obj.lc_id())\n        allowed_class_paths.add(class_path)\n        # Add legacy paths that map to the same class.\n        for mapping_key, mapping_value in import_mappings.items():\n            if tuple(mapping_value) == class_path:\n                allowed_class_paths.add(mapping_key)\n    return allowed_class_paths\n\n\nclass Reviver:\n    \"\"\"Reviver for JSON objects.\n\n    Used as the `object_hook` for `json.loads` to reconstruct LangChain objects from\n    their serialized JSON representation.\n\n    Only classes in the allowlist can be instantiated.\n    \"\"\"","sourceCodeStart":307,"sourceCodeEnd":343,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/load/load.py#L307-L343","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass classes, not instances: allowed_objects=[FakeMessagesList] (no parentheses)","Verify each entry is a subclass of langchain_core.load.serializable.Serializable","For module-path strings, import the class first and pass the class object"],"exampleFix":"# before\nloads(text, allowed_objects=[my_prompt_instance])\n# after\nloads(text, allowed_objects=[type(my_prompt_instance)])","handlingStrategy":"type-guard","validationCode":"from langchain_core.load.serializable import Serializable\nassert all(isinstance(o, type) and issubclass(o, Serializable) for o in allowed_objects), 'allowed_objects must be Serializable classes'","typeGuard":"from langchain_core.load.serializable import Serializable\ndef is_valid_allowed_objects(objs: list) -> bool:\n    return all(isinstance(o, type) and issubclass(o, Serializable) for o in objs)","tryCatchPattern":"try:\n    loads(text, allowed_objects=objs)\nexcept TypeError as e:\n    if 'Serializable subclasses' in str(e):\n        loads(text, allowed_objects=[o if isinstance(o, type) else type(o) for o in objs])\n    else:\n        raise","preventionTips":["Pass classes (no parentheses), never instances, in allowed_objects","Centralize your allowlist as module-level class constants so mistakes surface at import time"],"tags":["deserialization","allowlist","type-error","validation"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}