{"record":{"id":"9e679c5083abb843","repo":"keras-team/keras","slug":"if-an-rnn-is-stateful-the-batch-size-of-the-input","errorCode":null,"errorMessage":"If an RNN is stateful, the batch size of the input sequences must be the same as the batch size of the initial state. \n- Expected batch size: {self._expected_batch_size}\n- Received batch size: {actual_batch_size}","messagePattern":"If an RNN is stateful, the batch size of the input sequences must be the same as the batch size of the initial state\\. \n- Expected batch size: (.+?)\n- Received batch size: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/rnn/rnn.py","lineNumber":396,"sourceCode":"                \"the time dimension by passing a `shape` \"\n                \"or `batch_shape` argument to your `Input()`.\"\n            )\n\n        if initial_state is None:\n            if self.stateful:\n                initial_state = self.states\n            else:\n                initial_state = self.get_initial_state(\n                    batch_size=ops.shape(sequences)[0]\n                )\n        if self.stateful:\n            actual_batch_size = sequences.shape[0]\n            if (\n                self._expected_batch_size is not None\n                and actual_batch_size is not None\n                and actual_batch_size != self._expected_batch_size\n            ):\n                raise ValueError(\n                    f\"If an RNN is stateful, the batch size of the \"\n                    f\"input sequences must be the same as the batch \"\n                    f\"size of the initial state. \\n\"\n                    f\"- Expected batch size: {self._expected_batch_size}\\n\"\n                    f\"- Received batch size: {actual_batch_size}\"\n                )\n\n        # RNN expect the states in a list, even if single state.\n        if not tree.is_nested(initial_state):\n            initial_state = [initial_state]\n        initial_state = list(initial_state)\n\n        # Cast states to compute dtype.\n        # Note that states may be deeply nested\n        # (e.g. in the stacked cells case).\n        initial_state = tree.map_structure(\n            lambda x: backend.convert_to_tensor(\n                x, dtype=self.cell.compute_dtype","sourceCodeStart":378,"sourceCodeEnd":414,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/rnn/rnn.py#L378-L414","documentation":"Error \"If an RNN is stateful, the batch size of the input sequences must be the same as the batch size of the initial state. \n- Expected batch size: {self._expected_batch_size}\n- Received batch size: {actual_batch_size}\" thrown in keras-team/keras.","triggerScenarios":"Thrown at keras/src/layers/rnn/rnn.py:396 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}