Skip to main content

How to Predict a Missing Word with a Hugging Face Fill-Mask Pipeline

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

Models such as BERT learn language by guessing words hidden in sentences. A Hugging Face fill-mask pipeline lets you play that game with them: you hide a word behind a special mask token, and the model suggests what goes there, each with a score. This guide asks DistilBERT what someone drinks every morning, with Transformers 5.26.0 on Python 3.14.

Syntax​

Syntax
from transformers import pipeline

unmasker = pipeline("fill-mask", model="<model>", revision="<commit>")
guesses = unmasker("<text with one mask token>", top_k=<number>)
PartWhat it means
"fill-mask"The task: guess the hidden word
model="<model>"A model trained to fill masks, as <owner>/<name> on the Hugging Face Hub
revision="<commit>"The exact version of the model, so it can't change under you
<text with one mask token>Your sentence, with the model's mask token where the word is missing
top_k=<number>How many guesses to return; 5 if you leave it out

Each model has its own mask token: BERT models use [MASK], RoBERTa models <mask>. unmasker.tokenizer.mask_token tells you which. The pipeline's documentation says it works only with models "trained with a masked language modeling objective".

The Example​

The sentence is "I drink a cup of [MASK] every morning." The model is distilbert/distilbert-base-uncased, a smaller, faster version of BERT trained on English books and Wikipedia. It's Apache-2.0 licensed.

Prerequisites​

  • uv. This guide uses uv 0.13.10. The project uses Python 3.14, which uv downloads if needed.
  • About 270 MB for the model, downloaded on the first run.

Step 1: Create the Project​

Terminal
uv init --python 3.14 fill-mask-demo
cd fill-mask-demo
uv add transformers torch

That's a project with Transformers 5.26.0 and PyTorch 2.15.0.

Step 2: Write the Script​

fill_mask.py
from transformers import pipeline

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

print(unmasker.tokenizer.mask_token)
for guess in unmasker("I drink a cup of [MASK] every morning.", top_k=3):
print(f"{guess['token_str']:<8}{guess['score']:.3f} {guess['sequence']}")

The fill_mask.py script in a macOS-style code window: a fill-mask pipeline with the distilbert/distilbert-base-uncased model pinned to a commit, a print of the tokenizer's mask token, and a loop over the top three guesses for I drink a cup of [MASK] every morning that prints each guess, its score and the completed sentence.

The script first prints the model's mask token, to be sure the sentence uses the right one. Then it asks for three guesses. Each guess is a dictionary, and the loop prints three of its keys:

KeyWhat it is
token_strThe guessed word
scoreHow likely the model thinks it is, from 0 to 1
sequenceThe whole sentence with the guess filled in

A fourth key, token, holds the guessed word's number in the model's vocabulary.

Step 3: Run It​

Terminal
uv run python fill_mask.py
Output
Loading weights: 100%|██████████████████████████████████████████| 105/105 [00:00<00:00, 9167.02it/s]
[MASK]
coffee 0.536 i drink a cup of coffee every morning.
tea 0.183 i drink a cup of tea every morning.
water 0.069 i drink a cup of water every morning.

The terminal after uv run python fill_mask.py: the mask token [MASK], then three guesses: coffee 0.536, tea 0.183 and water 0.069, each with the sentence i drink a cup of coffee, tea or water every morning.

The mask token is [MASK], as the sentence assumed. The model's favourite is coffee (0.536), then tea and water. The sentences are in lowercase because this model is "uncased": it lowercases all text before reading it. On the first run, download progress bars for the model's files come before these lines. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Writing <mask> for a BERT modelThe pipeline finds no mask and raises PipelineException: No mask_token ([MASK]) found on the input: see the fixPrint unmasker.tokenizer.mask_token and use that
Putting two masks in one sentenceEach mask gets its own list of guesses, chosen separately: "I drink a [MASK] of [MASK] every morning." gave lot and glass for the first, wine and coffee for the secondMask one word at a time

FAQ​

Is the top guess always right?​

No. It's the word the model finds most likely, which can be wrong. Asked to complete "The capital of France is [MASK].", this same model guessed marseille (0.143), nantes (0.090) and toulouse (0.088), not Paris. Bigger models usually do better.

Can I limit the guesses to words I choose?​

Yes. With targets=["coffee", "tea"], the same sentence returned only coffee (0.536) and tea (0.183). The documentation notes that words missing from the model's vocabulary are split into pieces and only the first piece is scored.

What is this useful for?​

Mostly for seeing what a model has learned, and as the first step in training BERT-like models. To use one for a task, such as sorting messages, you fine-tune it.

Conclusion​

A fill-mask pipeline with distilbert/distilbert-base-uncased filled "I drink a cup of [MASK] every morning." with coffee (0.536), tea (0.183) and water (0.069). unmasker.tokenizer.mask_token gave the mask to write, and top_k=3 chose the number of guesses.

Additional Resources​