Checking continuation-prefix lookups in a WordPiece vocabulary
Inspect ordinary and ##-prefixed entries separately and trace a failing lookup without assuming they are interchangeable
A direct vocabulary-membership check for a spelling can reject a piece that the pinned WordPiece model would use. The failure is easy to hide because play and ##play answer different lookup questions: the unprefixed form concerns the beginning of a WordPiece unit, while the prefixed form concerns a later position. The procedure below tests direct keys and tokenizer output separately instead of treating ordinary and continuation entries as interchangeable.
Verified: 2026-10-05. The applicable target is google-bert/bert-base-uncased at revision 86b5e0934494bd15c9632b12f734a8a67f723594, loaded with transformers==4.56.2 and use_fast=True. The verification date applies to the supplied pinned tokenizer.json and Transformers v4.56.2 BERT tokenizer source, not to an execution of the script in this article. The script prints its own observations.
Preserve lookup position
Within the pinned BERT WordPiece process, a candidate beginning at the first character of a normalized, pretokenized word is matched under its literal spelling, such as wanted. A candidate beginning later is represented with the continuation prefix, such as ##wanted. The prefix describes position; it is not a cosmetic spelling variant. A hit for wanted therefore does not establish ##wanted, and the reverse hit does not establish an initial match. Do not prepend another ## to a key that already has one, or strip it and reuse the result in the other position.
At each character, the BERT WordPiece routine selects the longest available match and then continues from the resulting boundary. Direct membership of an entire compound spelling is therefore only a coarse check. Its absence does not establish that the characters lack smaller matches. Whitespace and punctuation are handled before WordPiece, so a continuation key should not be fabricated across a pretokenized boundary. Special tokens and [UNK] are separate entries. Normalize the probe before drawing conclusions: for this uncased tokenizer, the raw input spelling need not be the literal vocabulary key.
The following script inspects ordinary and ##-prefixed candidates independently, then asks the same tokenizer instance to encode several long ASCII probes without padding or truncation.
# Python 3
# Install in a clean environment:
# python -m pip install transformers==4.56.2
from importlib.metadata import version
from transformers import AutoTokenizer
MODEL_ID = "google-bert/bert-base-uncased"
FULL_COMMIT = "86b5e0934494bd15c9632b12f734a8a67f723594"
assert version("transformers") == "4.56.2"
print("tokenizers dependency:", version("tokenizers"))
tokenizer = AutoTokenizer.from_pretrained(
MODEL_ID,
revision=FULL_COMMIT,
use_fast=True,
)
assert tokenizer.is_fast
print("tokenizer class:", type(tokenizer).__name__)
vocab = tokenizer.get_vocab()
print("\nDirect piece-key lookups")
for form in ("un", "wanted", "play", "ing"):
print(
f"{form!r}: "
f"ordinary_id={vocab.get(form)!r}, "
f"continuation_id={vocab.get('##' + form)!r}"
)
probes = (
"unwanted",
"internationalization",
"electroencephalography",
"replaying",
)
rows = []
for text in probes:
pieces = tokenizer.tokenize(text)
content_ids = tokenizer.encode(text, add_special_tokens=False)
model_input_ids = tokenizer(
text,
add_special_tokens=True,
padding=False,
truncation=False,
return_attention_mask=False,
)["input_ids"]
assert pieces
assert content_ids == tokenizer.convert_tokens_to_ids(pieces)
assert model_input_ids == [
tokenizer.cls_token_id,
*content_ids,
tokenizer.sep_token_id,
]
for index, piece in enumerate(pieces):
if piece != "[UNK]":
assert piece in vocab
assert piece.startswith("##") == (index > 0)
row = {
"text": text,
"ordinary_id": vocab.get(text),
"continuation_id": vocab.get("##" + text),
"pieces": pieces,
"content_token_count": len(content_ids),
"model_input_token_count": len(model_input_ids),
}
rows.append(row)
print("\n", row)
failing_whole_form = next(
(
row
for row in rows
if row["ordinary_id"] is None
and row["continuation_id"] is None
and row["content_token_count"] > 1
and "[UNK]" not in row["pieces"]
),
None,
)
assert failing_whole_form is not None, (
"No whole-form lookup failure with successful fragmentation was found."
)
print("\nTraced whole-form failure followed by valid pieces:")
print(failing_whole_form)
Interpret the two kinds of evidence
The first four records report literal dictionary membership. An integer is the stored token ID; None means that exact string was not found. These lines are not predictions about which token the tokenizer will select. The probe records show what the tokenizer actually selects after its normalization and pre-tokenization stages.
A missing whole-form key accompanied by several non-[UNK] pieces is a failed coarse lookup, not failed tokenization. The final assertion makes that condition explicit for at least one supplied long probe. In such a row, the matcher used a sequence of individual vocabulary entries even though neither the ordinary nor the continuation spelling of the complete probe was present. By contrast, a direct key can exist without being selected: whole-unit greediness, pre-tokenization, or the current position may favor a different sequence. Even a hit for ##wanted would not imply a hit for ##unwanted; continuation status cannot be propagated to an arbitrary longer spelling.
The per-piece assertions provide a second check. Every emitted non-unknown piece must exist in the loaded vocabulary, and only pieces after the first must begin with ##. A failure points to a different vocabulary source, a nonstandard added token, or a dependency/serialization mismatch. It should not be repaired by ignoring the prefix rule.
Separate token counts and failure modes
tokenize() and encode(add_special_tokens=False) expose content tokens. They do not include the model’s sequence-level classification and separator tokens. The second encoding in the script represents one unpadded, untruncated model-input sequence and therefore adds [CLS] and [SEP], making its token count exactly two greater than the content-token count. This distinction is diagnostic bookkeeping, not a statement about model capacity or inference behavior. Padding, truncation, a token pair, or batching would change sequence-level accounting and require separately specified checks.
Fragmentation and unknownness are also different. Fragmentation means that a unit was covered by multiple valid pieces. [UNK] is the fallback when a pretokenized unit cannot be covered by the WordPiece matching process. A missing compound key may lead either to successful fragmentation or to [UNK]; the emitted piece list distinguishes them. If [UNK] appears, investigate normalization, character coverage, and pre-tokenized boundaries rather than assuming that an absent ##-prefixed whole-form key is the only cause.
Scope and limitations
These observations apply only to google-bert/bert-base-uncased@86b5e0934494bd15c9632b12f734a8a67f723594 under Transformers 4.56.2’s fast-tokenizer path. The ASCII probes avoid making claims about Unicode normalization. They also omit paired sequences, padding, truncation, batching, and user-added tokens. Results must not be transferred to another checkpoint or vocabulary merely because it also uses WordPiece.
The pinned JSON is the vocabulary and serialization authority, while the linked Python source documents BERT tokenization semantics. The script actually exercises the installed fast implementation, which also depends on the separately installed tokenizers package; Transformers 4.56.2 alone does not record that transitive version in the output. The code therefore prints it. Any disagreement should first be checked against the exact pinned JSON and the recorded dependency versions, not resolved by accepting both prefixed and unprefixed keys interchangeably.
Re-check after a file or dependency change
Use a clean Python 3 environment, install transformers==4.56.2, and rerun the script unchanged. Retain its stdout, the reported tokenizers version, tokenizer class name, and a hash of the downloaded tokenizer.json. If the model commit changes, update both model constants only after reviewing the new pinned file; the previous observations no longer establish behavior for that revision. If only the Transformers or tokenizers dependency changes, keep the model constants fixed and compare direct key hits, emitted pieces, special-token handling, and both token counts against the saved run. A changed file at the same expected hash is an artifact-integrity issue, not a tokenizer behavior to investigate by weakening the assertions.
Sources
- google-bert/bert-base-uncased tokenizer.json at 86b5e093
Pinned WordPiece vocabulary and normalization settings for the English uncased model.
- Transformers v4.56.2 BertTokenizer source
Versioned Python implementation of BERT basic and WordPiece tokenization.
Token counts in this article were produced by the tokenizers pinned on the sources page and are checked by the site's test suite. If a count ever stops matching the pinned vocabulary, the build fails.