{"record":{"id":"cdf9f2a87711f8f5","repo":"keras-team/keras","slug":"the-sparse-attribute-of-kerastensor-is-immutable","errorCode":null,"errorMessage":"The `sparse` attribute of KerasTensor is immutable. One should create a new instance of KerasTensor for this.","messagePattern":"The `sparse` attribute of KerasTensor is immutable\\. One should create a new instance of KerasTensor for this\\.","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"keras/src/backend/common/keras_tensor.py","lineNumber":97,"sourceCode":"\n    @property\n    def dtype(self):\n        return self._dtype\n\n    @dtype.setter\n    def dtype(self, value):\n        raise AttributeError(\n            \"The `dtype` attribute of KerasTensor is immutable. One should \"\n            \"create a new instance of KerasTensor for this.\"\n        )\n\n    @property\n    def sparse(self):\n        return self._sparse\n\n    @sparse.setter\n    def sparse(self, value):\n        raise AttributeError(\n            \"The `sparse` attribute of KerasTensor is immutable. One should \"\n            \"create a new instance of KerasTensor for this.\"\n        )\n\n    @property\n    def ragged_rank(self):\n        return self._ragged_rank\n\n    @ragged_rank.setter\n    def ragged_rank(self, value):\n        raise AttributeError(\n            \"The `ragged_rank` attribute of KerasTensor is immutable. One \"\n            \"should create a new instance of KerasTensor for this.\"\n        )\n\n    @property\n    def row_splits_dtype(self):\n        return self._row_splits_dtype","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/common/keras_tensor.py#L79-L115","documentation":"Raised by the sparse property setter of KerasTensor when code assigns to .sparse. KerasTensor is an immutable symbolic node in a Keras Functional graph; its sparsity flag is fixed at construction, so the setter is a deliberate guard against in-place mutation.","triggerScenarios":"Thrown at keras/src/backend/common/keras_tensor.py:97 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Construct a new KerasTensor with the desired sparse value instead of assigning to the attribute","Apply a sparse-producing op (e.g. from keras.ops) that returns a new tensor with the wanted layout","If a layer output needs different sparsity, change the layer/op that produced the tensor"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}