Skip to content

# `mistral_common.protocol.instruct.validator`

## `MistralRequestValidator(mode=ValidationMode.test)`

Bases: `Generic[UserMessageType, AssistantMessageType, ToolMessageType, SystemMessageType]`

Validator for Mistral requests.

This class validates the structure and content of Mistral requests.

Examples:

    >>> from mistral_common.protocol.instruct.messages import UserMessage, AssistantMessage
    >>> validator = MistralRequestValidator()
    >>> messages = [UserMessage(content="Hello how are you ?")]
    >>> validator.validate_messages(messages, False)

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`mode` | `ValidationMode` | The validation mode. Defaults to ValidationMode.test. | `test`

Source code in `src/mistral_common/protocol/instruct/validator.py`

    100
    101
    102
    103
    104
    105
    106

    def __init__(self, mode: ValidationMode = ValidationMode.test):
        r"""Initializes the `MistralRequestValidator`.

        Args:
            mode: The validation mode. Defaults to ValidationMode.test.
        """
        self._mode = mode

### `validate_messages(messages, continue_final_message)`

Validates the list of messages.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`messages` | `list[UATS]` | The list of messages to validate. | required
`continue_final_message` | `bool` | Whether to continue the final message. | required

Examples:

    >>> from mistral_common.protocol.instruct.messages import UserMessage, AssistantMessage
    >>> validator = MistralRequestValidator()
    >>> messages = [AssistantMessage(content="Hi"), UserMessage(content="Hello")]
    >>> validator.validate_messages(messages, False)

Source code in `src/mistral_common/protocol/instruct/validator.py`

    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126

    def validate_messages(self, messages: list[UATS], continue_final_message: bool) -> None:
        r"""Validates the list of messages.

        Args:
            messages: The list of messages to validate.
            continue_final_message: Whether to continue the final message.

        Examples:
            >>> from mistral_common.protocol.instruct.messages import UserMessage, AssistantMessage
            >>> validator = MistralRequestValidator()
            >>> messages = [AssistantMessage(content="Hi"), UserMessage(content="Hello")]
            >>> validator.validate_messages(messages, False)
        """
        self._validate_message_list_structure(messages, continue_final_message=continue_final_message)
        self._validate_message_list_content(messages)

### `validate_request(request)`

Validates the request

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`request` | `ChatCompletionRequest` | The request to validate. | required

Returns:

Type | Description
--- | ---
`ChatCompletionRequest[UATS]` | The validated request.

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`

    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156

    def validate_request(self, request: ChatCompletionRequest) -> ChatCompletionRequest[UATS]:
        r"""Validates the request

        Args:
            request: The request to validate.

        Returns:
            The validated request.

        Examples:
            >>> from mistral_common.protocol.instruct.messages import UserMessage
            >>> validator = MistralRequestValidator()
            >>> request = ChatCompletionRequest(messages=[UserMessage(content="Hello")])
            >>> validated_request = validator.validate_request(request)
        """

        if self._mode == ValidationMode.serving:
            if request.model is None:
                raise InvalidRequestException("Model name parameter is required for serving mode")

        # Validate the messages
        self.validate_messages(request.messages, continue_final_message=request.continue_final_message)

        # Validate the tools
        self._validate_tools(request.tools or [])

        self._validate_model_settings(request)

        return request

## `MistralRequestValidatorV13(mode=ValidationMode.test)`

Bases: `MistralRequestValidatorV5`

Validator for v13 Mistral requests.

This validator extends v5 functionality by: - Adding stricter tool call ID validation: they should be distinct and called. - Allowing system prompts with audio chunks

Source code in `src/mistral_common/protocol/instruct/validator.py`

    100
    101
    102
    103
    104
    105
    106

    def __init__(self, mode: ValidationMode = ValidationMode.test):
        r"""Initializes the `MistralRequestValidator`.

        Args:
            mode: The validation mode. Defaults to ValidationMode.test.
        """
        self._mode = mode

## `MistralRequestValidatorV15(mode=ValidationMode.test)`

Bases: `MistralRequestValidatorV13`

Source code in `src/mistral_common/protocol/instruct/validator.py`

    100
    101
    102
    103
    104
    105
    106

    def __init__(self, mode: ValidationMode = ValidationMode.test):
        r"""Initializes the `MistralRequestValidator`.

        Args:
            mode: The validation mode. Defaults to ValidationMode.test.
        """
        self._mode = mode

## `MistralRequestValidatorV3(mode=ValidationMode.test)`

Bases: `MistralRequestValidator`

Validator for v3 Mistral requests.

This validator adds additional validation for tool call IDs.

Examples:

    >>> validator = MistralRequestValidatorV3()

Source code in `src/mistral_common/protocol/instruct/validator.py`

    100
    101
    102
    103
    104
    105
    106

    def __init__(self, mode: ValidationMode = ValidationMode.test):
        r"""Initializes the `MistralRequestValidator`.

        Args:
            mode: The validation mode. Defaults to ValidationMode.test.
        """
        self._mode = mode

## `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:

    >>> validator = MistralRequestValidatorV5()

Source code in `src/mistral_common/protocol/instruct/validator.py`

    100
    101
    102
    103
    104
    105
    106

    def __init__(self, mode: ValidationMode = ValidationMode.test):
        r"""Initializes the `MistralRequestValidator`.

        Args:
            mode: The validation mode. Defaults to ValidationMode.test.
        """
        self._mode = mode

## `ValidationMode`

Bases: `str`, `Enum`

Enum for the validation mode.

Attributes:

Name | Type | Description
--- | --- | ---
`serving` |  | The serving mode.
`finetuning` |  | The finetuning mode.
`test` |  | The test mode.

Examples:

    >>> mode = ValidationMode.serving

## `get_validator(version, mode)`

Get the appropriate validator for a given tokenizer version and validation mode.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`version` | `TokenizerVersion` | The tokenizer version. | required
`mode` | `ValidationMode` | The validation mode. | required

Returns:

Type | Description
--- | ---
`MistralRequestValidator` | The validator instance.

Source code in `src/mistral_common/protocol/instruct/validator.py`

    596
    597
    598
    599
    600
    601
    602
    603
    604
    605
    606
    607
    608
    609
    610
    611
    612
    613
    614
    615
    616
    617
    618
    619
    620
    621

    def get_validator(version: TokenizerVersion, mode: ValidationMode) -> MistralRequestValidator:
        r"""Get the appropriate validator for a given tokenizer version and validation mode.

        Args:
            version: The tokenizer version.
            mode: The validation mode.

        Returns:
            The validator instance.
        """
        validator: MistralRequestValidator
        match version:
            case TokenizerVersion.v1 | TokenizerVersion.v2:
                validator = MistralRequestValidator(mode=mode)
            case TokenizerVersion.v3:
                validator = MistralRequestValidatorV3(mode=mode)
            case TokenizerVersion.v7:
                validator = MistralRequestValidatorV5(mode=mode)
            case TokenizerVersion.v11 | TokenizerVersion.v13:
                validator = MistralRequestValidatorV13(mode=mode)
            case TokenizerVersion.v15:
                validator = MistralRequestValidatorV15(mode=mode)
            case _:
                assert_never(f"Unsupported tokenizer version: {version}")

        return validator