keras-team/keras · error · AttributeError
The `shape` attribute of KerasTensor is immutable. One…
Error message
The `shape` attribute of KerasTensor is immutable. One should create a new instance of KerasTensor for this.
What it means
Raised by the shape property setter of KerasTensor: unlike tf.Tensor-like objects, KerasTensor stores its inferred symbolic shape immutably at construction, so assigning tensor.shape = ... is not supported and immediately raises AttributeError.
Solutions
- Construct a new KerasTensor with the desired shape instead of mutating `shape`.
- To change dimensions, apply an op (reshape, expand_dims) which returns a new symbolic tensor.
- Set shape at construction time: KerasTensor((None, 128)).
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/backend/common/keras_tensor.py:75 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/94fd75a46fd323cd.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/common/keras_tensor.py:75
)
self._ragged_rank = (
int(ragged_rank) if ragged_rank is not None else None
)
self._row_splits_dtype = (
backend.standardize_dtype(row_splits_dtype)
if row_splits_dtype is not None
else None
)
self.name = name or auto_name(self.__class__.__name__)
self.record_history = record_history
@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._sparseView on GitHub (pinned to 7a34a03db6)