keras-team/keras · error · ValueError
Feature '{name}' has `output_mode='one_hot'`. Thus its prepr
Error message
Feature '{name}' has `output_mode='one_hot'`. Thus its preprocessor should return an integer dtype. Instead it returns a {dtype} dtype. What it means
When FeatureSpace is configured to produce one-hot output for a feature (output_mode='one_hot'), the feature's preprocessor must emit integer indices that can be binarized. The code checks the dtype flowing out of the preprocessor and rejects non-integer output such as float32 or string.
Source
Thrown at keras/src/layers/preprocessing/feature_space.py:685
] + [
self.crosses_by_name[name] for name in self._crossed_features_names
]
for name, feature, spec in zip(all_names, all_features, all_specs):
if tree.is_nested(feature):
dtype = tree.flatten(feature)[0].dtype
else:
dtype = feature.dtype
dtype = backend.standardize_dtype(dtype)
if spec.output_mode == "one_hot":
preprocessor = self.preprocessors.get(
name
) or self.crossers.get(name)
cardinality = None
if not dtype.startswith("int"):
raise ValueError(
f"Feature '{name}' has `output_mode='one_hot'`. "
"Thus its preprocessor should return an integer dtype. "
f"Instead it returns a {dtype} dtype."
)
if isinstance(
preprocessor, (layers.IntegerLookup, layers.StringLookup)
):
cardinality = preprocessor.vocabulary_size()
elif isinstance(preprocessor, layers.CategoryEncoding):
cardinality = preprocessor.num_tokens
elif isinstance(preprocessor, layers.Discretization):
cardinality = preprocessor.num_bins
elif isinstance(
preprocessor, (layers.HashedCrossing, layers.Hashing)
):
cardinality = preprocessor.num_bins
else:View on GitHub (pinned to 7a34a03db6)
Solutions
- Make the feature's preprocessor return integer indices (e.g. use integer_hashed, string_hashed, or a lookup layer with output_mode='int')
- If the feature must stay continuous, do not one-hot encode it — use output_mode='float' for that feature or keep it out of one_hot mode
- For crossing features in one_hot mode, set crossing_output_mode='one_hot' or 'int' with an appropriate hasher
Example fix
// before
fs = FeatureSpace(..., output_mode="one_hot")
# feature 'x' preprocessor returns float
// after
fs = FeatureSpace(
features={"x": FeatureSpace.integer_hashed(max_tokens=32)},
output_mode="one_hot",
) Defensive patterns
Strategy: validation
Validate before calling
out = fs.preprocessors[name](sample_batch)
assert str(out.dtype).startswith("int"), f"{name} yields {out.dtype}" Type guard
def is_int_preprocessor(p):
d = getattr(p, "dtype", None)
return d is not None and str(d).startswith("int") Try / catch
catch ValueError from fs.get_encoded_features() and inspect the offending feature's preprocessor output dtype via fs.preprocessors[name] before correcting the feature spec
Prevention
- Check that the preprocessor assigned to a feature returns integer output before setting output_mode='one_hot'
- Keep FeatureSpace feature specs consistent with the desired output_mode
When it happens
Trigger: A feature whose preprocessor returns floats (e.g. normalization) combined with FeatureSpace output_mode='one_hot'; a crossing outputting floats while crossing_output_mode='one_hot'.
Common situations: A custom feature spec or crossing returns floats; mixing output_mode='one_hot' with a preprocessor that has not been set to integer output; misconfigured crossing_output_mode on FeatureSpace.
Related errors
- Feature '{name}' has `output_mode='one_hot'`. However it isn
- `dtype` was passed both positionally and as a keyword argume
- Quantization mode='{mode}' doesn't work well with compute_dt
- `adapt()` can only be called on a tf.data.Dataset or a dict
- Cannot concatenate features because feature '{name}' has not
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/59b87ea87de2c6ed.
Report an issue: GitHub.