keras-team/keras · error · ValueError
Sequential model '{self.name}' has already been configured t
Error message
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} What it means
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.
Source
Thrown at keras/src/models/sequential.py:178
def _obj_type(self):
return "Sequential"
def build(self, input_shape=None):
try:
input_shape = standardize_shape(input_shape)
except:
# Do not attempt to build if the model does not have a single
# input tensor.
return
if not self._layers:
raise ValueError(
f"Sequential model {self.name} cannot be built because it has "
"no layers. Call `model.add(layer)`."
)
if isinstance(self._layers[0], InputLayer):
if self._layers[0].batch_shape != input_shape:
raise ValueError(
f"Sequential model '{self.name}' has already been "
"configured to use input shape "
f"{self._layers[0].batch_shape}. You cannot build it "
f"with input_shape {input_shape}"
)
else:
dtype = self._layers[0].compute_dtype
self._layers = [
InputLayer(batch_shape=input_shape, dtype=dtype)
] + self._layers
# Build functional model
inputs = self._layers[0].output
x = inputs
for layer in self._layers[1:]:
try:
x = layer(x)
except NotImplementedError:View on GitHub (pinned to 7a34a03db6)
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
Example fix
# before model = keras.Sequential([keras.Input((32,)), keras.layers.Dense(10)]) model.build((None, 28, 28)) # ValueError # after model = keras.Sequential([keras.Input((28, 28)), keras.layers.Dense(10)]) # or: model = keras.Sequential([Dense(10)]); model.build((None, 28, 28))
Defensive patterns
Strategy: validation
Validate before calling
first = model._layers[0] if model._layers else None
if isinstance(first, keras.layers.InputLayer):
assert first.batch_shape == input_shape Try / catch
try:
model.build(input_shape)
except ValueError as e:
if 'already been configured' in str(e):
shape = model._layers[0].batch_shape Prevention
- 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
When it happens
Trigger: model = keras.Sequential([keras.Input((32,)), ...]); model.build((None, 64,))
Common situations: Loading checkpoints, writing tutorials, or migrating from Keras 2 where build behaved differently
Related errors
- Sequential model '{self.name}' has no defined input shape ye
- To call stateless_call, {self.__class__.__name__} must be bu
- Cannot quantize a layer that isn't yet built. Layer '{self.n
- Layer '{self.name}' was never built and thus it doesn't have
- You tried to call `count_params` on layer '{self.name}', but
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/7dc34e0c83eace6e.
Report an issue: GitHub.