keras-team/keras · error · ValueError
`idf_weights` should only be set if output_mode is `'tf_idf'
Error message
`idf_weights` should only be set if output_mode is `'tf_idf'`. Received: output_mode={self.output_mode} and idf_weights={idf_weights} What it means
The mirror of the missing-weights check: set_vocabulary() rejects `idf_weights` when the layer's output_mode is anything other than tf_idf, keeping the weight vector and the mode consistent.
Source
Thrown at keras/src/layers/preprocessing/index_lookup.py:438
Args:
vocabulary: Either an array or a string path to a text file.
If passing an array, can pass a tuple, list,
1D numpy array, or 1D tensor containing the vocbulary terms.
If passing a file path, the file should contain one line
per term in the vocabulary.
idf_weights: A tuple, list, 1D numpy array, or 1D tensor
of inverse document frequency weights with equal
length to vocabulary. Must be set if `output_mode`
is `"tf_idf"`. Should not be set otherwise.
"""
if self.output_mode == "tf_idf":
if idf_weights is None:
raise ValueError(
"`idf_weights` must be set if output_mode is 'tf_idf'."
)
elif idf_weights is not None:
raise ValueError(
"`idf_weights` should only be set if output_mode is "
f"`'tf_idf'`. Received: output_mode={self.output_mode} "
f"and idf_weights={idf_weights}"
)
if isinstance(vocabulary, str):
if serialization_lib.in_safe_mode():
raise ValueError(
"Requested the loading of a vocabulary file outside of the "
"model archive. This carries a potential risk of loading "
"arbitrary and sensitive files and thus it is disallowed "
"by default. If you trust the source of the artifact, you "
"can override this error by passing `safe_mode=False` to "
"the loading function, or calling "
"`keras.config.enable_unsafe_deserialization(). "
f"Vocabulary file: '{vocabulary}'"
)
View on GitHub (pinned to 7a34a03db6)
Solutions
- Only pass idf_weights to the tf_idf layer; branch on `layer.output_mode`.
- Or rebuild the layer with `output_mode='tf_idf'` if idf scaling is wanted.
Example fix
# before
def set_vocab(layer, vocab, idf):
layer.set_vocabulary(vocab, idf_weights=idf)
# after
def set_vocab(layer, vocab, idf):
if layer.output_mode == 'tf_idf':
layer.set_vocabulary(vocab, idf_weights=idf)
else:
layer.set_vocabulary(vocab) Defensive patterns
Strategy: validation
Validate before calling
if layer.output_mode == 'tf_idf':
layer.set_vocabulary(vocab, idf_weights=idf)
else:
layer.set_vocabulary(vocab) Prevention
- Branch on layer.output_mode in shared vocabulary-setting helpers.
- Store per-layer metadata (mode) next to vocabularies in pipelines.
When it happens
Trigger: `layer.set_vocabulary(vocab, idf_weights=w)` on a layer constructed with output_mode 'int', 'multi_hot', 'count' or 'one_hot'.
Common situations: Shared vocabulary-setting helper code used for both a tf_idf layer and an int-index layer.
Related errors
- `idf_weights` should only be set if `output_mode` is `'tf_id
- `num_oov_indices` must be greater than or equal to 0. Receiv
- `salt` can only be used when `oov_method='farmhash'`. Receiv
- The `salt` argument for `IndexLookup` can only be a tuple of
- `output_mode` must be `'int'` when `invert` is true. Receive
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/277391e268479143.
Report an issue: GitHub.