keras-team/keras · error · ValueError

The RNN cell should have a `state_size` attribute (single…

Error message

The RNN cell should have a `state_size` attribute (single integer or list of integers, one integer per RNN state). Received: cell={cell}

What it means

Raised by RNN.__init__ when the `cell` object lacks a `state_size` attribute. state_size (a single integer or list of integers, one per state tensor) is how the RNN determines how many states to carry across timesteps; without it the cell cannot be driven.

Solutions

  1. Use a built-in cell (e.g. LSTMCell) which defines state_size
  2. For custom cells, add state_size as an attribute or property, e.g. self.state_size = units or [units, units]
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/layers/rnn/rnn.py:193 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/c824573588c2d410. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/layers/rnn/rnn.py:193

        self,
        cell,
        return_sequences=False,
        return_state=False,
        go_backwards=False,
        stateful=False,
        unroll=False,
        zero_output_for_mask=False,
        **kwargs,
    ):
        if isinstance(cell, (list, tuple)):
            cell = StackedRNNCells(cell)
        if "call" not in dir(cell):
            raise ValueError(
                "Argument `cell` should have a `call` method. "
                f"Received: cell={cell}"
            )
        if "state_size" not in dir(cell):
            raise ValueError(
                "The RNN cell should have a `state_size` attribute "
                "(single integer or list of integers, "
                "one integer per RNN state). "
                f"Received: cell={cell}"
            )
        super().__init__(**kwargs)

        # If True, the output for masked timestep will be zeros, whereas in the
        # False case, output from previous timestep is returned for masked
        # timestep.
        self.zero_output_for_mask = zero_output_for_mask
        self.cell = cell
        self.return_sequences = return_sequences
        self.return_state = return_state
        self.go_backwards = go_backwards
        self.stateful = stateful
        self.unroll = unroll

View on GitHub (pinned to 7a34a03db6)