{"record":{"id":"4499fa6d625221b8","repo":"keras-team/keras","slug":"targets-not-json-serializable-targets","errorCode":null,"errorMessage":"Targets not JSON Serializable: {targets}","messagePattern":"Targets not JSON Serializable: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"keras/src/legacy/preprocessing/sequence.py","lineNumber":150,"sourceCode":"\n        Returns:\n            A Python dictionary with the TimeseriesGenerator configuration.\n        \"\"\"\n        data = self.data\n        if type(self.data).__module__ == np.__name__:\n            data = self.data.tolist()\n        try:\n            json_data = json.dumps(data)\n        except TypeError as e:\n            raise TypeError(f\"Data not JSON Serializable: {data}\") from e\n\n        targets = self.targets\n        if type(self.targets).__module__ == np.__name__:\n            targets = self.targets.tolist()\n        try:\n            json_targets = json.dumps(targets)\n        except TypeError as e:\n            raise TypeError(f\"Targets not JSON Serializable: {targets}\") from e\n\n        config = super().get_config()\n        config.update(\n            {\n                \"data\": json_data,\n                \"targets\": json_targets,\n                \"length\": self.length,\n                \"sampling_rate\": self.sampling_rate,\n                \"stride\": self.stride,\n                \"start_index\": self.start_index,\n                \"end_index\": self.end_index,\n                \"shuffle\": self.shuffle,\n                \"reverse\": self.reverse,\n                \"batch_size\": self.batch_size,\n            }\n        )\n        return config\n","sourceCodeStart":132,"sourceCodeEnd":168,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/legacy/preprocessing/sequence.py#L132-L168","documentation":"The targets counterpart of the data check: TimeseriesGenerator.get_config json.dumps the targets (after tolist() for numpy arrays), and any non-JSON-serializable target value (datetimes, Decimals, custom classes) triggers this TypeError.","triggerScenarios":"get_config() on a generator whose targets contain datetime or Decimal values or are stored as object-dtype arrays.","commonSituations":"Forecasting pipelines whose labels are timestamps or currency Decimals; save paths that capture generator configs.","solutions":["Make targets numeric (float/int) before building the generator","Keep a separate mapping for original values and pass integer codes as targets","Prefer tf.keras.utils.timeseries_dataset_from_array in new code"],"exampleFix":"# before\ngen = TimeseriesGenerator(prices, dates, length=5)\n# after\ngen = TimeseriesGenerator(prices, dates.astype('int64'), length=5)","handlingStrategy":"validation","validationCode":"try:\n    json.dumps(np.asarray(targets).tolist())\nexcept TypeError:\n    targets = np.asarray(targets).astype('float64')","typeGuard":null,"tryCatchPattern":"try:\n    gen.get_config()\nexcept TypeError as e:\n    if 'Targets not JSON Serializable' not in str(e):\n        raise","preventionTips":["Keep targets numeric (float/int) exclusively"],"tags":["keras","serialization","json","timeseries"],"backgroundTag":"json-serialization-failed","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}