Tokenizer Class
A thin wrapper aroundtiktoken providing quick access to special tokens and language-specific encoding.
Initialization
Parameters
tiktoken.Encoding
required
The underlying tiktoken encoding instance
int
required
Number of languages supported by this tokenizer (typically 99)
str | None
default:"None"
The language code (e.g., “en”, “fr”, “es”) for this tokenizer instance
str | None
default:"None"
The task type: either “transcribe” or “translate”
Tuple[int]
default:"()"
Start-of-transcript token sequence (automatically generated in post_init)
Dict[str, int]
default:"{}"
Dictionary mapping special token strings to their token IDs (automatically populated)
Methods
encode()
Encode text into a list of token IDs.str
required
The text to encode
any
Additional keyword arguments passed to the underlying tiktoken encoding
List[int]
List of token IDs representing the encoded text
decode()
Decode token IDs back into text, filtering out timestamp tokens.List[int]
required
List of token IDs to decode
any
Additional keyword arguments passed to the underlying tiktoken decoder
str
The decoded text with timestamp tokens filtered out (tokens >= timestamp_begin are removed)
decode_with_timestamps()
Decode token IDs including timestamp annotations.List[int]
required
List of token IDs to decode
any
Additional keyword arguments passed to the underlying tiktoken decoder
str
The decoded text with timestamp tokens annotated as
<|1.08|> formatto_language_token()
Convert a language code to its corresponding token ID.str
required
Language code (e.g., “en”, “fr”, “es”)
int
Token ID corresponding to the language
KeyError if the language is not found in the tokenizer.
split_to_word_tokens()
Split tokens into word-level tokens based on language-specific rules.List[int]
required
List of token IDs to split
Tuple[List[str], List[List[int]]]
A tuple containing:
- List of decoded words
- List of token ID lists corresponding to each word
Special Token Properties
All special token properties are cached for performance.eot
int
End-of-transcript token ID
sot
int
Start-of-transcript token ID for
<|startoftranscript|>transcribe
int
Transcribe task token ID for
<|transcribe|>translate
int
Translate task token ID for
<|translate|>sot_lm
int
Start-of-language-model token ID for
<|startoflm|>sot_prev
int
Start-of-previous token ID for
<|startofprev|>no_speech
int
No-speech token ID for
<|nospeech|>no_timestamps
int
No-timestamps token ID for
<|notimestamps|>timestamp_begin
int
Token ID for the first timestamp token
<|0.00|>language_token
int
Token ID for the language configured in this tokenizer instance
ValueError if no language is configured.
all_language_tokens
Tuple[int]
Tuple of all language token IDs supported by this tokenizer
all_language_codes
Tuple[str]
Tuple of all language codes (e.g., “en”, “fr”, “es”) supported by this tokenizer
sot_sequence_including_notimestamps
Tuple[int]
The start-of-transcript sequence with the no-timestamps token appended
non_speech_tokens
Tuple[int]
Tuple of token IDs for non-speech annotations (e.g., speaker tags, music symbols) that should be suppressed during generation
- Music notation:
♪♪♪ - Speaker tags:
[DAVID] - Stage directions:
(SPEAKING FOREIGN LANGUAGE) - Various symbols and brackets
get_tokenizer()
Factory function to create a tokenizer instance for Whisper models.Parameters
bool
required
Whether to use the multilingual tokenizer (True) or English-only tokenizer (False)
int
default:"99"
Number of languages to support (only relevant for multilingual models)
str | None
default:"None"
Language code or name (e.g., “en”, “english”, “fr”, “french”). If multilingual=True and language is None, defaults to “en”
str | None
default:"None"
Task type: “transcribe” or “translate”. If multilingual=True and task is None, defaults to “transcribe”
Tokenizer
Configured tokenizer instance
ValueError if an unsupported language is provided.
Language Code Resolution
The function accepts both language codes and full language names:Language Constants
LANGUAGES
Dictionary mapping language codes to language names.Dict[str, str]
Dictionary with 99 language code to name mappings
- Western European: English, French, German, Spanish, Italian, Portuguese, Dutch, etc.
- Eastern European: Russian, Polish, Czech, Ukrainian, Romanian, etc.
- Asian: Chinese, Japanese, Korean, Hindi, Thai, Vietnamese, etc.
- Middle Eastern: Arabic, Hebrew, Persian, Turkish, Urdu, etc.
- African: Swahili, Afrikaans, Amharic, Hausa, Yoruba, etc.
- Others: Latin, Sanskrit, Hawaiian, Maori, etc.
TO_LANGUAGE_CODE
Dictionary for looking up language codes by name, including aliases.Dict[str, str]
Dictionary mapping language names and aliases to their codes
- “burmese” → “my”
- “mandarin” → “zh”
- “castilian” → “es”
- “flemish” → “nl”
- “haitian” → “ht”
- “moldavian”/“moldovan” → “ro”
- “sinhalese” → “si”