{"record":{"id":"05fac86b6be40a6b","repo":"keras-team/keras","slug":"unable-to-serialize-obj-to-json-because-the-typ","errorCode":null,"errorMessage":"Unable to serialize {obj} to JSON, because the TypeSpec class {type(obj)} has not been registered.","messagePattern":"Unable to serialize (.+?) to JSON, because the TypeSpec class (.+?) has not been registered\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/saving/json_utils.py","lineNumber":197,"sourceCode":"\n    if obj is Ellipsis:\n        return {\"class_name\": \"__ellipsis__\"}\n\n    # if isinstance(obj, wrapt.ObjectProxy):\n    #     return obj.__wrapped__\n\n    if tf.available and isinstance(obj, tf.TypeSpec):\n        from tensorflow.python.framework import type_spec_registry\n\n        try:\n            type_spec_name = type_spec_registry.get_name(type(obj))\n            return {\n                \"class_name\": \"TypeSpec\",\n                \"type_spec\": type_spec_name,\n                \"serialized\": obj._serialize(),\n            }\n        except ValueError:\n            raise ValueError(\n                f\"Unable to serialize {obj} to JSON, because the TypeSpec \"\n                f\"class {type(obj)} has not been registered.\"\n            )\n    if tf.available and isinstance(obj, tf.__internal__.CompositeTensor):\n        spec = tf.type_spec_from_value(obj)\n        tensors = []\n        for tensor in tf.nest.flatten(obj, expand_composites=True):\n            tensors.append((tensor.dtype.name, tensor.numpy().tolist()))\n        return {\n            \"class_name\": \"CompositeTensor\",\n            \"spec\": get_json_type(spec),\n            \"tensors\": tensors,\n        }\n\n    if isinstance(obj, enum.Enum):\n        return obj.value\n\n    if isinstance(obj, bytes):","sourceCodeStart":179,"sourceCodeEnd":215,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/saving/json_utils.py#L179-L215","documentation":"When saving a Keras model to JSON, get_json_type serializes TensorFlow TypeSpec objects (e.g. TensorSpec in an input signature) by looking up a registered serialization name. If the concrete TypeSpec class has no registration, the lookup raises ValueError and this wrapper error names the object and type that failed.","triggerScenarios":"model.to_json() (or any legacy JSON save path) on a model whose config contains an unregistered tf.TypeSpec subclass, e.g. custom ragged/sparse tensor specs or specs from a TF version whose registry does not match Keras.","commonSituations":"Models with ragged or sparse inputs after a TensorFlow/Keras version mismatch; custom input types introduced by an upgrade.","solutions":["Save with the native format: model.save('model.keras') instead of to_json()","Align keras and tensorflow versions so the spec class is registered","For custom TypeSpecs, register them with TF's TypeSpec serialization registry"],"exampleFix":"# before\njson_config = model.to_json()\n# after\nmodel.save('model.keras')  # native format, no JSON type registry","handlingStrategy":"fallback","validationCode":"spec = tf.type_spec_from_value(x)\nfrom tensorflow.python.saved_model import nested_structure_coder\nassert spec.__class__ in nested_structure_coder._TYPE_SPEC_TO_CODEC  # crude registry check","typeGuard":null,"tryCatchPattern":"try:\n    model.to_json()\nexcept ValueError:\n    model.save('model.keras')  # fallback to native format","preventionTips":["Prefer the .keras format for models with ragged/sparse inputs","Pin compatible keras/tensorflow versions"],"tags":["keras","serialization","json","tensorflow","typespec"],"backgroundTag":"json-serialization-failed","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}