keras-team/keras · error · ValueError
Vocabulary file {vocabulary} does not exist.
Error message
Vocabulary file {vocabulary} does not exist. What it means
set_vocabulary treats a string argument as a filesystem path to a vocabulary file. Here `tf.io.gfile.exists` reports that path as missing, so the layer cannot load it.
Source
Thrown at keras/src/layers/preprocessing/index_lookup.py:458
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}'"
)
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()` "View on GitHub (pinned to 7a34a03db6)
Solutions
- Check the path before calling: `tf.io.gfile.exists(path)`.
- Use paths relative to the project root, or ship the vocabulary file with the deployment artifact.
- If you meant to pass tokens, pass a list of strings, not a single string.
Example fix
# before
layer.set_vocabulary('vocab.txt') # a str is treated as a file path
# after
tokens = [line.strip() for line in open('vocab.txt')]
layer.set_vocabulary(tokens) Defensive patterns
Strategy: validation
Validate before calling
import tensorflow as tf
if isinstance(vocab, str):
if not tf.io.gfile.exists(vocab):
raise FileNotFoundError(vocab)
layer.set_vocabulary(vocab) Type guard
def is_vocab_file(p) -> bool:
import tensorflow as tf
return isinstance(p, str) and tf.io.gfile.exists(p) Try / catch
try:
layer.set_vocabulary(path)
except ValueError as e:
raise FileNotFoundError('vocabulary file missing: %s' % path) from e Prevention
- Never pass a single string when you mean a single token — use a list.
- Ship vocabulary files inside the deployment artifact and use relative paths.
When it happens
Trigger: `layer.set_vocabulary('/data/vocab.txt')` where the path is wrong, lives on another machine, or is not mounted in the container; also hit when a bare token string is passed by accident, since a str is always treated as a path, never as one token.
Common situations: Absolute paths baked into saved models that don't exist on the loading machine; vocabulary file not packaged with the deployment.
Understand the failure class
Background: "File not found" and ENOENT errors: why libraries can't find a file that should exist — this error's family across 50 libraries.
Related errors
- Cannot set an empty vocabulary. Received: vocabulary={vocabu
- The passed vocabulary has at least one repeated term. Please
- Found reserved mask token at unexpected location in `vocabul
- Found reserved OOV token at unexpected location in `vocabula
- Attempted to set a vocabulary larger than the maximum vocab
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/f0ade3a5b93e52f8.
Report an issue: GitHub.