keras-team/keras · error · ValueError
All cells must have a `state_size` attribute. Received cell
Error message
All cells must have a `state_size` attribute. Received cell without a `state_size`: {cell} What it means
StackedRNNCells requires every cell to expose a `state_size` attribute (checked via `dir(cell)` in `__init__`), because the RNN layer needs to know how many state tensors and their sizes to initialize and pass between timesteps. A cell with `call` but no `state_size` cannot be stepped by the RNN machinery, so construction aborts.
Source
Thrown at keras/src/layers/rnn/stacked_rnn_cells.py:43
rnn_cells = [keras.layers.LSTMCell(128) for _ in range(2)]
stacked_lstm = keras.layers.StackedRNNCells(rnn_cells)
lstm_layer = keras.layers.RNN(stacked_lstm)
result = lstm_layer(x)
```
"""
def __init__(self, cells, **kwargs):
super().__init__(**kwargs)
for cell in cells:
if "call" not in dir(cell):
raise ValueError(
"All cells must have a `call` method. "
f"Received cell without a `call` method: {cell}"
)
if "state_size" not in dir(cell):
raise ValueError(
"All cells must have a `state_size` attribute. "
f"Received cell without a `state_size`: {cell}"
)
self.cells = cells
@property
def state_size(self):
return [c.state_size for c in self.cells]
@property
def output_size(self):
if getattr(self.cells[-1], "output_size", None) is not None:
return self.cells[-1].output_size
elif isinstance(self.cells[-1].state_size, (list, tuple)):
return self.cells[-1].state_size[0]
else:
return self.cells[-1].state_size
View on GitHub (pinned to 7a34a03db6)
Solutions
- Add state_size to the custom cell, e.g. self.state_size = units for a single-state cell (or a tuple/list for multiple states)
- If the object is not meant to be a cell, replace it with a proper Keras cell (SimpleRNNCell, LSTMCell, GRUCell)
- For multi-state cells, make sure state_size lengths match what call() returns as new_states
Example fix
# before
class MyCell:
def __init__(self, units):
self.units = units
def call(self, inputs, states):
...
# after
class MyCell:
def __init__(self, units):
self.units = units
self.state_size = units
def call(self, inputs, states):
... Defensive patterns
Strategy: type-guard
Validate before calling
assert all(hasattr(c, 'state_size') for c in cells), 'cells missing state_size' stacked = keras.layers.StackedRNNCells(cells=cells)
Type guard
def has_state_size(cell) -> bool:
return hasattr(cell, 'state_size') and cell.state_size is not None Prevention
- Add state_size whenever implementing a custom RNN cell
- If state_size is a tuple, its length must equal the number of returned new_states
When it happens
Trigger: Passing a custom object that implements call() but not state_size to keras.layers.StackedRNNCells(cells=[...]); porting a PyTorch-style RNN module or generic Layer into a Keras cell list.
Common situations: Writing custom RNN cells and forgetting state_size; migrating from tf.keras v1 or other frameworks whose cell interfaces differ; refactoring a Layer into a cell without adding the RNN cell contract.
Related errors
- Received an invalid value for argument `units`, expected a p
- All cells must have a `call` method. Received cell without a
- Unknown activation function '{activation}' cannot be seriali
- Could not interpret activation function identifier: {identif
- ConvNeXt does not support the `channels_first` image data fo
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/c6a4875afe10e9f3.
Report an issue: GitHub.