keras-team/keras · error · TypeError

Unsupported data type: {type(data)}. `adapt` supports `np.nd

Error message

Unsupported data type: {type(data)}. `adapt` supports `np.ndarray`, backend tensors, `tf.data.Dataset`, `keras.utils.PyDataset`, and iterables of batches (e.g. list, generator).

What it means

Normalization.adapt() only accepts np.ndarray, backend tensors, tf.data.Dataset, PyDataset, or iterables of batches. Anything else (int, str path, callable) hits the final else-branch and raises a TypeError enumerating the supported types.

Source

Thrown at keras/src/layers/preprocessing/normalization.py:320

                    "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:
            raise TypeError(
                f"Unsupported data type: {type(data)}. `adapt` supports "
                f"`np.ndarray`, backend tensors, `tf.data.Dataset`, "
                f"`keras.utils.PyDataset`, and iterables of batches (e.g. "
                f"list, generator)."
            )

        if not self.built:
            self.build(input_shape)
        else:
            for d in self._keep_axis:
                if input_shape[d] != self._build_input_shape[d]:
                    raise ValueError(
                        "The layer was built with "
                        f"input_shape={self._build_input_shape}, "
                        "but adapt() is being called with data with "
                        f"an incompatible shape, data.shape={input_shape}"
                    )

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Load the data first, then adapt: layer.adapt(np.loadtxt(path, delimiter=','))
  2. Wrap sequences: layer.adapt(list_of_batches)
  3. Convert containers: layer.adapt(df['col'].to_numpy()) instead of raw non-array objects

Example fix

// before
layer.adapt('train.csv')
// after
import numpy as np
layer.adapt(np.loadtxt('train.csv', delimiter=','))
Defensive patterns

Strategy: type-guard

Validate before calling

def adapt_ready(d):
    return hasattr(d, 'shape') or hasattr(d, '__iter__')
assert adapt_ready(data), 'pass arrays, datasets, or iterables of batches'

Type guard

def supported_adapt_input(d):
    return hasattr(d, 'shape') or hasattr(d, '__iter__')

Prevention

When it happens

Trigger: layer.adapt(5), adapt('path/to/file.csv'), adapt(some_function), or passing an object without __iter__ that is not one of the recognized dataset types.

Common situations: Passing a path expecting adapt to load it; passing a scikit-learn Dataset-like object; typos where a variable holding a filename is adapted.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/e4852296b754f124. Report an issue: GitHub.