mistral_common.tokens.tokenizers.tekken
ModelData
Bases: TypedDict
Complete tekken tokenizer model data from the JSON file.
Attributes:
| Name | Type | Description |
|---|---|---|
vocab |
list[TokenInfo]
|
List of TokenInfo entries for regular vocabulary tokens. |
special_tokens |
list[SpecialTokenInfo] | None
|
List of SpecialTokenInfo entries, or |
config |
TekkenConfig
|
TekkenConfig with tokenizer settings. |
version |
int
|
Integer version of the tokenizer file format. |
type |
str
|
String type identifier for the tokenizer. |
image |
ImageConfig
|
ImageConfig for image processing, or missing if not supported. |
audio |
AudioConfig
|
AudioConfig for audio processing, or missing if not supported. |
SpecialTokenInfo
TekkenConfig
Bases: TypedDict
Tekken tokenizer configuration in the JSON file.
Attributes:
| Name | Type | Description |
|---|---|---|
pattern |
str
|
Regex pattern string used for tokenization (tiktoken |
num_vocab_tokens |
int
|
Number of regular (non-special) tokens in the vocabulary. |
default_vocab_size |
int
|
Default total vocabulary size (vocab + special tokens). |
default_num_special_tokens |
int
|
Default number of special tokens. |
version |
str
|
Version string of the tokenizer (e.g., "v1", "v2"). |
Tekkenizer(vocab, special_tokens, pattern, vocab_size, num_special_tokens, version, *, name='tekkenizer', _path=None, image_config=None, audio_config=None, model_settings_builder=None)
Bases: Tokenizer
Tekken tokenizer based on the tiktoken library.
High-performance tokenizer for multiple languages, supporting text, image, and audio modalities. Uses byte-level BPE with customizable patterns and special tokens.
The tokenizer works by:
1. Using tiktoken's fast BPE encoder for regular tokens
2. Managing special tokens separately (prefixed to the vocabulary)
3. Supporting multimodal configurations via image_config and audio_config
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vocab
|
list[TokenInfo]
|
List of token information defining the vocabulary. Each entry contains the token's rank, base64-encoded bytes, and string representation. |
required |
special_tokens
|
list[SpecialTokenInfo]
|
List of special token definitions. If fewer than
|
required |
pattern
|
str
|
Regex pattern used for tokenization (tiktoken |
required |
vocab_size
|
int
|
Total vocabulary size (vocab tokens + special tokens).
Must be <= len(vocab) + |
required |
num_special_tokens
|
int
|
Total number of special tokens. If |
required |
version
|
TokenizerVersion
|
Tokenizer version. Determines supported features and validation rules. |
required |
name
|
str
|
Identifier for this tokenizer instance. Defaults to "tekkenizer". |
'tekkenizer'
|
_path
|
str | Path | None
|
Source file path. Internal use only; not for direct instantiation. |
None
|
image_config
|
ImageConfig | None
|
Configuration for image processing, or |
None
|
audio_config
|
AudioConfig | None
|
Configuration for audio processing, or |
None
|
model_settings_builder
|
ModelSettingsBuilder | None
|
Builder for model-specific settings. Must be |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
AssertionError
|
If |
Source code in src/mistral_common/tokens/tokenizers/tekken.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
audio
property
writable
The audio configuration for this tokenizer.
Returns:
| Type | Description |
|---|---|
AudioConfig | None
|
AudioConfig instance if audio support is configured, otherwise |
bos_id
cached
property
The beginning-of-sentence token ID.
eos_id
cached
property
The end-of-sentence token ID.
file_path
property
The path to the tokenizer file.
Returns:
| Type | Description |
|---|---|
Path
|
Path to the source JSON file. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the tokenizer was not loaded from a file (e.g., constructed directly via init without _path). |
image
property
writable
The image configuration for this tokenizer.
Returns:
| Type | Description |
|---|---|
ImageConfig | None
|
ImageConfig instance if image support is configured, otherwise |
model_settings_builder
property
The model settings builder for this tokenizer.
Returns:
| Type | Description |
|---|---|
ModelSettingsBuilder | None
|
ModelSettingsBuilder instance if the tokenizer version supports model |
ModelSettingsBuilder | None
|
settings, otherwise |
n_words
property
Total vocabulary size of the tokenizer.
Returns:
| Type | Description |
|---|---|
int
|
Sum of regular vocabulary tokens and special tokens. |
num_special_tokens
property
The total number of special tokens in this tokenizer.
Returns:
| Type | Description |
|---|---|
int
|
Count of all special tokens (original + filler tokens). |
pad_id
cached
property
The padding token ID.
special_ids
cached
property
unk_id
cached
property
The unknown token ID.
version
property
The version of this tokenizer.
Returns:
| Type | Description |
|---|---|
TokenizerVersion
|
TokenizerVersion enum value indicating the tokenizer's version. |
decode(tokens, special_token_policy=SpecialTokenPolicy.IGNORE)
Decode a list of token IDs into a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
list[int]
|
List of token IDs to decode. |
required |
special_token_policy
|
SpecialTokenPolicy
|
Policy for handling special tokens: - IGNORE: Skip special tokens (default) - KEEP: Include special token strings in output - RAISE: Raise ValueError if special tokens are present |
IGNORE
|
Returns:
| Type | Description |
|---|---|
str
|
The decoded UTF-8 string. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/mistral_common/tokens/tokenizers/tekken.py
encode(s, bos, eos)
Encode a string into a list of token ids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The string to encode. |
required |
bos
|
bool
|
If |
required |
eos
|
bool
|
If |
required |
Returns:
| Type | Description |
|---|---|
list[int]
|
List of token IDs. Regular tokens are offset by |
Source code in src/mistral_common/tokens/tokenizers/tekken.py
from_file(path)
classmethod
Load the tekken tokenizer from a JSON file.
The file must contain vocab, config, and optionally special_tokens, image,
audio, and model_settings_builder sections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the tokenizer JSON file. Must exist and be readable. |
required |
Returns:
| Type | Description |
|---|---|
Tekkenizer
|
A Tekkenizer instance configured from the file. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file has an unknown version, is missing required fields,
or contains incompatible configuration (e.g., |
AssertionError
|
If the file does not exist. |
Source code in src/mistral_common/tokens/tokenizers/tekken.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
get_control_token(s)
Get the token ID of a control token. Deprecated: use get_special_token() instead.
Deprecated: Use get_special_token() instead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The string representation of the control token. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The integer token ID for the control token. |
Source code in src/mistral_common/tokens/tokenizers/tekken.py
get_special_token(s)
Get the token ID of a special token by its string representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
s
|
str
|
The string representation of the special token (e.g., " |
required |
Returns:
| Type | Description |
|---|---|
int
|
The integer token ID for the special token. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the special token string is not recognized. |
Source code in src/mistral_common/tokens/tokenizers/tekken.py
id_to_byte_piece(token_id, special_token_policy=SpecialTokenPolicy.IGNORE)
Convert a token ID to its byte representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_id
|
int
|
The token ID to convert. |
required |
special_token_policy
|
SpecialTokenPolicy
|
Policy for handling special tokens: - IGNORE: Return empty bytes for special tokens - KEEP: Return UTF-8 encoded special token string - RAISE: Raise ValueError if token is special |
IGNORE
|
Returns:
| Type | Description |
|---|---|
bytes
|
The byte representation of the token. For regular tokens, returns the |
bytes
|
original byte sequence from the vocabulary. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/mistral_common/tokens/tokenizers/tekken.py
id_to_piece(token_id)
Convert a token ID to its string representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_id
|
int
|
The token ID to convert. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The string representation of the token. Special tokens are decoded as their |
str
|
token string; regular tokens are decoded using the underlying tiktoken model. |
Source code in src/mistral_common/tokens/tokenizers/tekken.py
is_byte(token_id)
Check if a token ID represents a single byte.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token_id
|
int
|
The token ID to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
range [0, 255], meaning it represents a single byte. |
Source code in src/mistral_common/tokens/tokenizers/tekken.py
is_special(token)
Check if a token is a special token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
token
|
int | integer | str
|
Token ID (int or numpy integer) or token string to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If token is not an int, numpy integer, or str. |
Source code in src/mistral_common/tokens/tokenizers/tekken.py
vocab()
Get all tokens in the vocabulary as strings.
Note
Tokens with decoding errors are collapsed into the "<?>" string. This may result in len(set(vocab)) != len(vocab). Use with caution.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of all tokens in the vocabulary as strings, in token ID order. |
Source code in src/mistral_common/tokens/tokenizers/tekken.py
TokenInfo
is_tekken(path)
Check if the given path is a tekken tokenizer file.
is_tekkenizer(tokenizer)
Check if a tokenizer is a Tekkenizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer
|
Tokenizer
|
The tokenizer to check. |
required |
Returns:
| Type | Description |
|---|---|
TypeGuard[Tekkenizer]
|
|