keras-team/keras · error · ValueError

output_mode `'tf_idf'` does not support loading a vocabulary

Error message

output_mode `'tf_idf'` does not support loading a vocabulary from file.

What it means

TF-IDF mode needs per-token idf weights, which a plain vocabulary file cannot carry, so loading a vocabulary from a file path is unsupported when output_mode='tf_idf'.

Source

Thrown at keras/src/layers/preprocessing/index_lookup.py:462

        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}'"
                )

            if not tf.io.gfile.exists(vocabulary):
                raise ValueError(
                    f"Vocabulary file {vocabulary} does not exist."
                )
            if self.output_mode == "tf_idf":
                raise ValueError(
                    "output_mode `'tf_idf'` does not support loading a "
                    "vocabulary from file."
                )
            self.lookup_table = self._lookup_table_from_file(vocabulary)
            self._record_vocabulary_size()
            return

        if not tf.executing_eagerly() and (
            tf.is_tensor(vocabulary) or tf.is_tensor(idf_weights)
        ):
            raise RuntimeError(
                f"Cannot set a tensor vocabulary on layer {self.name} "
                "when not executing eagerly. "
                "Create this layer or call `set_vocabulary()` "
                "outside of any traced function."
            )

        # TODO(mattdangerw): for better performance we should rewrite this

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Load the file yourself and pass tokens plus weights, then `set_vocabulary(tokens, idf_weights=w)`.
  2. Or switch output_mode to 'int' or 'multi_hot' if idf weighting is not needed.

Example fix

# before
tfidf_layer.set_vocabulary('vocab.txt')

# after
tokens = [l.strip() for l in open('vocab.txt')]
tfidf_layer.set_vocabulary(tokens, idf_weights=idf)
Defensive patterns

Strategy: validation

Validate before calling

if layer.output_mode == 'tf_idf':
    tokens = [l.strip() for l in open(vocab_file)]
    layer.set_vocabulary(tokens, idf_weights=idf)
else:
    layer.set_vocabulary(vocab_file)

Prevention

When it happens

Trigger: `layer.set_vocabulary('vocab.txt')` on a layer built with output_mode='tf_idf'.

Common situations: Reusing a text vocabulary file from a StringLookup or int-mode pipeline to build a TF-IDF layer.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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