keras-team/keras · error · AttributeError
The `dtype` attribute of KerasTensor is immutable. One…
Error message
The `dtype` attribute of KerasTensor is immutable. One should create a new instance of KerasTensor for this.
What it means
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.
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.
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/backend/common/keras_tensor.py:86 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/8cf0754188c2a157.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/common/keras_tensor.py:86
@property
def shape(self):
return self._shape
@shape.setter
def shape(self, value):
raise AttributeError(
"The `shape` attribute of KerasTensor is immutable. One should "
"create a new instance of KerasTensor for this."
)
@property
def dtype(self):
return self._dtype
@dtype.setter
def dtype(self, value):
raise AttributeError(
"The `dtype` attribute of KerasTensor is immutable. One should "
"create a new instance of KerasTensor for this."
)
@property
def sparse(self):
return self._sparse
@sparse.setter
def sparse(self, value):
raise AttributeError(
"The `sparse` attribute of KerasTensor is immutable. One should "
"create a new instance of KerasTensor for this."
)
@property
def ragged_rank(self):
return self._ragged_rankView on GitHub (pinned to 7a34a03db6)