{"record":{"id":"ea97a3d0ee3c17e6","repo":"keras-team/keras","slug":"adapt-can-only-be-called-on-a-tf-data-dataset","errorCode":null,"errorMessage":"`adapt()` can only be called on a tf.data.Dataset or a dict of arrays/lists. Received instead: {dataset} (of type {type(dataset)})","messagePattern":"`adapt\\(\\)` can only be called on a tf\\.data\\.Dataset or a dict of arrays/lists\\. Received instead: (.+?) \\(of type (.+?)\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/layers/preprocessing/feature_space.py","lineNumber":516,"sourceCode":"            # Special case: a Normalization layer with preset mean/variance.\n            # Not adaptable.\n            if isinstance(preprocessor, layers.Normalization):\n                if preprocessor.input_mean is not None:\n                    continue\n            # Special case: a TextVectorization layer with provided vocabulary.\n            elif isinstance(preprocessor, layers.TextVectorization):\n                if preprocessor._has_input_vocabulary:\n                    continue\n            if hasattr(preprocessor, \"adapt\"):\n                adaptable_preprocessors.append(name)\n        return adaptable_preprocessors\n\n    def adapt(self, dataset, verbose=1):\n        if not isinstance(dataset, tf.data.Dataset):\n            if isinstance(dataset, dict):\n                dataset = tf.data.Dataset.from_tensor_slices(dataset)\n            else:\n                raise ValueError(\n                    \"`adapt()` can only be called on a tf.data.Dataset or a \"\n                    \"dict of arrays/lists. \"\n                    f\"Received instead: {dataset} (of type {type(dataset)})\"\n                )\n\n        adaptable_preprocessors = self._list_adaptable_preprocessors()\n        if not adaptable_preprocessors:\n            self._is_adapted = True\n            self.get_encoded_features()\n            self.built = True\n            self._sublayers_built = True\n            return\n\n        # Check if the dataset needs batching\n        x = next(iter(dataset))\n        if len(x[adaptable_preprocessors[0]].shape) == 0:\n            dataset = dataset.batch(32)\n","sourceCodeStart":498,"sourceCodeEnd":534,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/layers/preprocessing/feature_space.py#L498-L534","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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})"],"exampleFix":"// before\nfs.adapt(df)  # pandas DataFrame\n// after\nfs.adapt({col: df[col].values for col in df.columns})\n# or: fs.adapt(tf.data.Dataset.from_tensor_slices(dict(df)))","handlingStrategy":"validation","validationCode":"if not isinstance(dataset, tf.data.Dataset) and not isinstance(dataset, dict):\n    raise TypeError(\"adapt() needs a tf.data.Dataset or dict of arrays\")","typeGuard":"def is_adaptable(x):\n    return isinstance(x, tf.data.Dataset) or (\n        isinstance(x, dict) and all(hasattr(v, \"__len__\") or hasattr(v, \"__getitem__\") for v in x.values())\n    )","tryCatchPattern":"catch ValueError around fs.adapt(raw_data) and fall back to converting dict inputs to a tf.data.Dataset before retrying","preventionTips":["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"],"tags":["keras","preprocessing","feature-space","input-type"],"backgroundTag":"invalid-input-type","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}