Skip to main content
Qwen uses BPE (Byte Pair Encoding) tokenization on UTF-8 bytes using the tiktoken package. The tokenizer includes both regular BPE tokens and special control tokens.

Quick Start

Vocabulary Overview

Qwen’s tokenizer contains 151,851 tokens total:
  • 151,643 regular tokens - BPE tokens learned from UTF-8 byte sequences
  • 208 special tokens - Control tokens for specific model functions

Design Philosophy

The tokenizer is optimized for:
  1. Efficient Chinese, English, and code encoding
  2. Multilingual support without vocabulary expansion
  3. Number segmentation by single digits
  4. No unknown tokens - falls back to single bytes if needed

Regular Tokens

Regular tokens are BPE tokens learned from byte sequences encoded using UTF-8.

UTF-8 Byte Encoding

Unlike tokenizers that operate on Unicode codepoints, Qwen operates directly on UTF-8 bytes:
This may produce replacement characters (�) in incomplete generation. The tokens complete when more bytes arrive:

Handling Decoding Errors

You can control UTF-8 decoding error handling:
For more error handling options, see the Python documentation.

Retrieving Vocabulary

Access the complete regular token vocabulary:
We do not support or recommend adding regular tokens to the vocabulary directly. See Vocabulary Expansion for the correct approach.

Special Tokens

Special tokens signify specific functions to the model and don’t typically appear in input text.

Used Special Tokens

Qwen-7B (Base Model):
  • <|endoftext|> - End of document marker
Qwen-7B-Chat:
  • <|endoftext|> - End of document marker
  • <|im_start|> - Start of ChatML turn
  • <|im_end|> - End of ChatML turn
These tokens have determined meanings and should not be used for other purposes.

Extra Special Tokens

Qwen reserves extra special tokens for custom use:
  • <|extra_0|> through <|extra_204|> (205 tokens)
  • Available for fine-tuning and custom applications

Retrieving Special Tokens

BOS, EOS, PAD, UNK Tokens

The concepts of bos, eos, unk, pad, mask, sep are not applicable to Qwen pretrained models.
For frameworks that require these tokens (e.g., during fine-tuning):
Setting bos, eos, or unk without fine-tuning may introduce unknown behavior. Do not use <|endoftext|> as eos unless you understand the implications.

Injection Attack Prevention

The Problem

What happens when special token surface forms appear in text?
Should this be tokenized as: Correct (safe):
Incorrect (injection vulnerability):

Default Behavior

By default, Qwen now parses surface forms as special tokens (the second behavior above).
This matches community practice but is potentially unsafe.

Safe Tokenization

To prevent injection attacks, use allowed_special=set():

Fine-Grained Control

Allow specific special tokens:
Raise errors on specific tokens:
For more information, see the tiktoken documentation.

Backward Compatibility

The unsafe default is equivalent to:

Vocabulary Expansion

Read carefully and understand the caveats before expanding vocabulary. Use at your own risk.

Process

You cannot directly add words to BPE vocabulary. You must learn new merges: 1. Prepare vocabulary file qwen_extra_vocab.txt:
Format: token<TAB>frequency (frequencies needed for BPE computation) 2. Get base vocabulary:
  • Base file: qwen.tiktoken
  • Start index: 151,851 (default, or override inactive control tokens)
3. Learn new merges:
The script is available here. Output:
4. Use expanded vocabulary:
Requires tokenizer code from after 2023-10-08. Older versions must manually append qwen.tiktoken with content from qwen_extra.tiktoken.
5. Fine-tune the model to learn the new tokens.

Caveats

Unicode Boundary Issues

Because Qwen operates on UTF-8 bytes (not Unicode codepoints), limited training data may not recognize Unicode boundaries correctly. Problem: Invalid merges across codepoint boundaries may occur:
Solution: Include all Unicode codepoints from your words with higher frequencies:

Context-Dependent Tokenization

BPE is distribution-based, not semantic. Tokens may split differently in different contexts:
This reflects frequency in training data and is normal behavior.

Existing Token Example

This is expected BPE behavior based on frequency distribution.

Multilingual Efficiency

While optimized for Chinese, English, and code, Qwen achieves high compression rates for:
  • Thai (th)
  • Hebrew (he)
  • Arabic (ar)
  • Korean (ko)
  • Vietnamese (vi)
  • Japanese (ja)
  • Turkish (tr)
  • Indonesian (id)
  • Polish (pl)
  • Russian (ru)
  • Dutch (nl)
  • Portuguese (pt)
  • Italian (it)
  • German (de)
  • Spanish (es)
  • French (fr)
This provides strong scalability and efficiency for multilingual applications without vocabulary expansion.

Best Practices

Use injection prevention when:
  • Processing untrusted user input
  • Building chatbots or interactive systems
  • Handling code or structured text that might contain special token patterns
Consider vocabulary expansion when:
  • Fine-tuning on domain-specific text with unique terminology
  • Working with languages that would benefit from better tokenization
  • You have sufficient training data for the new tokens (1000+ examples)
Do NOT expand vocabulary if:
  • You’re only doing inference
  • You have limited training data
  • Your use case works well with existing tokenization
During generation, incomplete UTF-8 sequences may appear:

Additional Resources