Verifying tokenizer.json bytes before comparing WordPiece outputs
Hash downloaded JSON bytes and compare to pinned site asset digests; distinguish model repo revision from local file integrity
Before comparing WordPiece output across two runs, compare the bytes of the tokenizer.json those runs actually read. If the files differ, the token-ID comparison that follows is measuring the wrong thing.
This is the failure mode that costs the most time: two engineers pin google-bert/bert-base-uncased@86b5e0934494bd15c9632b12f734a8a67f723594, call AutoTokenizer.from_pretrained(..., revision=..., use_fast=True), pass the same string, and get different input_ids. The first instinct is to suspect WordPiece. The pin is a claim about a repository revision. It is not a claim about the file on either machine’s disk. Between the Hub and the fast tokenizer’s parser sit a cache directory, a download, and whatever wrote to that cache last.
This article assumes Transformers 4.56.2. from_pretrained(..., revision=FULL_COMMIT) resolves the commit, but the resolved artifact is cached — and once a file is in the cache at that commit’s path, later calls reuse it. A partially written download, a corporate proxy that re-serializes JSON, a line-ending conversion on checkout, or a patched copy saved back into the cache all leave the revision string untouched while changing the numbers the tokenizer returns.
The two identities you have to keep apart
Repository revision identity answers “which commit did the Hub serve?” File integrity answers “are the bytes the tokenizer parsed the bytes that commit contains?” You need both, and the second is the one you can check locally.
Hashing the raw file — before json.loads, before any normalization — is what makes the answer reproducible. Hash a parsed object after re-serializing it and you get a different digest for semantically identical content, which is worse than useless. Hash the bytes you downloaded and you get a value you can compare against a value recorded elsewhere, and a stable string to log next to any token-ID comparison.
A digest match proves identity, not correctness, and a digest you cannot compare against proves nothing on its own. Without a recorded digest, the cheapest useful check is self-consistency: does the vocabulary inside the bytes agree, entry for entry, with the vocabulary the loaded tokenizer exposes?
The check, in order
Download the pinned file at the full commit and hash the raw bytes. Parse those same bytes and read model.vocab. Load the fast tokenizer at the same revision and enumerate get_vocab(). Confirm the file’s entries agree with the loaded tokenizer. Only then encode your probe string, and record the digest next to the token IDs.
The script below does that for the English uncased model. It prints the digest rather than asserting a value, because a digest asserted from a run this article did not perform would be fabricated.
import hashlib
import json
from pathlib import Path
from huggingface_hub import hf_hub_download
from transformers import AutoTokenizer
MODEL_ID = "google-bert/bert-base-uncased"
REVISION = "86b5e0934494bd15c9632b12f734a8a67f723594"
pinned_path = hf_hub_download(
repo_id=MODEL_ID,
filename="tokenizer.json",
revision=REVISION,
)
pinned_bytes = Path(pinned_path).read_bytes()
digest = hashlib.sha256(pinned_bytes).hexdigest()
print(f"sha256(tokenizer.json) = {digest}")
print(f"byte length = {len(pinned_bytes)}")
doc = json.loads(pinned_bytes)
file_vocab = doc["model"]["vocab"]
print(f"entries in model.vocab = {len(file_vocab)}")
tok = AutoTokenizer.from_pretrained(MODEL_ID, revision=REVISION, use_fast=True)
loaded_vocab = tok.get_vocab()
print(f"entries in loaded tokenizer = {len(loaded_vocab)}")
mismatches = {
token: (file_id, loaded_vocab.get(token))
for token, file_id in file_vocab.items()
if loaded_vocab.get(token) != file_id
}
print(f"entries where loaded id != file id: {len(mismatches)}")
assert not mismatches, list(mismatches.items())[:5]
probe = "the quick brown fox"
content_ids = tok(probe, add_special_tokens=False)["input_ids"]
model_ids = tok(probe, add_special_tokens=True)["input_ids"]
print(f"content tokens = {len(content_ids)}")
print(f"model-input tokens = {len(model_ids)}")
print(f"model input ids = {model_ids}")
print(f"decoded content round-trips = {tok.decode(content_ids) == probe}")
enc = tok(probe, return_offsets_mapping=True)
print(f"offsets = {enc['offset_mapping']}")
hf_hub_download and from_pretrained share the same cache and the same commit-keyed path, so under a normal cache configuration the loaded fast tokenizer is reading the file you hashed. If you override the cache, pass tokenizer_file= explicitly, or run with local_files_only=True, that equivalence is not automatic — check the path you hashed against the one the tokenizer resolved.
Reading what you see
If the digest matches a value recorded from a trusted run, the bytes are the same and you can move on to comparing token IDs. If it does not, look at byte length first: a truncated download usually shows up as a shorter file that fails json.loads outright, or parses into a vocabulary with fewer entries than expected. tokenizer.json for bert-base-uncased carries 30,522 vocabulary entries; the multilingual cased model at 3f076fdb1ab68d5b2880cb87a0886f315b8146f8 carries 119,547. A count that does not match the model you believe you loaded is a strong signal that the file is not the file you pinned.
A digest mismatch with a correct length and a correct vocabulary count is more subtle. That pattern is consistent with re-serialization — semantically equal JSON, different bytes — or with a genuine edit somewhere in the file. The key-by-key comparison distinguishes the two: if every vocabulary entry maps to the same integer as the loaded tokenizer, the mapping is intact even when the digest differs, and you are looking at formatting rather than meaning.
If the assertion fires, stop comparing outputs. The token-ID comparison is downstream of a vocabulary question, and the vocabulary has just failed its check.
Counts that mean what you think
When you compare token counts, name which count you mean. add_special_tokens=False gives content tokens: the pieces the WordPiece model itself produced. add_special_tokens=True gives model-input tokens, which for BERT includes [CLS] and [SEP] — a constant offset on a single-sequence input, and easy to mistake for a tokenizer difference when one side forgot the argument.
Also separate unknown tokens from fragmentation. An unknown token is one vocabulary entry — [UNK], one ID, standing in for a character or word the greedy matcher could not resolve. Fragmentation is the matcher succeeding: one input string becomes several subword IDs. Both raise the count. Only offsets from the fast tokenizer, or a decode of the content IDs, tell you which happened.
Every count here is scoped to bert-base-uncased at the revision above, under Transformers 4.56.2. A count quoted without its revision and tokenizer version is not a comparable number.
What this evidence does not cover
Byte verification proves that the file you hashed is the file a trusted run hashed. It does not prove that the reference output you are comparing against came from that same revision, nor that both sides used the same add_special_tokens, truncation, padding, or added-token settings. A digest match on tokenizer.json says nothing about a tokenizer_config.json that adds tokens at load time, and nothing about downstream casing behavior. Treat the digest as one precondition, not a certificate.
The check also depends on Transformers 4.56.2’s behavior: how from_pretrained resolves, caches, and reports the fast backend. A different Transformers version can resolve the same pin differently. Verified: 2026-10-05, against google-bert/bert-base-uncased revision 86b5e0934494bd15c9632b12f734a8a67f723594 and transformers==4.56.2.
Re-checking when the pin or the dependency moves
When the Hugging Face commit changes, replace REVISION with the full new SHA — a branch name is not a pin — and record the new digest alongside it. When you upgrade Transformers, re-run the script before reusing any old token-ID comparison: the tokenizer JSON can be byte-identical while the loading path around it changes. Keep the digest in the same log line as the token IDs you compare, so a future mismatch points at either the bytes or the code, never at both at once.
Sources
- google-bert/bert-base-uncased tokenizer.json at 86b5e093
Pinned WordPiece vocabulary and normalization settings for the English uncased model.
- google-bert/bert-base-multilingual-cased tokenizer.json at 3f076fdb
Pinned WordPiece vocabulary and normalization settings for the multilingual cased model.
- Hugging Face Transformers v4.56.2 tokenizer API
Versioned reference for encoding options, output fields, and fast-tokenizer alignment methods.
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.