WordPiece Tokenization Explained: What It Changes and When It Fits

How WordPiece tokenization splits words into subword units, what it changes about your text, how it differs from BPE and Unigram, and when to keep plain text.

WordPiece is a subword tokenization method. It breaks text into pieces that a language model can look up in a fixed vocabulary. It is best known as the tokenizer behind BERT and related models, and Google researchers originally described it for speech recognition work. This guide explains what WordPiece does to text, how it differs from similar methods, and when its output is the right thing to use or share.

Short answer: is WordPiece suitable for your use?

WordPiece fits one purpose: preparing text as input for a model that was trained with the same WordPiece vocabulary. Its main benefit is that a vocabulary of limited size can still represent rare or unseen words, because it builds them from smaller known pieces instead of discarding them. The decisive tradeoff is that the output is a model-specific sequence of fragments and IDs. It is not a readable or portable form of the text. Some configurations also discard information along the way, such as letter case or accents.

Use plain text for anything a person will read, search, edit, store as a record, or pass to a different model or tool. Treat WordPiece output as an intermediate artifact, produced when it is needed and tied to one specific vocabulary.

What WordPiece actually changes

WordPiece does not reverse, mirror, restyle or substitute characters, and it applies no visual or font-like presentation. Words such as reverse, mirror or font do not describe it. It segments text. In typical BERT-style pipelines, the process has three stages:

  • Normalization: depending on configuration, the text may be cleaned, lowercased, and stripped of accents. Uncased BERT-style setups decompose characters and remove combining marks, which changes the characters themselves.
  • Pre-tokenization: the text is split on whitespace and punctuation into word-level chunks. Many implementations also separate each CJK character into its own chunk.
  • Subword segmentation: each chunk is matched against the vocabulary from left to right, always taking the longest piece that exists. Pieces that continue a word are marked with a prefix, usually two hash signs. If a chunk cannot be fully covered by vocabulary pieces, the whole chunk is typically replaced by an unknown token such as [UNK].

Segmentation itself only adds boundaries and markers. Normalization and unknown-token replacement are different: they can permanently remove information that the original string contained.

How the matching works

The sketch below shows the greedy longest-match-first logic in simplified form. Real implementations add details, such as a configurable limit on word length beyond which a word becomes the unknown token.

def wordpiece(word, vocab, unk="[UNK]", prefix="##"):
    pieces = []
    start = 0
    while start < len(word):
        end = len(word)
        match = None
        while start < end:
            piece = word[start:end]
            if start > 0:
                piece = prefix + piece
            if piece in vocab:
                match = piece
                break
            end -= 1
        if match is None:
            return [unk]
        pieces.append(match)
        start = end
    return pieces

An illustrative example

Take a word such as "unaffable" and a vocabulary that contains "un", "##aff" and "##able" but not the full word. The output would be:

unaffable  ->  ["un", "##aff", "##able"]

This matches the mechanism. The first piece has no prefix because it starts the word. Each later piece carries the continuation marker. Removing the markers and joining the pieces gives back the original word. The result depends entirely on the vocabulary, though. A different vocabulary might keep the word whole or split it in other places. This example contains no combining marks or multi-code-point emoji. Those cases depend on normalization settings and vocabulary contents, and an emoji absent from the vocabulary may become the unknown token.

How WordPiece differs from similar tokenizers

Three subword methods are often confused: WordPiece, Byte-Pair Encoding (BPE), and the Unigram language model method, which is commonly used through the SentencePiece library. They differ mainly in how they build the vocabulary and how they split new text.

Method | How the vocabulary is built | How new text is split | Word boundaries
WordPiece | Merges symbol pairs chosen by how much they improve a likelihood-style score, which favors pairs whose parts are rare on their own | Greedy longest match from the left within each pre-split word | Marks continuation pieces with a prefix such as ##
BPE | Merges the most frequent adjacent pair at each step | Applies the learned merges in order | Varies by implementation; byte-level variants encode spaces inside tokens
Unigram (often via SentencePiece) | Starts from a large candidate set and prunes pieces that contribute least to a probabilistic model | Chooses the most probable segmentation | SentencePiece treats the space as a visible symbol, so splitting does not depend on whitespace pre-splitting

In practice you rarely choose among these freely. The model you are using fixes the method and the vocabulary. Feeding a model text tokenized with a different vocabulary produces invalid or meaningless input.

Will the output stay readable and usable after copying or sharing?

Not as readable text. Token strings and token IDs are designed for software, not for people. Before copying, sharing, or storing them, keep these limits in mind:

  • Readability: continuation markers and fragment boundaries break up words. A list of pieces is harder to read than the sentence it came from.
  • Round-tripping: removing the prefixes and rejoining the pieces usually restores each word. However, spacing around punctuation, original whitespace, letter case and accents may not come back exactly, especially with uncased normalization.
  • Unknown tokens: any span replaced by [UNK] is lost and cannot be recovered from the tokenized output.
  • Portability: token IDs only mean something with the exact vocabulary that produced them. Pasted into another model or tool, they are wrong or useless.
  • Search and editing: search engines, spell checkers and editors expect ordinary text. Searching or editing tokenized output gives unreliable results.
  • Accessibility: assistive technology may announce markers and fragments literally, or handle them unpredictably, depending on the software and its settings. No specific screen reader was tested for this article, so this is a reasonable expectation rather than a verified result.

WordPiece does not change reading order. It works on characters in their stored logical order and adds no bidirectional control characters. Reading-order problems in right-to-left scripts come from other parts of a pipeline, not from the segmentation step.

A plain-text equivalent is needed whenever the content leaves the model pipeline. That includes display to users, records, logs meant for people, search indexes, and handoff to other systems. The simplest safe practice is to keep the original string next to any tokenized form and never treat the tokens as the source of truth.

Gaps in the available evidence

This overview relies on the generally documented behavior of WordPiece and BERT-style tokenizers. No measurements, benchmarks, vocabulary statistics or tests of particular platforms or assistive technologies were supplied. As a result, this article makes no claims about speed, vocabulary size, segmentation quality for specific languages, or behavior in any named product. Before relying on WordPiece output, test the specific tokenizer configuration and vocabulary you plan to use. Check how it handles your actual text, especially accented characters, emoji, and scripts that do not separate words with spaces.