keras-team/keras · error · ValueError
Invalid value for argument `output_mode`. Expected one of {a
Error message
Invalid value for argument `output_mode`. Expected one of {accepted_output_modes}. Received: output_mode={output_mode} What it means
The Hashing preprocessing layer only supports four output modes: 'int', 'one_hot', 'multi_hot', and 'count'. The layer validates output_mode in __init__ and raises this ValueError when any other string (or a typo) is passed. Internally the mode decides how the hashed bucket index is converted into an output tensor.
Source
Thrown at keras/src/layers/preprocessing/hashing.py:177
if num_bins is None or num_bins <= 0:
raise ValueError(
"The `num_bins` for `Hashing` cannot be `None` or "
f"non-positive values. Received: num_bins={num_bins}."
)
if output_mode == "int" and (
self.dtype_policy.name not in ("int32", "int64")
):
raise ValueError(
'When `output_mode="int"`, `dtype` should be an integer '
f"type, 'int32' or 'in64'. Received: dtype={kwargs['dtype']}"
)
# 'output_mode' must be one of (INT, ONE_HOT, MULTI_HOT, COUNT)
accepted_output_modes = ("int", "one_hot", "multi_hot", "count")
if output_mode not in accepted_output_modes:
raise ValueError(
"Invalid value for argument `output_mode`. "
f"Expected one of {accepted_output_modes}. "
f"Received: output_mode={output_mode}"
)
if sparse and output_mode == "int":
raise ValueError(
"`sparse` may only be true if `output_mode` is "
'`"one_hot"`, `"multi_hot"`, or `"count"`. '
f"Received: sparse={sparse} and "
f"output_mode={output_mode}"
)
self.num_bins = num_bins
self.mask_value = mask_value
self.strong_hash = True if salt is not None else False
self.output_mode = output_mode
self.sparse = sparseView on GitHub (pinned to 7a34a03db6)
Solutions
- Set output_mode to one of exactly 'int', 'one_hot', 'multi_hot', or 'count'.
- Note that 'tf_idf' is not supported by Hashing; use TextVectorization if you need tf-idf output.
- Check for leading/trailing whitespace or wrong case in the string you pass programmatically.
Example fix
# before layer = keras.layers.Hashing(num_bins=100, output_mode="onehot") # after layer = keras.layers.Hashing(num_bins=100, output_mode="one_hot")
Defensive patterns
Strategy: validation
Validate before calling
ALLOWED = {"int", "one_hot", "multi_hot", "count"}
if output_mode not in ALLOWED:
raise ValueError(f"unsupported output_mode {output_mode!r}") Type guard
def is_valid_output_mode(m):
return isinstance(m, str) and m in {"int", "one_hot", "multi_hot", "count"}
Prevention
- Keep output mode names in a module-level constant set and reuse it at every call site.
- Add a unit test that constructs the layer with each supported mode.
When it happens
Trigger: Calling keras.layers.Hashing(output_mode=...) with a misspelled or unsupported value, e.g. 'onehot', 'multi-hot', 'INT', 'freq', 'binary', or passing None.
Common situations: Porting code from tf.keras StringLookup/IntegerLookup (which accept 'tf_idf' and other modes) to the standalone Hashing layer; typos from camelCase vs snake_case mode names.
Related errors
- `sparse` may only be true if `output_mode` is `"one_hot"`, `
- The `salt` argument for `Hashing` can only be a tuple of siz
- `salt` can only be used when `oov_method='farmhash'`. Receiv
- Invalid quantization mode. Expected one of {dtype_policies.Q
- `adapt()` can only be called on a tf.data.Dataset or a dict
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/32036690996bbaf8.
Report an issue: GitHub.