{"record":{"id":"8cf0754188c2a157","repo":"keras-team/keras","slug":"the-dtype-attribute-of-kerastensor-is-immutable","errorCode":null,"errorMessage":"The `dtype` attribute of KerasTensor is immutable. One should create a new instance of KerasTensor for this.","messagePattern":"The `dtype` 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":86,"sourceCode":"\n    @property\n    def shape(self):\n        return self._shape\n\n    @shape.setter\n    def shape(self, value):\n        raise AttributeError(\n            \"The `shape` attribute of KerasTensor is immutable. One should \"\n            \"create a new instance of KerasTensor for this.\"\n        )\n\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","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/backend/common/keras_tensor.py#L68-L104","documentation":"Raised by the dtype property setter of KerasTensor: the dtype of a symbolic KerasTensor is fixed when the tensor is created (it comes from the producing op or constructor), so post-hoc assignment like tensor.dtype = 'float16' raises AttributeError.","triggerScenarios":"Thrown at keras/src/backend/common/keras_tensor.py:86 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Create a new KerasTensor with the target dtype: KerasTensor(shape, dtype='float16').","Use keras.ops.cast(tensor, 'float16') to obtain a new tensor with the desired dtype.","Set the dtype on the producing layer/op rather than on the output 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"}