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.). |
__reduce__()
Provide a pickling recipe so the tokenizer survives multiprocessing.
The tokenizer is serialized as a (from_file, (path, mode)) pair, so the
object is reconstructed from its source file rather than pickling internal state.
Returns:
| Type | Description |
|---|---|
tuple[Callable, tuple[Any, ...]]
|
A tuple of the |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the tokenizer was not loaded from a file, in which case there is no path to reconstruct from. |
Source code in src/mistral_common/tokens/tokenizers/mistral.py
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
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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | |
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
from_model(model, strict=True)
classmethod
Deprecated in favor of from_hf_hub or from_file, will be removed in 1.13.0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
The model name. Must be one of the known legacy model names (e.g., "mistral-small-2402", "codestral-2405"); newer models are not registered here. |
required |
strict
|
bool
|
Has to be |
True
|
Returns:
| Type | Description |
|---|---|
MistralTokenizer
|
The Mistral tokenizer for the given model. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If strict is |
TokenizerException
|
If the model name is not recognized. |
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. |