keras-team/keras · error · ValueError

You need to call `.adapt(dataset)` on the FeatureSpace befor

Error message

You need to call `.adapt(dataset)` on the FeatureSpace before you can start using it.

What it means

FeatureSpace must be adapted to data before use whenever it contains adaptable preprocessors (lookup/vocabulary layers). adapt() computes vocabularies; without it the layer cannot translate raw values to indices and refuses to run.

Source

Thrown at keras/src/layers/preprocessing/feature_space.py:741

                        f"has not been encoded (it has dtype {dtype}). "
                        "Consider using `output_mode='dict'`."
                    )
                features_to_concat.append(feature)
            else:
                output_dict[name] = feature

        if self.output_mode == "concat":
            self.concat = TFDConcat(axis=-1)
            return self.concat(features_to_concat)
        else:
            return output_dict

    def _check_if_adapted(self):
        if not self._is_adapted:
            if not self._list_adaptable_preprocessors():
                self._is_adapted = True
            else:
                raise ValueError(
                    "You need to call `.adapt(dataset)` on the FeatureSpace "
                    "before you can start using it."
                )

    def _check_if_built(self):
        if not self._sublayers_built:
            self._check_if_adapted()
            # Finishes building
            self.get_encoded_features()
            self._sublayers_built = True

    def _convert_input(self, x):
        if not isinstance(x, (tf.Tensor, tf.SparseTensor, tf.RaggedTensor)):
            if not isinstance(x, (list, tuple, int, float)):
                x = backend.convert_to_numpy(x)
            x = tf.convert_to_tensor(x)
        return x

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Call fs.adapt(dataset) on your raw (unencoded) training data before get_encoded_features()/__call__
  2. If there are truly no adaptable features, ensure none of the feature specs create lookup layers
  3. Re-adapt after changing feature specs

Example fix

// before
fs = FeatureSpace(features)
inputs = fs.get_encoded_features()  # ValueError
// after
fs = FeatureSpace(features)
fs.adapt(train_ds)
inputs = fs.get_encoded_features()
Defensive patterns

Strategy: validation

Validate before calling

if fs._list_adaptable_preprocessors() and not fs._is_adapted:
    fs.adapt(raw_train_data)  # adapt before use

Type guard

def is_adapted(fs):
    return fs._is_adapted or not fs._list_adaptable_preprocessors()

Try / catch

catch ValueError around model/FeatureSpace usage and call fs.adapt(unlabeled_data) before retrying

Prevention

When it happens

Trigger: Calling fs.get_encoded_features(), fs(data), or _check_if_built paths before any fs.adapt(dataset) call, while the feature specs include vocabulary/hash-based preprocessors.

Common situations: Building a model that calls FeatureSpace before adapt(); loading a FeatureSpace config without its adapted state; skipping adapt because the data 'already looks fine'.

Related errors


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