Skip to content
wordpiece.org

Probing WordPiece behavior on source-code identifiers

Test snake_case, camelCase, package paths, and mixed digits; avoid claiming a code-specialized tokenizer

Published 8 min read Verified

The problem this article addresses is narrow and recurring: you have a model whose tokenizer is WordPiece, you feed it source-code identifiers such as getUserID, snake_case_name, com.example.pkg.Class, or v2beta1, and you need to know what the tokenizer actually did. Not what a code-specialized tokenizer would do, and not what a general-purpose LLM embedding would do — only what the pinned WordPiece model at a specific revision produces.

The distinction matters because two very different things can happen to an identifier. A subword can be fragmented — split into several known vocabulary pieces, each of which is a real token — or it can be unknown — mapped to [UNK] because no vocabulary piece matched at that position. Fragmentation is often informative: getUserID splitting into get, user, ##id preserves recoverable structure. Unknown tokens destroy information: the original characters are gone. A diagnostic that reports only “the identifier is 3 tokens” hides which of these occurred.

What WordPiece does to an identifier

WordPiece is a greedy longest-match-first algorithm over a fixed vocabulary, with a continuation prefix (## in BERT) marking subword pieces that are not word-initial. The pinned google-bert/bert-base-uncased tokenizer at revision 86b5e0934494bd15c9632b12f734a8a67f723594 has a known vocabulary size of 30,522; the pinned google-bert/bert-base-multilingual-cased tokenizer at 3f076fdb1ab68d5b2880cb87a0886f315b8146f8 has 119,547. Both are WordPiece models. Neither was designed for code, and neither claims to be.

For source-code probing, three behaviors are worth isolating:

  1. Splitting on a separator. snake_case_name contains underscores. Whether the tokenizer treats _ as a boundary depends on normalization and on whether _ and its neighbors exist as vocabulary pieces.
  2. Case sensitivity. The uncased model lowercases and strips accents during normalization; the cased model does not. getUserID therefore behaves differently across the two.
  3. Digit and punctuation runs. v2beta1 and com.example.pkg mix letters, digits, and dots. The greedy matcher may consume 2 as part of a piece, or leave it as a separate token, or fall back to [UNK].

A self-contained diagnostic

The following script loads one pinned tokenizer and prints, per identifier, the token strings, the token IDs, whether any [UNK] appears, and the offset mapping from the fast tokenizer. Offsets let you see which original character range each token covers — the cleanest way to distinguish fragmentation from unknown substitution. Run it locally; the exact output depends on the pinned files and your installed Transformers version.

# transformers==4.56.2
from transformers import AutoTokenizer

MODEL = "google-bert/bert-base-uncased"
REVISION = "86b5e0934494bd15c9632b12f734a8a67f723594"

tok = AutoTokenizer.from_pretrained(
    MODEL,
    revision=REVISION,
    use_fast=True,
)

identifiers = [
    "getUserID",
    "snake_case_name",
    "com.example.pkg.Class",
    "v2beta1",
    "HTTPResponseCode",
]

unk_id = tok.unk_token_id

for ident in identifiers:
    enc = tok(ident, add_special_tokens=False, return_offsets_mapping=True)
    pieces = tok.convert_ids_to_tokens(enc["input_ids"])
    offsets = enc["offset_mapping"]
    has_unk = unk_id in enc["input_ids"]
    print(f"\nidentifier: {ident!r}")
    print(f"  pieces   : {pieces}")
    print(f"  ids      : {enc['input_ids']}")
    print(f"  offsets  : {offsets}")
    print(f"  contains [UNK]: {has_unk}")
    assert len(pieces) == len(offsets) == len(enc["input_ids"])

Two things to note about the API. add_special_tokens=False excludes [CLS] and [SEP], so the printed token list reflects only the identifier’s content tokens. If you later feed these into a model, the model-input token count will be two higher for a single-sequence classification input (or three higher for a pair), and that difference is not a tokenization artifact — it is the special-token wrapper. Keep the two counts separate in any report. return_offsets_mapping=True is available on fast tokenizers; if you set use_fast=False, this key is absent and you must use a slow-tokenizer alignment path instead.

Reading the observations

When you run this, expect the uncased model to lowercase the identifier before matching: getUserID becomes getuserid internally. Whether that surfaces as one piece, several pieces, or [UNK] depends on what the 30,522-entry vocabulary contains. The cased model, pinned at the multilingual revision, preserves case and will generally match differently — but “generally” is not a claim about counts; you must observe it.

Interpret the three signals independently:

  • Fragmentation without [UNK]: every character of the identifier is covered by a known piece. The offsets will tile the string with no gaps (except possibly around normalized characters). Information is preserved in subword form.
  • [UNK] present: at least one position had no match. The offset for that token may cover a single character or a run; the original characters are not recoverable from the ID alone. This is the failure mode that silently degrades downstream features.
  • Separator behavior: for snake_case_name, look at whether _ appears inside a piece or as its own token. The answer is vocabulary-dependent and normalization-dependent, not a property of WordPiece in the abstract.

Do not conflate token count with token quality. A two-token result where one token is [UNK] is worse for code-aware analysis than a four-token result where every piece is known and offsets tile cleanly.

Limitations

This procedure probes tokenization only. It says nothing about whether the model has learned code semantics, nothing about downstream accuracy, and nothing about any other model. The pinned vocabularies are fixed artifacts: bert-base-uncased at 30,522 entries and bert-base-multilingual-cased at 119,547 entries are the only sizes this article asserts, and only for those exact revisions. A code-specialized tokenizer trained on source repositories would behave differently — but this article does not test one and does not claim BERT is a code tokenizer. Treat every observation as specific to the revision you loaded.

Verified: 2026-10-05. The applicable artifacts are google-bert/bert-base-uncased@86b5e0934494bd15c9632b12f734a8a67f723594 and google-bert/bert-base-multilingual-cased@3f076fdb1ab68d5b2880cb87a0886f315b8146f8, under transformers==4.56.2. The primary evidence is the pinned tokenizer.json files for those revisions and the v4.56.2 tokenizer API documentation. The code block above was not executed for this article; its output is what you obtain locally.

Re-checking when pins move

If the tokenizer file at the pinned revision changes — which should not happen for an immutable commit, but mirror or cache issues can occur — or if you upgrade Transformers, re-run the script and compare the printed pieces and offsets against your previous run. A change in convert_ids_to_tokens output, a shift in offset tiling, or the appearance or disappearance of [UNK] for the same identifier indicates the behavior you documented no longer holds. Record the revision string, the Transformers version, and the observed pieces together; a token count alone is not reproducible evidence. If you switch to the multilingual cased revision, re-run from scratch rather than assuming the uncased results transfer.

Sources

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.

Check it yourself

Open the tokenizer

Every count in this guide comes from a vocabulary pinned by checksum. Paste the same text into the tool and you will get the same output.

Related guides