keras-team/keras · error · ValueError
`idf_weights` should only be set if `output_mode` is…
Error message
`idf_weights` should only be set if `output_mode` is `'tf_idf'`. Received: idf_weights={idf_weights} and output_mode={output_mode} What it means
Inverse document frequency weights only apply to the 'tf_idf' output mode; supplying them for any other output_mode is rejected. This ValueError fires at IndexLookup construction when idf_weights is not None while output_mode is not 'tf_idf'.
Solutions
- Set `output_mode='tf_idf'` if you want idf weighting.
- Or remove the `idf_weights` argument.
Example fix
# before layer = IndexLookup(vocabulary=vocab, idf_weights=idf) # after layer = IndexLookup(vocabulary=vocab, idf_weights=idf, output_mode='tf_idf')
Defensive patterns
Strategy: validation
Validate before calling
if idf_weights is not None and output_mode != 'tf_idf':
raise ValueError('idf_weights requires output_mode=tf_idf') Prevention
- Treat idf_weights as a tf_idf-only argument in config schemas.
- When porting sklearn vectorizers, set output_mode first, then weights.
When it happens
Trigger: `IndexLookup(vocabulary=vocab, idf_weights=w)` while output_mode is 'int' (default), 'multi_hot', 'count' or 'one_hot'.
Common situations: Reusing a TF-IDF layer's full argument set after switching output_mode, or loading idf weights from a fitted sklearn TfidfVectorizer into a count-mode layer.
Related errors
- `idf_weights` should only be set if output_mode is…
- `idf_weights` must be set if output_mode is 'tf_idf'.
- `idf_weights` must be the same length as vocabulary…
- `num_oov_indices` must be greater than or equal to 0…
- `output_mode` must be `'int'` when `invert` is true…
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/24a81939ad95b068.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/layers/preprocessing/index_lookup.py:210
arg_name="output_mode",
)
if invert and output_mode != "int":
raise ValueError(
"`output_mode` must be `'int'` when `invert` is true. "
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'`, `'count'` or `'tf_idf'`. "
f"Received: sparse={sparse} and "
f"output_mode={output_mode}"
)
if idf_weights is not None and output_mode != "tf_idf":
raise ValueError(
"`idf_weights` should only be set if `output_mode` is "
f"`'tf_idf'`. Received: idf_weights={idf_weights} and "
f"output_mode={output_mode}"
)
super().__init__(name=name)
self._convert_input_args = False
self._allow_non_tensor_positional_args = True
self.supports_jit = False
self.invert = invert
self.max_tokens = max_tokens
self.num_oov_indices = num_oov_indices
self.mask_token = mask_token
self.oov_token = oov_token
self.output_mode = output_mode
self.sparse = sparse
self.pad_to_max_tokens = pad_to_max_tokensView on GitHub (pinned to 7a34a03db6)