keras-team/keras · error · ValueError
`adapt()` can only be called on a tf.data.Dataset or a dict
Error message
`adapt()` can only be called on a tf.data.Dataset or a dict of arrays/lists. Received instead: {dataset} (of type {type(dataset)}) What it means
FeatureSpace.adapt() only accepts a tf.data.Dataset or a plain dict of arrays/lists so it can iterate samples and compute vocabulary statistics. Any other input type (DataFrame, list of dicts, numpy array, generator) is rejected immediately. Convert your data to one of the two accepted forms before adapting.
Source
Thrown at keras/src/layers/preprocessing/feature_space.py:516
# Special case: a Normalization layer with preset mean/variance.
# Not adaptable.
if isinstance(preprocessor, layers.Normalization):
if preprocessor.input_mean is not None:
continue
# Special case: a TextVectorization layer with provided vocabulary.
elif isinstance(preprocessor, layers.TextVectorization):
if preprocessor._has_input_vocabulary:
continue
if hasattr(preprocessor, "adapt"):
adaptable_preprocessors.append(name)
return adaptable_preprocessors
def adapt(self, dataset, verbose=1):
if not isinstance(dataset, tf.data.Dataset):
if isinstance(dataset, dict):
dataset = tf.data.Dataset.from_tensor_slices(dataset)
else:
raise ValueError(
"`adapt()` can only be called on a tf.data.Dataset or a "
"dict of arrays/lists. "
f"Received instead: {dataset} (of type {type(dataset)})"
)
adaptable_preprocessors = self._list_adaptable_preprocessors()
if not adaptable_preprocessors:
self._is_adapted = True
self.get_encoded_features()
self.built = True
self._sublayers_built = True
return
# Check if the dataset needs batching
x = next(iter(dataset))
if len(x[adaptable_preprocessors[0]].shape) == 0:
dataset = dataset.batch(32)
View on GitHub (pinned to 7a34a03db6)
Solutions
- Convert the input to a dict of arrays: fs.adapt({'col1': arr1, 'col2': arr2})
- Or wrap it in a dataset: fs.adapt(tf.data.Dataset.from_tensor_slices(dict(df)))
- For DataFrames: fs.adapt({c: df[c].values for c in df.columns})
Example fix
// before
fs.adapt(df) # pandas DataFrame
// after
fs.adapt({col: df[col].values for col in df.columns})
# or: fs.adapt(tf.data.Dataset.from_tensor_slices(dict(df))) Defensive patterns
Strategy: validation
Validate before calling
if not isinstance(dataset, tf.data.Dataset) and not isinstance(dataset, dict):
raise TypeError("adapt() needs a tf.data.Dataset or dict of arrays") Type guard
def is_adaptable(x):
return isinstance(x, tf.data.Dataset) or (
isinstance(x, dict) and all(hasattr(v, "__len__") or hasattr(v, "__getitem__") for v in x.values())
) Try / catch
catch ValueError around fs.adapt(raw_data) and fall back to converting dict inputs to a tf.data.Dataset before retrying
Prevention
- Pass tf.data.Dataset or dict of numpy/array-like values to FeatureSpace.adapt
- Never pass a bare DataFrame; convert with dict(df) or {c: df[c].values for c in df} first
When it happens
Trigger: fs.adapt(pandas_dataframe), fs.adapt(numpy_array), fs.adapt(list_of_dicts), or fs.adapt(generator) — anything that is not a tf.data.Dataset or dict.
Common situations: Passing a pandas DataFrame, a numpy array, or a list of dicts to adapt(). Loading data with pd.read_csv and handing it straight to FeatureSpace.
Related errors
- A FeatureSpace can only be called with a dict. Received: dat
- Feature '{name}' has `output_mode='one_hot'`. Thus its prepr
- Feature '{name}' has `output_mode='one_hot'`. However it isn
- Cannot concatenate features because feature '{name}' has not
- You need to call `.adapt(dataset)` on the FeatureSpace befor
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/ea97a3d0ee3c17e6.
Report an issue: GitHub.