Skip to main content

Fixed: "PipelineException: No mask_token ([MASK]) found on the input"

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A Hugging Face fill-mask pipeline stopped with PipelineException: No mask_token ([MASK]) found on the input. The text marked the missing word with <mask>, but this model's mask token is [MASK], so the pipeline found nothing to fill. Writing the text with the tokenizer's own mask_token makes it work with any fill-mask model.

What I Was Doing​

I was asking distilbert/distilbert-base-uncased to guess a missing word, in Transformers 5.26.0 on Python 3.14, and checking a mistake that's easy to make: marking the missing word with <mask>, the mask token RoBERTa models use, in a text for this BERT-style model:

mask_error.py
from transformers import pipeline

unmasker = pipeline(
"fill-mask",
model="distilbert/distilbert-base-uncased",
revision="12040accade4e8a0f71eabdb258fecc2e7e948be",
)

print(unmasker("The sky is <mask> today.", top_k=3))
Terminal
uv run python mask_error.py

What I Expected​

Three guesses for the missing word, each with its score and the completed sentence.

What I Got​

A traceback. Its first lines:

Output (first lines)
Loading weights: 100%|██████████████████████████████████████████| 105/105 [00:00<00:00, 8412.64it/s]
Traceback (most recent call last):
File "/Users/you/source/fill-mask-demo/mask_error.py", line 9, in <module>
print(unmasker("The sky is <mask> today.", top_k=3))
~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

And its last lines:

Output (last lines)
File "/Users/you/source/fill-mask-demo/.venv/lib/python3.14/site-packages/transformers/pipelines/fill_mask.py", line 94, in _ensure_exactly_one_mask_token
raise PipelineException(
...<3 lines>...
)
transformers.pipelines.base.PipelineException: No mask_token ([MASK]) found on the input

The terminal after uv run python mask_error.py: a traceback from the line that calls unmasker with The sky is mask today, through the fill-mask pipeline's preprocess and _ensure_exactly_one_mask_token, ending in transformers.pipelines.base.PipelineException: No mask_token ([MASK]) found on the input.

The message names the token the pipeline was looking for: [MASK]. My text had <mask>.

What I Checked​

How the pipeline looks for the mask. In transformers/pipelines/fill_mask.py, the pipeline searches the tokenized text for the tokenizer's mask token, by its number in the vocabulary, and raises this exception when it finds none:

transformers/pipelines/fill_mask.py (excerpt, 5.26.0)
def get_masked_index(self, input_ids: GenericTensor) -> np.ndarray:
masked_index = torch.nonzero(input_ids == self.tokenizer.mask_token_id, as_tuple=False)
return masked_index

def _ensure_exactly_one_mask_token(self, input_ids: GenericTensor) -> np.ndarray:
masked_index = self.get_masked_index(input_ids)
numel = np.prod(masked_index.shape)
if numel < 1:
raise PipelineException(
"fill-mask",
self.model.base_model_prefix,
f"No mask_token ({self.tokenizer.mask_token}) found on the input",
)

To this tokenizer, <mask> is just ordinary characters, split into ordinary tokens, so none of them is the mask token.

Which mask token each model uses. It depends on how the model was trained. BERT-style models, such as this DistilBERT model and answerdotai/ModernBERT-base, use [MASK]. FacebookAI/roberta-base uses <mask>: it's one of the special tokens in its tokenizer.json. Examples written for one kind of model fail with the other.

Why It Happens​

A fill-mask model only knows the one mask token it was trained with. The pipeline looks for exactly that token, and a mask written for another model looks like normal text to it.

The Fix​

Don't type the mask token: take it from the pipeline's tokenizer and put it into the text:

mask_fixed.py
from transformers import pipeline

unmasker = pipeline(
"fill-mask",
model="distilbert/distilbert-base-uncased",
revision="12040accade4e8a0f71eabdb258fecc2e7e948be",
)

mask = unmasker.tokenizer.mask_token
for guess in unmasker(f"The sky is {mask} today.", top_k=3):
print(guess["sequence"])

The fixed mask_fixed.py in a macOS-style code window: the fill-mask pipeline with distilbert/distilbert-base-uncased pinned to a commit, mask set to unmasker.tokenizer.mask_token, and a loop over the top three guesses for an f-string sentence with the mask inside, printing each completed sentence.

  • unmasker.tokenizer.mask_token is the mask token of the model the pipeline loaded, here [MASK].
  • The f before the string puts that token where {mask} is, so the same line works with a model whose mask token is <mask>.
  • guess["sequence"] is the sentence with the guess filled in.

After the Fix​

Terminal
uv run python mask_fixed.py
Output
Loading weights: 100%|█████████████████████████████████████████| 105/105 [00:00<00:00, 13979.68it/s]
the sky is visible today.
the sky is blue today.
the sky is clear today.

The terminal after the fix: the weights loading, then three completed sentences: the sky is visible today, the sky is blue today and the sky is clear today.

Three guesses: visible, blue and clear. The sentences are in lowercase because this is an uncased model: its tokenizer_config.json sets "do_lower_case": true. The commands work unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Copying an example written for another modelIts mask token may not be your model'sUse unmasker.tokenizer.mask_token
Using a model that wasn't trained to fill masksThe pipeline's documentation says it's for "models that have been trained with a masked language modeling objective"Choose a model listed under the Fill-Mask task on the Hub

Troubleshooting​

SymptomLikely causeFix
No mask_token (<mask>) found on the inputThe same mistake the other way round: [MASK] in the text, a model whose mask token is <mask>Take the token from unmasker.tokenizer.mask_token
A text with two masks returns a list of listsOne list of guesses per maskLoop over the masks, then over each mask's guesses

FAQ​

How do I see a model's mask token without running a pipeline?​

Some models list it in their tokenizer files on the Hub: ModernBERT's tokenizer_config.json has "mask_token": "[MASK]". Others don't: this DistilBERT model's tokenizer_config.json holds only do_lower_case and model_max_length, and its tokenizer uses its type's default. Printing unmasker.tokenizer.mask_token works for every model.

Why does the message say [MASK] when my text had <mask>?​

It names the token the pipeline was looking for, the model's own mask token, not what your text contained.

Conclusion​

PipelineException: No mask_token ([MASK]) found on the input meant the text had no [MASK]: it used <mask>, which is RoBERTa's mask token, not DistilBERT's. Building the text with unmasker.tokenizer.mask_token filled the gap with "visible", "blue" and "clear".

Additional Resources​