Skip to main content

Fixed: "Ignoring clean_up_tokenization_spaces=True for BPE tokenizer TokenizersBackend"

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A fill-mask pipeline with ModernBERT worked, but printed Ignoring clean_up_tokenization_spaces=True for BPE tokenizer TokenizersBackend first. The model's tokenizer settings ask for a text cleanup that Transformers 5 deliberately skips for this kind of tokenizer, and the warning says so. Loading the tokenizer yourself with clean_up_tokenization_spaces=False gives the same results without the warning.

What I Was Doing​

I was predicting a missing word with answerdotai/ModernBERT-base, a fill-mask model, in Transformers 5.26.0 on Python 3.14:

fill_mask.py
from transformers import pipeline

unmasker = pipeline(
"fill-mask",
model="answerdotai/ModernBERT-base",
revision="8949b909ec900327062f0ebf497f51aef5e6f0c8",
)

for guess in unmasker("The capital of France is [MASK]."):
print(f"{guess['token_str']:<10}{guess['score']:.3f}")
Terminal
uv run python fill_mask.py

What I Expected​

The pipeline's five guesses for the missing word, each with its score.

What I Got​

Output
Loading weights: 100%|██████████████████████████████████████████| 137/137 [00:00<00:00, 4434.72it/s]
[transformers] Ignoring clean_up_tokenization_spaces=True for BPE tokenizer TokenizersBackend. The clean_up_tokenization post-processing step is designed for WordPiece tokenizers and is destructive for BPE (it strips spaces before punctuation). Set clean_up_tokenization_spaces=False to suppress this warning, or set clean_up_tokenization_spaces_for_bpe_even_though_it_will_corrupt_output=True to force cleanup anyway.
Paris 0.923
Lyon 0.036
Nancy 0.023
Nice 0.006
Orleans 0.003

The terminal after uv run python fill_mask.py: the weights loading, a warning starting Ignoring clean_up_tokenization_spaces=True for BPE tokenizer TokenizersBackend, then the guesses Paris 0.923, Lyon 0.036, Nancy 0.023, Nice 0.006 and Orleans 0.003.

The guesses came, and they're good. The warning came before them. The guesses also start with a space ( Paris): that's how this tokenizer stores a word that follows a space, and it's not what the warning is about.

What I Checked​

Where the setting comes from. I never set clean_up_tokenization_spaces. The model's tokenizer_config.json on the Hub does:

tokenizer_config.json (excerpt, answerdotai/ModernBERT-base)
"clean_up_tokenization_spaces": true,

What the cleanup does. The warning comes from the tokenizer's decode, in transformers/tokenization_utils_tokenizers.py, which the fill-mask pipeline calls to turn each guessed token back into text. When the setting is on and the tokenizer is a BPE (byte-pair encoding) tokenizer, as ModernBERT's is, Transformers skips the cleanup and warns once:

transformers/tokenization_utils_tokenizers.py (excerpt, 5.26.0)
if clean_up_tokenization_spaces:
# Skip cleanup for BPE tokenizers — the cleanup was designed for
# WordPiece tokenizers and is destructive for BPE (it strips
# legitimate spaces before punctuation).
if (
type(self.backend_tokenizer.model).__name__ == "BPE"
and not self.clean_up_tokenization_spaces_for_bpe_even_though_it_will_corrupt_output
):
logger.warning_once(
"Ignoring clean_up_tokenization_spaces=True for BPE tokenizer"
# ...
)
else:
text = self.clean_up_tokenization(text)

Why it was added. The pull request that added it, huggingface/transformers#44915, explains that the cleanup "applies English-specific string replacements" made for BERT-era WordPiece tokenizers, which decoded text as "Hello , world .". BPE tokenizers keep spaces inside their tokens, so for them the cleanup "is actively destructive — it strips legitimate spaces from correctly decoded text. For example, "x != y" becomes "x!= y"." Many models still have the setting on in their configuration, so the library ignores it for BPE tokenizers and warns.

Why It Happens​

ModernBERT's tokenizer configuration turns the cleanup on. Transformers 5 refuses to apply it to a BPE tokenizer, because it would damage the text, and warns that it's ignoring the setting. Nothing is wrong with the results: the warning is about a setting that has no effect.

The Fix​

Load the tokenizer yourself with the setting turned off, and give it to the pipeline:

fill_mask.py
from transformers import AutoTokenizer, pipeline

MODEL = "answerdotai/ModernBERT-base"
REVISION = "8949b909ec900327062f0ebf497f51aef5e6f0c8"

tokenizer = AutoTokenizer.from_pretrained(
MODEL, revision=REVISION, clean_up_tokenization_spaces=False
)
unmasker = pipeline("fill-mask", model=MODEL, revision=REVISION, tokenizer=tokenizer)

for guess in unmasker("The capital of France is [MASK]."):
print(f"{guess['token_str']:<10}{guess['score']:.3f}")

The fixed fill_mask.py in a macOS-style code window: MODEL and REVISION constants for answerdotai/ModernBERT-base, AutoTokenizer.from_pretrained with clean_up_tokenization_spaces=False, a fill-mask pipeline given that tokenizer, and the loop printing each guess and its score.

  • AutoTokenizer.from_pretrained(...) loads the model's tokenizer from the same commit as the model. A setting passed here, clean_up_tokenization_spaces=False, replaces the one in the model's tokenizer_config.json.
  • tokenizer=tokenizer makes the pipeline use this tokenizer instead of loading its own.
  • MODEL and REVISION keep the two loads on the same model and commit.

After the Fix​

Terminal
uv run python fill_mask.py
Output
Loading weights: 100%|█████████████████████████████████████████| 137/137 [00:00<00:00, 15494.25it/s]
Paris 0.923
Lyon 0.036
Nancy 0.023
Nice 0.006
Orleans 0.003

The terminal after the fix: the weights loading, then the same five guesses, Paris 0.923, Lyon 0.036, Nancy 0.023, Nice 0.006 and Orleans 0.003, with no warning.

The warning is gone, and the guesses and scores are exactly the same, because the cleanup was being skipped all along. The commands work unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Setting clean_up_tokenization_spaces_for_bpe_even_though_it_will_corrupt_output=True to make it quietThe warning goes, but the cleanup now runs and can remove real spaces from the textSet clean_up_tokenization_spaces=False
Loading the tokenizer from another commit than the modelThe tokenizer and the model may not matchPass the same revision to both
Stripping the leading space from token_str to "fix" the warningThe space has nothing to do with itUse .strip() only when you display the word

Troubleshooting​

SymptomLikely causeFix
The same warning with TokenizersBackend from another modelIts tokenizer configuration has the same settingThe same fix, with that model's name
The warning comes from your own tokenizer.decode(...) callThe same setting, on a tokenizer you loadedLoad it with clean_up_tokenization_spaces=False, or pass the same argument to decode

FAQ​

Is my output wrong while the warning shows?​

No. The warning says Transformers is ignoring the setting. Here the five guesses and their scores were identical with and without it.

Should the model's configuration be changed instead?​

That's up to each model's authors. The pull request that added the warning chose a fix in the library because the setting is in many models' configurations.

Conclusion​

Ignoring clean_up_tokenization_spaces=True for BPE tokenizer TokenizersBackend meant ModernBERT's tokenizer configuration asks for a cleanup that Transformers 5 skips for BPE tokenizers. Loading the tokenizer with clean_up_tokenization_spaces=False and passing it to the pipeline removed the warning and changed nothing in the results.

Additional Resources​