{"record":{"id":"2087e8e1ece830c8","repo":"keras-team/keras","slug":"adapt-received-an-empty-iterable-no-batches-e","errorCode":null,"errorMessage":"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).","messagePattern":"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\\)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/normalization.py","lineNumber":301,"sourceCode":"                    if isinstance(element_spec, tuple)\n                    else element_spec\n                )\n                return tuple(x_spec.shape)\n\n            input_shape = get_input_shape(data)\n            if len(input_shape) == 1:\n                data = data.batch(128)\n                input_shape = get_input_shape(data)\n        elif isinstance(data, PyDataset):\n            input_shape = _extract_batch(data[0]).shape\n        elif hasattr(data, \"__iter__\"):\n            data_is_iterable = True\n            # Consume first batch to infer input_shape; then chain it back for\n            # accumulation so we iterate over (first_batch, *rest).\n            data_iter = iter(data)\n            first_batch = next(data_iter, None)\n            if first_batch is None:\n                raise ValueError(\n                    \"adapt() received an empty iterable (no batches). \"\n                    \"Expected at least one batch. Pass a non-empty iterable \"\n                    \"of arrays or tensors, e.g. layer.adapt([x]) or \"\n                    \"layer.adapt(list_of_batches).\"\n                )\n            first_batch = _extract_batch(first_batch)\n            input_shape = getattr(first_batch, \"shape\", None)\n            if input_shape is None:\n                raise TypeError(\n                    \"adapt() expects an iterable that yields arrays or \"\n                    \"tensors with a `.shape` attribute (e.g. numpy arrays or \"\n                    \"backend tensors). Got an element of type \"\n                    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:","sourceCodeStart":283,"sourceCodeEnd":319,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/normalization.py#L283-L319","documentation":"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.","triggerScenarios":"layer.adapt([]), adapt(generator_that_yields_nothing), adapt(filter(...) with no matching rows), or a tf.data.Dataset of size 0.","commonSituations":"Empty train split after a bad filter/mask; placeholder lists during scaffolding; generators guarded by a condition that never fires.","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"],"exampleFix":"// before\nlayer.adapt(filtered_df[col])  # filtered empty\n// after\nassert len(filtered_df) > 0\nlayer.adapt(filtered_df[col].to_numpy())","handlingStrategy":"validation","validationCode":"first = next(iter(data), None)\nif first is None:\n    raise ValueError('cannot adapt on empty data; pass e.g. [x]')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Assert non-empty before adapt","Guard dataset-producing filters with count checks"],"tags":["keras","normalization","adapt","empty-data"],"backgroundTag":"empty-dataset-input","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}