keras-team/keras · error · AttributeError
The `sparse` attribute of KerasTensor is immutable. One…
Error message
The `sparse` attribute of KerasTensor is immutable. One should create a new instance of KerasTensor for this.
What it means
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.
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
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/backend/common/keras_tensor.py:97 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/cdf9f2a87711f8f5.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/common/keras_tensor.py:97
@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_rank
@ragged_rank.setter
def ragged_rank(self, value):
raise AttributeError(
"The `ragged_rank` attribute of KerasTensor is immutable. One "
"should create a new instance of KerasTensor for this."
)
@property
def row_splits_dtype(self):
return self._row_splits_dtypeView on GitHub (pinned to 7a34a03db6)