keras-team/keras · error · ValueError

A FeatureSpace can only be called with a dict. Received: dat

Error message

A FeatureSpace can only be called with a dict. Received: data={data} (of type {type(data)}

What it means

FeatureSpace is a multi-input layer keyed by feature name, so it can only be called with a dict mapping feature names to batch data. Any non-dict input (tensor, list, DataFrame, tuple) is rejected at call time.

Source

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

    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

    def __call__(self, data):
        self._check_if_built()
        if not isinstance(data, dict):
            raise ValueError(
                "A FeatureSpace can only be called with a dict. "
                f"Received: data={data} (of type {type(data)}"
            )

        # Many preprocessing layers support all backends but many do not.
        # Switch to TF to make FeatureSpace work universally.
        data = {key: self._convert_input(value) for key, value in data.items()}
        rebatched = False
        for name, x in data.items():
            if len(x.shape) == 0:
                data[name] = tf.reshape(x, (1, 1))
                rebatched = True
            elif len(x.shape) == 1:
                data[name] = tf.expand_dims(x, -1)

        with backend_utils.TFGraphScope():
            # This scope is to make sure that inner DataLayers
            # will not convert outputs back to backend-native --

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Call fs({'feature_name': batch_array, ...}) with keys matching the features= spec
  2. For DataFrames convert first: fs(dict(df)) or fs({c: df[c].values for c in df.columns})
  3. When using tf.data, keep datasets yielding dicts

Example fix

// before
raw_inputs = fs(x_array)  # wrong
// after
raw_inputs = fs({"a": a_array, "b": b_array})
Defensive patterns

Strategy: type-guard

Validate before calling

assert isinstance(data, dict), "FeatureSpace expects a dict of feature_name -> batch"

Type guard

def is_feature_dict(d, feature_names):
    return isinstance(d, dict) and set(d) == set(feature_names)

Try / catch

catch ValueError and convert data to a dict keyed by feature names before calling again

Prevention

When it happens

Trigger: fs(numpy_array), fs(list_of_tensors), fs(pandas_dataframe), or any single-tensor call instead of fs({'feature': batch}).

Common situations: Calling fs(data) with a numpy array, a pandas DataFrame, or a list instead of a dict; feature-name mismatch after refactoring keys.

Related errors


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