Skip to content

# `mistral_common.tokens.tokenizers.model_settings_builder`

## `EnumBuilder(**data)`

Bases: `FieldBuilder[E]`

Builder for enum fields.

This class validates that enum fields contain only authorized values. It rejects duplicate values during initialization and ensures the allowed values list is non-empty when None is not accepted.

Attributes:

Name | Type | Description
--- | --- | ---
`type` | `ValidatorType` | The type of validator (always ENUM for this class).
`values` | `list[E]` | List of allowed enum values.

Source code in `pydantic/main.py`

    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270

    def __init__(self, /, **data: Any) -> None:
        """Create a new model by parsing and validating input data from keyword arguments.

        Raises [`ValidationError`][pydantic_core.ValidationError] if the input data cannot be
        validated to form a valid model.

        `self` is explicitly positional-only to allow `self` as a field name.
        """
        # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
        __tracebackhide__ = True
        validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
        if self is not validated_self:
            warnings.warn(
                'A custom validator is returning a value other than `self`.\n'
                "Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.\n"
                'See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.',
                stacklevel=2,
            )

### `validate_default()`

Ensure the default value, if set, is among the allowed values.

Source code in `src/mistral_common/tokens/tokenizers/model_settings_builder.py`

    123
    124
    125
    126
    127
    128

    @model_validator(mode="after")
    def validate_default(self) -> "EnumBuilder":
        r"""Ensure the default value, if set, is among the allowed values."""
        if self.default is not None and self.default not in self.values:
            raise ValueError(f"Default value {self.default=} is not in {self.values=}.")
        return self

### `validate_empty_list()`

Ensure the allowed values list is non-empty when None is not accepted.

Source code in `src/mistral_common/tokens/tokenizers/model_settings_builder.py`

    116
    117
    118
    119
    120
    121

    @model_validator(mode="after")
    def validate_empty_list(self) -> "EnumBuilder":
        r"""Ensure the allowed values list is non-empty when None is not accepted."""
        if len(self.values) == 0 and not self.accepts_none:
            raise ValueError(f"Empty list of values for {self.values=} while not accepts_none.")
        return self

### `validate_unique_values()`

Ensure no duplicate values are present in the allowed values list.

Source code in `src/mistral_common/tokens/tokenizers/model_settings_builder.py`

    109
    110
    111
    112
    113
    114

    @model_validator(mode="after")
    def validate_unique_values(self) -> "EnumBuilder":
        r"""Ensure no duplicate values are present in the allowed values list."""
        if len(set(self.values)) != len(self.values):
            raise ValueError(f"Duplicate values in {self.values=}")
        return self

## `FieldBuilder(**data)`

Bases: `MistralBase`, `Generic[T]`

Base class for field builders.

This class serves as the base for all field builders in the validation framework. It ensures that all builders have a type attribute that specifies the kind of validation being performed.

Attributes:

Name | Type | Description
--- | --- | ---
`type` | `ValidatorType` | The type of validator (e.g., ENUM).
`accepts_none` | `bool` | Whether the field accepts None as a valid value in the request.
`default` | `T | None` | The default value to use when the field is None, if accepts_none is True.

Source code in `pydantic/main.py`

    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270

    def __init__(self, /, **data: Any) -> None:
        """Create a new model by parsing and validating input data from keyword arguments.

        Raises [`ValidationError`][pydantic_core.ValidationError] if the input data cannot be
        validated to form a valid model.

        `self` is explicitly positional-only to allow `self` as a field name.
        """
        # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
        __tracebackhide__ = True
        validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
        if self is not validated_self:
            warnings.warn(
                'A custom validator is returning a value other than `self`.\n'
                "Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.\n"
                'See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.',
                stacklevel=2,
            )

### `build_value(field_name, value)`

Resolve and validate a field value, returning the final built result.

Raises:

Type | Description
--- | ---
`InvalidRequestException` | If the value is invalid or missing when required.

Source code in `src/mistral_common/tokens/tokenizers/model_settings_builder.py`

    79
    80
    81
    82
    83
    84
    85
    86
    87
    88

    @final
    def build_value(self, field_name: str, value: T | None) -> T | None:
        r"""Resolve and validate a field value, returning the final built result.

        Raises:
            InvalidRequestException: If the value is invalid or missing when required.
        """
        value = self._build_from_optional(field_name, value)
        self.validate_built_value(field_name, value)
        return value

### `validate_built_value(field_name, value)`

Validate a fully built value, including None checks.

Raises:

Type | Description
--- | ---
`InvalidRequestException` | If value is None when not permitted, or fails subclass validation.

Source code in `src/mistral_common/tokens/tokenizers/model_settings_builder.py`

    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77

    @final
    def validate_built_value(self, field_name: str, value: Any) -> None:
        r"""Validate a fully built value, including None checks.

        Raises:
            InvalidRequestException: If value is None when not permitted, or fails subclass validation.
        """
        if value is None:
            if not (self.accepts_none and self.default is None):
                raise InvalidRequestException(f"{field_name} should be set for this model.")
        else:
            self._validate_built_value(field_name, value)

### `validate_default_accept_none()`

Ensure a default value is only set when accepts_none is True.

Source code in `src/mistral_common/tokens/tokenizers/model_settings_builder.py`

    41
    42
    43
    44
    45
    46
    47
    48

    @model_validator(mode="after")
    def validate_default_accept_none(self) -> "FieldBuilder":
        r"""Ensure a default value is only set when accepts_none is True."""
        if not self.accepts_none and self.default is not None:
            raise ValueError(
                f"Default values can only be defined for accepts_none fields {self.accepts_none=} {self.default=}"
            )
        return self

## `ModelSettingsBuilder(**data)`

Bases: `MistralBase`

Builder for ModelSettings to ensure only authorized values are used.

This class validates that model settings contain only authorized values for each field. It enforces a strict field matching approach where: - All fields in model settings must have corresponding builder fields. - All builder fields must have corresponding model settings fields. - Validation is performed only on matching fields. - Clear error messages are provided for field mismatches.

Attributes:

Name | Type | Description
--- | --- | ---
`reasoning_effort` | `EnumBuilder[ReasoningEffort] | None` | Builder for the allowed ReasoningEffort values, or None if unsupported.

Source code in `pydantic/main.py`

    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270

    def __init__(self, /, **data: Any) -> None:
        """Create a new model by parsing and validating input data from keyword arguments.

        Raises [`ValidationError`][pydantic_core.ValidationError] if the input data cannot be
        validated to form a valid model.

        `self` is explicitly positional-only to allow `self` as a field name.
        """
        # `__tracebackhide__` tells pytest and some other tools to omit this function from tracebacks
        __tracebackhide__ = True
        validated_self = self.__pydantic_validator__.validate_python(data, self_instance=self)
        if self is not validated_self:
            warnings.warn(
                'A custom validator is returning a value other than `self`.\n'
                "Returning anything other than `self` from a top level model validator isn't supported when validating via `__init__`.\n"
                'See the `model_validator` docs (https://docs.pydantic.dev/latest/concepts/validators/#model-validators) for more details.',
                stacklevel=2,
            )

### `build_settings(request)`

Build and validate a ModelSettings instance from a raw request.

Iterates over all known fields, applies the corresponding builder, and constructs a validated ModelSettings object.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`request` | `ChatCompletionRequest` | The incoming chat completion request. | required

Returns:

Type | Description
--- | ---
`ModelSettings` | A validated ModelSettings instance.

Raises:

Type | Description
--- | ---
`InvalidRequestException` | If any field value is invalid or unsupported.

Source code in `src/mistral_common/tokens/tokenizers/model_settings_builder.py`

    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186

    def build_settings(self, request: ChatCompletionRequest) -> ModelSettings:
        r"""Build and validate a ModelSettings instance from a raw request.

        Iterates over all known fields, applies the corresponding builder, and
        constructs a validated ModelSettings object.

        Args:
            request: The incoming chat completion request.

        Returns:
            A validated ModelSettings instance.

        Raises:
            InvalidRequestException: If any field value is invalid or unsupported.
        """
        dict_settings = {}
        for field_name in ModelSettingsBuilder.model_fields:
            # We have a CI test to ensure all fields match between ModelSettings and ModelSettingsEncoder.
            value = getattr(request, field_name)
            field_builder: FieldBuilder | None = getattr(self, field_name)
            if field_builder is not None:
                dict_settings[field_name] = field_builder.build_value(field_name, value)

        return ModelSettings.model_validate(dict_settings)

### `none()` `staticmethod`

Return a ModelSettingsBuilder with no field builders configured.

Source code in `src/mistral_common/tokens/tokenizers/model_settings_builder.py`

    158
    159
    160
    161

    @staticmethod
    def none() -> "ModelSettingsBuilder":
        r"""Return a ModelSettingsBuilder with no field builders configured."""
        return ModelSettingsBuilder()

### `validate_settings(settings)`

Validate that all fields in a ModelSettings instance match the configured builders.

Ensures that fields without a builder are unset, and fields with a builder hold a value that passes validation.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`settings` | `ModelSettings` | The ModelSettings instance to validate. | required

Raises:

Type | Description
--- | ---
`InvalidRequestException` | If a field is set but has no builder, or fails its builder.

Source code in `src/mistral_common/tokens/tokenizers/model_settings_builder.py`

    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207

    def validate_settings(self, settings: ModelSettings) -> None:
        r"""Validate that all fields in a ModelSettings instance match the configured builders.

        Ensures that fields without a builder are unset, and fields with a builder
        hold a value that passes validation.

        Args:
            settings: The ModelSettings instance to validate.

        Raises:
            InvalidRequestException: If a field is set but has no builder, or fails its builder.
        """
        for field_name in ModelSettingsBuilder.model_fields:
            value = getattr(settings, field_name)
            field_builder: FieldBuilder | None = getattr(self, field_name)
            if field_builder is None:
                if value is not None:
                    raise InvalidRequestException(f"{field_name} not supported for this model")
            else:
                field_builder.validate_built_value(field_name, value)

## `ValidatorType`

Bases: `str`, `Enum`

Enumeration of validator types.

Attributes:

Name | Type | Description
--- | --- | ---
`ENUM` |  | Indicates that the validator is for enum values.