keras-team/keras · error · ValueError

state_size must be specified as property on the RNN cell.

Error message

state_size must be specified as property on the RNN cell.

What it means

Raised during RNN.__init__/state setup when getattr(cell, 'state_size', None) returns None — the cell exposes a state_size attribute but its value is None. The RNN cannot infer state shapes from a None state_size, so it aborts with this guard message.

Solutions

  1. Make the cell's state_size an int or list/tuple of ints, e.g. self.state_size = self.units
  2. Check that a property-based state_size doesn't return None before the cell is fully initialized
Defensive patterns

Strategy: validation

When it happens

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

Appendix: source

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

        # 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

        self.supports_masking = False
        self.input_spec = None
        self.states = None
        self._expected_batch_size = None

        state_size = getattr(self.cell, "state_size", None)
        if state_size is None:
            raise ValueError(
                "state_size must be specified as property on the RNN cell."
            )
        if not isinstance(state_size, (list, tuple, int)):
            raise ValueError(
                "state_size must be an integer, or a list/tuple of integers "
                "(one for each state tensor)."
            )
        if isinstance(state_size, int):
            self.state_size = [state_size]
            self.single_state = True
        else:
            self.state_size = list(state_size)
            self.single_state = False

    def compute_output_shape(self, sequences_shape, initial_state_shape=None):
        batch_size = sequences_shape[0]
        length = sequences_shape[1]
        states_shape = []

View on GitHub (pinned to 7a34a03db6)