keras-team/keras · error · ValueError
Cannot set an empty vocabulary. Received: vocabulary={vocabu
Error message
Cannot set an empty vocabulary. Received: vocabulary={vocabulary} What it means
An empty vocabulary would leave the lookup table with zero entries and break every downstream index computation, so set_vocabulary rejects arrays of size 0 instead of silently continuing.
Source
Thrown at keras/src/layers/preprocessing/index_lookup.py:493
"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
# entire function to operate on tensors and convert vocabulary to a
# tensor here.
if tf.is_tensor(vocabulary):
vocabulary = self._tensor_vocab_to_numpy(vocabulary)
elif isinstance(vocabulary, (list, tuple)):
vocabulary = np.array(vocabulary)
if tf.is_tensor(idf_weights):
idf_weights = idf_weights.numpy()
elif isinstance(idf_weights, (list, tuple)):
idf_weights = np.array(idf_weights)
if vocabulary.size == 0:
raise ValueError(
"Cannot set an empty vocabulary. "
f"Received: vocabulary={vocabulary}"
)
oov_start = self._oov_start_index()
token_start = self._token_start_index()
special_tokens = [self.mask_token] * oov_start + [
self.oov_token
] * self.num_oov_indices
found_special_tokens = np.array_equal(
special_tokens, vocabulary[:token_start]
)
if found_special_tokens:
tokens = vocabulary[token_start:]
else:
tokens = vocabulary
repeated_tokens = self._find_repeated_tokens(tokens)View on GitHub (pinned to 7a34a03db6)
Solutions
- Check `len(vocab) > 0` before calling set_vocabulary.
- If the vocabulary came from adapt, inspect the input dataset — it is yielding nothing.
Example fix
# before layer.set_vocabulary(vocab) # after assert len(vocab) > 0, 'vocabulary is empty' layer.set_vocabulary(vocab)
Defensive patterns
Strategy: validation
Validate before calling
if len(vocab) == 0:
raise ValueError('refusing to set an empty vocabulary')
layer.set_vocabulary(vocab) Type guard
def has_tokens(v) -> bool:
return len(v) > 0 Prevention
- Assert non-empty vocabularies right after generation or adapt.
- Log dataset sizes before adapting so empty slices are visible early.
When it happens
Trigger: `set_vocabulary(np.array([]))` or an empty list; adapt() on a dataset slice that yields no tokens; parsing a vocabulary file to zero lines.
Common situations: Adapting on a fully filtered-out dataset; empty vocabulary files; conditional data paths that occasionally produce no rows.
Related errors
- Vocabulary file {vocabulary} does not exist.
- 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/7341f8ac2282f974.
Report an issue: GitHub.