keras-team/keras · error · ValueError
adapt() received an empty iterable (no batches). Expected at
Error message
adapt() received an empty iterable (no batches). Expected at least one batch. Pass a non-empty iterable of arrays or tensors, e.g. layer.adapt([x]) or layer.adapt(list_of_batches).
What it means
Normalization.adapt() iterates over batches; when handed a generic iterable it pulls the first batch to infer the input shape. If the iterator is immediately exhausted (first_batch is None), it raises this ValueError instead of silently producing garbage statistics.
Source
Thrown at keras/src/layers/preprocessing/normalization.py:301
if isinstance(element_spec, tuple)
else element_spec
)
return tuple(x_spec.shape)
input_shape = get_input_shape(data)
if len(input_shape) == 1:
data = data.batch(128)
input_shape = get_input_shape(data)
elif isinstance(data, PyDataset):
input_shape = _extract_batch(data[0]).shape
elif hasattr(data, "__iter__"):
data_is_iterable = True
# Consume first batch to infer input_shape; then chain it back for
# accumulation so we iterate over (first_batch, *rest).
data_iter = iter(data)
first_batch = next(data_iter, None)
if first_batch is None:
raise ValueError(
"adapt() received an empty iterable (no batches). "
"Expected at least one batch. Pass a non-empty iterable "
"of arrays or tensors, e.g. layer.adapt([x]) or "
"layer.adapt(list_of_batches)."
)
first_batch = _extract_batch(first_batch)
input_shape = getattr(first_batch, "shape", None)
if input_shape is None:
raise TypeError(
"adapt() expects an iterable that yields arrays or "
"tensors with a `.shape` attribute (e.g. numpy arrays or "
"backend tensors). Got an element of type "
f"{type(first_batch).__name__}. Ensure each yielded "
"element is array-like with a `.shape` attribute."
)
input_shape = tuple(input_shape)
data = itertools.chain([first_batch], data_iter)
else:View on GitHub (pinned to 7a34a03db6)
Solutions
- Pass a non-empty iterable, e.g. layer.adapt([x]) for a single array
- Check the data source is non-empty before adapt
- Fix the upstream filter that emptied the dataset
Example fix
// before layer.adapt(filtered_df[col]) # filtered empty // after assert len(filtered_df) > 0 layer.adapt(filtered_df[col].to_numpy())
Defensive patterns
Strategy: validation
Validate before calling
first = next(iter(data), None)
if first is None:
raise ValueError('cannot adapt on empty data; pass e.g. [x]') Prevention
- Assert non-empty before adapt
- Guard dataset-producing filters with count checks
When it happens
Trigger: layer.adapt([]), adapt(generator_that_yields_nothing), adapt(filter(...) with no matching rows), or a tf.data.Dataset of size 0.
Common situations: Empty train split after a bad filter/mask; placeholder lists during scaffolding; generators guarded by a condition that never fires.
Related errors
- adapt() expects an iterable that yields arrays or tensors wi
- Unsupported data type: {type(data)}. `adapt` supports `np.nd
- The layer was built with input_shape={self._build_input_shap
- You need to call `.adapt(dataset)` on the FeatureSpace befor
- Cannot adapt layer '{self.name}' after setting a static voca
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/2087e8e1ece830c8.
Report an issue: GitHub.