mistral_common.protocol.instruct.validator
MistralRequestValidator(mode=ValidationMode.test)
Bases: Generic[UserMessageType, AssistantMessageType, ToolMessageType, SystemMessageType]
Validates Mistral chat completion requests and messages.
Performs comprehensive validation of request structure, message content, tool definitions, and model settings according to the specified validation mode.
Examples:
>>> from mistral_common.protocol.instruct.messages import UserMessage, AssistantMessage
>>> validator = MistralRequestValidator()
>>> messages = [UserMessage(content="Hello how are you ?")]
>>> validator.validate_messages(messages)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
ValidationMode
|
The validation mode to use. Options:
- |
test
|
Source code in src/mistral_common/protocol/instruct/validator.py
mode
property
The validation mode this validator enforces.
Returns:
| Type | Description |
|---|---|
ValidationMode
|
The ValidationMode (serving, finetuning, test, or agnostic) this |
ValidationMode
|
instance was constructed with. |
validate_messages(messages)
Validate a list of messages.
Checks message structure (alternating user/assistant, valid roles) and content (valid chunk types for each role, valid tool calls, etc.).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[UATS]
|
List of message objects to validate. Each message must be a subclass of BaseMessage with a valid role. |
required |
Raises:
| Type | Description |
|---|---|
InvalidMessageStructureException
|
If message structure is invalid (e.g., consecutive assistant messages without intervening user message). |
InvalidUserMessageException
|
If a user message has invalid content. |
InvalidAssistantMessageException
|
If an assistant message has invalid content. |
InvalidToolMessageException
|
If a tool message has invalid content. |
InvalidSystemPromptException
|
If a system message has invalid content. |
InvalidToolCallException
|
If a tool call in an assistant message is invalid. |
Examples:
>>> from mistral_common.protocol.instruct.messages import UserMessage, AssistantMessage
>>> validator = MistralRequestValidator()
>>> messages = [AssistantMessage(content="Hi"), UserMessage(content="Hello")]
>>> validator.validate_messages(messages)
Source code in src/mistral_common/protocol/instruct/validator.py
validate_request(request)
Validate a complete ChatCompletionRequest.
Performs full validation including: - Model field (required in serving mode) - Message list structure and content - Tool definitions and tool calls - Model settings
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
ChatCompletionRequest
|
The ChatCompletionRequest to validate. |
required |
Returns:
| Type | Description |
|---|---|
The same request object after validation. Note
|
validation may mutate |
ChatCompletionRequest[UATS]
|
the request in-place for some normalizations. |
Raises:
| Type | Description |
|---|---|
InvalidRequestException
|
If request fails validation (missing model in serving mode, invalid messages, invalid tools, etc.). |
Examples:
>>> from mistral_common.protocol.instruct.messages import UserMessage
>>> validator = MistralRequestValidator()
>>> request = ChatCompletionRequest(messages=[UserMessage(content="Hello")])
>>> validated_request = validator.validate_request(request)
Source code in src/mistral_common/protocol/instruct/validator.py
MistralRequestValidatorV11(mode=ValidationMode.test)
Bases: MistralRequestValidatorV5
Validator for v11 Mistral requests.
This validator extends v5 functionality by: - Adding stricter tool call/result pairing validation. - Allowing thinking chunks in assistant messages. - Allowing system prompts with audio chunks
Source code in src/mistral_common/protocol/instruct/validator.py
MistralRequestValidatorV13(mode=ValidationMode.test)
Bases: MistralRequestValidatorV11
Validator for v13 Mistral requests.
Source code in src/mistral_common/protocol/instruct/validator.py
MistralRequestValidatorV15(mode=ValidationMode.test)
Bases: MistralRequestValidatorV13
Validator for v15 Mistral requests.
Source code in src/mistral_common/protocol/instruct/validator.py
MistralRequestValidatorV3(mode=ValidationMode.test)
Bases: MistralRequestValidator
Validator for v3 Mistral requests.
This validator adds additional validation for tool call IDs.
Examples:
Source code in src/mistral_common/protocol/instruct/validator.py
MistralRequestValidatorV5(mode=ValidationMode.test)
Bases: MistralRequestValidatorV3
Validator for v5 Mistral requests.
This validator allows for both tool calls and content in the assistant message.
Note
For requests containing audio, this validator ensures that no system prompt is present.
Examples:
Source code in src/mistral_common/protocol/instruct/validator.py
ValidationMode
Validation mode controlling which validation rules are applied.
Attributes:
| Name | Type | Description |
|---|---|---|
serving |
Strict validation for production serving. Requires model to be specified and enforces all constraints. |
|
finetuning |
Validation for finetuning scenarios. May allow some fields that are not allowed in serving mode. |
|
test |
Lenient validation for testing. Skips some production constraints. |
|
agnostic |
Pipeline-agnostic validation. Applies all content checks without assuming a downstream pipeline, so no terminal-role rule is enforced. |
Examples:
get_validator(version, mode)
Get the appropriate validator for a given tokenizer version and validation mode.
The validator version matches the tokenizer version and enforces the content chunk types, tool call ID rules, and model settings supported by that version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
TokenizerVersion
|
The tokenizer version the validator should match. |
required |
mode
|
ValidationMode
|
The validation mode (serving, finetuning, test, or agnostic) controlling which constraints are enforced. |
required |
Returns:
| Type | Description |
|---|---|
MistralRequestValidator
|
The validator instance appropriate for the version and mode. |