{"record":{"id":"aca1d2c62320aa1e","repo":"keras-team/keras","slug":"the-layer-was-built-with-input-shape-self-build","errorCode":null,"errorMessage":"The layer was built with input_shape={self._build_input_shape}, but adapt() is being called with data with an incompatible shape, data.shape={input_shape}","messagePattern":"The layer was built with input_shape=(.+?), but adapt\\(\\) is being called with data with an incompatible shape, data\\.shape=(.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/normalization.py","lineNumber":332,"sourceCode":"                    f\"{type(first_batch).__name__}. Ensure each yielded \"\n                    \"element is array-like with a `.shape` attribute.\"\n                )\n            input_shape = tuple(input_shape)\n            data = itertools.chain([first_batch], data_iter)\n        else:\n            raise TypeError(\n                f\"Unsupported data type: {type(data)}. `adapt` supports \"\n                f\"`np.ndarray`, backend tensors, `tf.data.Dataset`, \"\n                f\"`keras.utils.PyDataset`, and iterables of batches (e.g. \"\n                f\"list, generator).\"\n            )\n\n        if not self.built:\n            self.build(input_shape)\n        else:\n            for d in self._keep_axis:\n                if input_shape[d] != self._build_input_shape[d]:\n                    raise ValueError(\n                        \"The layer was built with \"\n                        f\"input_shape={self._build_input_shape}, \"\n                        \"but adapt() is being called with data with \"\n                        f\"an incompatible shape, data.shape={input_shape}\"\n                    )\n\n        if isinstance(data, np.ndarray):\n            total_mean = np.mean(data, axis=self._reduce_axis)\n            total_var = np.var(data, axis=self._reduce_axis)\n        elif backend.is_tensor(data):\n            total_mean = ops.mean(data, axis=self._reduce_axis)\n            total_var = ops.var(data, axis=self._reduce_axis)\n        elif isinstance(data, (tf.data.Dataset, PyDataset)) or data_is_iterable:\n            total_mean = ops.zeros(self._mean_and_var_shape)\n            total_var = ops.zeros(self._mean_and_var_shape)\n            total_count = 0\n\n            steps = None","sourceCodeStart":314,"sourceCodeEnd":350,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/normalization.py#L314-L350","documentation":"Once a Normalization layer is built, its kept-axis dimensions are fixed. adapt() on a built layer compares input_shape[d] to _build_input_shape[d] for each kept axis and raises when they differ, because the stored mean/variance buffers would no longer align with the data.","triggerScenarios":"Calling layer.adapt(data2) whose kept-axis dims differ from the data (or manual build) that first built it, e.g. built on 10 features, adapting 13-feature data.","commonSituations":"Schema drift between training and serving features; reusing a layer across preprocessing versions; adapting validation data with extra columns.","solutions":["Create and adapt a fresh Normalization layer for the new shape","Select/reorder features so kept-axis dims match the original build shape","If the shape truly changed, rebuild the whole model from the new adapted layer"],"exampleFix":"// before\nnorm.adapt(x_train)          # built with 10 features\nnorm.adapt(x_train_v2)       # 13 features -> ValueError\n// after\nnorm = keras.layers.Normalization()\nnorm.adapt(x_train_v2)","handlingStrategy":"validation","validationCode":"for d in layer._keep_axis:\n    if input_shape[d] != layer._build_input_shape[d]:\n        raise ValueError('shape drift; rebuild the layer')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Freeze feature schemas and validate before adapt","Recreate the Normalization layer on schema change"],"tags":["keras","normalization","adapt","shape-validation"],"backgroundTag":"array-shape-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}