{"record":{"id":"7dc34e0c83eace6e","repo":"keras-team/keras","slug":"sequential-model-self-name-has-already-been-co-7dc34e","errorCode":null,"errorMessage":"Sequential model '{self.name}' has already been configured to use input shape {self._layers[0].batch_shape}. You cannot build it with input_shape {input_shape}","messagePattern":"Sequential model '(.+?)' has already been configured to use input shape (.+?)\\. You cannot build it with input_shape (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/models/sequential.py","lineNumber":178,"sourceCode":"\n    def _obj_type(self):\n        return \"Sequential\"\n\n    def build(self, input_shape=None):\n        try:\n            input_shape = standardize_shape(input_shape)\n        except:\n            # Do not attempt to build if the model does not have a single\n            # input tensor.\n            return\n        if not self._layers:\n            raise ValueError(\n                f\"Sequential model {self.name} cannot be built because it has \"\n                \"no layers. Call `model.add(layer)`.\"\n            )\n        if isinstance(self._layers[0], InputLayer):\n            if self._layers[0].batch_shape != input_shape:\n                raise ValueError(\n                    f\"Sequential model '{self.name}' has already been \"\n                    \"configured to use input shape \"\n                    f\"{self._layers[0].batch_shape}. You cannot build it \"\n                    f\"with input_shape {input_shape}\"\n                )\n        else:\n            dtype = self._layers[0].compute_dtype\n            self._layers = [\n                InputLayer(batch_shape=input_shape, dtype=dtype)\n            ] + self._layers\n\n        # Build functional model\n        inputs = self._layers[0].output\n        x = inputs\n        for layer in self._layers[1:]:\n            try:\n                x = layer(x)\n            except NotImplementedError:","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/models/sequential.py#L160-L196","documentation":"Sequential.build(input_shape) refuses to build when the model already starts with an InputLayer whose batch_shape differs from the requested shape. The InputLayer fixes the input shape at construction time, so a conflicting later build is rejected instead of silently rebuilding.","triggerScenarios":"model = keras.Sequential([keras.Input((32,)), ...]); model.build((None, 64,))","commonSituations":"Loading checkpoints, writing tutorials, or migrating from Keras 2 where build behaved differently","solutions":["Pass the exact same shape as the InputLayer's batch_shape, including the batch dimension (e.g. (None, 28, 28))","Recreate the model without the InputLayer and rely on build(input_shape) alone","Recreate the model with keras.Input(new_shape) as the first layer"],"exampleFix":"# before\nmodel = keras.Sequential([keras.Input((32,)), keras.layers.Dense(10)])\nmodel.build((None, 28, 28))  # ValueError\n\n# after\nmodel = keras.Sequential([keras.Input((28, 28)), keras.layers.Dense(10)])\n# or: model = keras.Sequential([Dense(10)]); model.build((None, 28, 28))","handlingStrategy":"validation","validationCode":"first = model._layers[0] if model._layers else None\nif isinstance(first, keras.layers.InputLayer):\n    assert first.batch_shape == input_shape","typeGuard":null,"tryCatchPattern":"try:\n    model.build(input_shape)\nexcept ValueError as e:\n    if 'already been configured' in str(e):\n        shape = model._layers[0].batch_shape","preventionTips":["Create the Sequential with an explicit keras.Input(shape) as the first layer","Call build() with the same shape as the InputLayer","Check model._functional before probing input_shape/output_shape"],"tags":["keras","sequential","input-shape","build"],"backgroundTag":"input-shape-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}