mistral_common.tokens.tokenizers.mistral
MistralTokenizer(instruct_tokenizer, validator, request_normalizer)
Bases: Generic[UserMessageType, AssistantMessageType, ToolMessageType, SystemMessageType, TokenizedType]
Mistral tokenizer.
This class is a wrapper around a InstructTokenizer, a MistralRequestValidator and a InstructRequestNormalizer.
It provides a convenient interface to tokenize, validate ad normalize Mistral requests.
Attributes:
| Name | Type | Description |
|---|---|---|
instruct_tokenizer |
InstructTokenizer[InstructRequest, FIMRequest, TokenizedType]
|
The instruct tokenizer to use. See InstructTokenizer. |
Prefer classmethods like from_file or from_hf_hub over direct construction;
they resolve the correct validator and normalizer for the tokenizer version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instruct_tokenizer
|
InstructTokenizer[InstructRequest, FIMRequest, TokenizedType]
|
Versioned tokenizer that performs the actual tokenization. |
required |
validator
|
MistralRequestValidator[UserMessageType, AssistantMessageType, ToolMessageType, SystemMessageType]
|
Validator applied to requests before normalization. Its mode (serving/finetuning/test) controls which constraints are enforced. |
required |
request_normalizer
|
InstructRequestNormalizer[UserMessageType, AssistantMessageType, ToolMessageType, SystemMessageType, InstructRequestType]
|
Normalizer converting validated ChatCompletionRequests into InstructRequests ready for tokenization. |
required |
Source code in src/mistral_common/tokens/tokenizers/mistral.py
mode
property
The validation mode of the tokenizer.
Returns:
| Type | Description |
|---|---|
ValidationMode
|
The ValidationMode (serving, finetuning, or test) used by this tokenizer's validator. |
version
property
The version of the tokenizer.
Returns:
| Type | Description |
|---|---|
TokenizerVersion
|
The TokenizerVersion enum value determining supported features |
TokenizerVersion
|
(tool calls, audio, model settings, etc.). |
decode(tokens, special_token_policy=SpecialTokenPolicy.IGNORE)
Decode a list of tokens 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 the output - RAISE: Raise ValueError if special tokens are present |
IGNORE
|
Returns:
| Type | Description |
|---|---|
str
|
The decoded string. |
Source code in src/mistral_common/tokens/tokenizers/mistral.py
encode_chat_completion(request, max_model_input_len=None)
Encode a chat completion request into tokens.
Validates the request, normalizes it into an InstructRequest, and tokenizes it. This is the main entry point for chat tokenization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
ChatCompletionRequest[UATS]
|
The chat completion request to encode. |
required |
max_model_input_len
|
int | None
|
Maximum number of input tokens the model accepts.
Used only when |
None
|
Returns:
| Type | Description |
|---|---|
TokenizedType
|
The tokenized request (tokens, message boundaries, etc.), specific to |
TokenizedType
|
the tokenizer version. |
Raises:
| Type | Description |
|---|---|
TokenizerException
|
If |
MistralCommonException
|
If request validation fails. |
Source code in src/mistral_common/tokens/tokenizers/mistral.py
encode_fim(request)
Encode a fill-in-the-middle request into tokens.
Wraps the prompt with prefix/middle/suffix markers so the model can complete the middle section.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
FIMRequest
|
The fill-in-the-middle request containing the prompt parts. |
required |
Returns:
| Type | Description |
|---|---|
TokenizedType
|
The tokenized fill-in-the-middle request. |
Source code in src/mistral_common/tokens/tokenizers/mistral.py
encode_speech_request(request)
Encode a speech synthesis request into tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
SpeechRequest
|
The speech request containing the text and optional audio. |
required |
Returns:
| Type | Description |
|---|---|
TokenizedType
|
The tokenized speech request. |
Source code in src/mistral_common/tokens/tokenizers/mistral.py
encode_transcription(request)
Encode a transcription request into tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
TranscriptionRequest
|
The transcription request containing the audio to transcribe. |
required |
Returns:
| Type | Description |
|---|---|
TokenizedType
|
The tokenized transcription request. |
Source code in src/mistral_common/tokens/tokenizers/mistral.py
from_file(tokenizer_filename, mode=ValidationMode.test)
classmethod
Load a tokenizer from a file.
Detects the tokenizer type (tekken JSON or sentencepiece model) from the file, wires in the matching image/audio encoders, validator, and normalizer for the tokenizer version, and returns the fully assembled MistralTokenizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer_filename
|
str | Path
|
Path to a tekken (.json containing "tekken" in the name) or sentencepiece (.model) tokenizer file. |
required |
mode
|
ValidationMode
|
The validation mode to use for the loaded tokenizer. |
test
|
Returns:
| Type | Description |
|---|---|
MistralTokenizer
|
The loaded tokenizer, configured with the version-appropriate |
MistralTokenizer
|
InstructTokenizer, validator, and normalizer. |
Raises:
| Type | Description |
|---|---|
TokenizerException
|
If the file is neither a tekken nor a sentencepiece tokenizer, or the tokenizer version is unrecognized. |
AssertionError
|
If the file declares image/audio support that its tokenizer version does not support. |
Source code in src/mistral_common/tokens/tokenizers/mistral.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 363 364 365 366 367 368 369 370 371 372 | |
from_hf_hub(repo_id, token=None, revision=None, force_download=False, local_files_only=False, mode=ValidationMode.test)
staticmethod
Download the Mistral tokenizer for a given Hugging Face repository ID.
See here for a list of our OSS models.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
repo_id
|
str
|
The Hugging Face repo ID, e.g. "mistralai/Mistral-Small-2411". |
required |
token
|
bool | str | None
|
Hugging Face access token for private repos. If |
None
|
revision
|
str | None
|
Git branch, tag, or commit hash to download. If |
None
|
mode
|
ValidationMode
|
The validation mode to use for the loaded tokenizer. |
test
|
force_download
|
bool
|
If |
False
|
local_files_only
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
MistralTokenizer
|
The Mistral tokenizer for the given repository. |
Source code in src/mistral_common/tokens/tokenizers/mistral.py
v1()
classmethod
v2()
classmethod
Get the Mistral tokenizer v2.
v3(is_tekken=False, is_mm=False)
classmethod
Get the Mistral tokenizer v3.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
is_tekken
|
bool
|
If |
False
|
is_mm
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
MistralTokenizer
|
The Mistral tokenizer v3, in test validation mode. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/mistral_common/tokens/tokenizers/mistral.py
v7(is_mm=False)
classmethod
Get the Mistral tokenizer v7.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
is_mm
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
MistralTokenizer
|
The Mistral tokenizer v7, in test validation mode. |
Source code in src/mistral_common/tokens/tokenizers/mistral.py
load_audio_encoder(audio_config, tokenizer)
Build an AudioEncoder from a config and a tokenizer.
Resolves the audio special token IDs (audio, begin_audio, streaming_pad,
text_to_audio, audio_to_text) from the tokenizer. Tokens that are not
defined in the tokenizer are set to None, making them optional.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio_config
|
AudioConfig
|
Audio processing configuration (encoding, sampling rate, etc.). |
required |
tokenizer
|
Tekkenizer
|
Tekkenizer providing the special token IDs for audio markers. |
required |
Returns:
| Type | Description |
|---|---|
AudioEncoder
|
An AudioEncoder bound to the tokenizer's special token IDs. IDs for |
AudioEncoder
|
undefined tokens are |
Source code in src/mistral_common/tokens/tokenizers/mistral.py
load_image_encoder(image_config, tokenizer)
Build an ImageEncoder from a config and a tokenizer.
Resolves the image special token IDs (img, img_break, img_end) from the
tokenizer and combines them with the image config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_config
|
ImageConfig
|
Image processing configuration (image size, patch size, etc.). |
required |
tokenizer
|
Tekkenizer | SentencePieceTokenizer
|
Tokenizer providing the special token IDs for image markers. |
required |
Returns:
| Type | Description |
|---|---|
ImageEncoder
|
An ImageEncoder bound to the tokenizer's special token IDs. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any of the required image special tokens is not defined in the tokenizer. |