mistral_common.protocol.instruct.request
ChatCompletionRequest(**data)
Bases: BaseCompletionRequest, Generic[ChatMessageType]
Request for a chat completion.
Main entry point for creating instruct model requests. Contains all configuration for a single chat completion including messages, tools, and model settings.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
str | None
|
Name of the model to use. Required in serving mode; optional in
other modes. If |
messages |
list[ChatMessageType]
|
List of chat messages (user, assistant, system, tool). Must not be empty. |
response_format |
ResponseFormat
|
Format of the response (text or JSON). Defaults to text. |
tools |
list[Tool] | None
|
List of available tools for the model to use. If |
tool_choice |
ToolChoice
|
Strategy for tool selection. Options: auto (model decides), none (no tools), any/required (deprecated, use required). Default: auto. |
parallel_tool_calls |
bool
|
Whether the model may call multiple tools in one
response. Defaults to |
truncate_for_context_length |
bool
|
If |
reasoning_effort |
ReasoningEffort | None
|
Controls reasoning effort (one of "none", "minimal", "low", "medium", "high",
"xhigh", or "max"). Requires tokenizer >= v15. If |
Examples:
>>> from mistral_common.protocol.instruct.messages import UserMessage, AssistantMessage
>>> from mistral_common.protocol.instruct.tool_calls import ToolTypes, Function
>>> request = ChatCompletionRequest(
... messages=[
... UserMessage(content="Hello!"),
... AssistantMessage(content="Hi! How can I help you?"),
... ],
... response_format=ResponseFormat(type=ResponseFormats.text),
... tools=[Tool(type=ToolTypes.function, function=Function(name="get_weather", parameters={}))],
... tool_choice=ToolChoiceEnum.auto,
... truncate_for_context_length=True,
... )
Source code in pydantic/main.py
from_openai(messages, tools=None, continue_final_message=False, **kwargs)
classmethod
Create a ChatCompletionRequest from OpenAI ChatCompletion request format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[dict[str, str | list[dict[str, str | dict[str, Any]]]]]
|
List of message dicts in OpenAI format. Each dict must have a "role" key and optionally "content" or other role-specific fields. |
required |
tools
|
list[dict[str, Any]] | None
|
List of tool dicts in OpenAI format, or |
None
|
continue_final_message
|
bool
|
If |
False
|
**kwargs
|
Any
|
Additional request parameters. Supports both OpenAI names
(e.g., "seed") and mistral-common names (e.g., |
{}
|
Returns:
| Type | Description |
|---|---|
ChatCompletionRequest
|
A ChatCompletionRequest instance with messages and tools converted |
ChatCompletionRequest
|
from OpenAI format. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If both "seed" and |
Source code in src/mistral_common/protocol/instruct/request.py
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 423 424 425 426 427 428 429 430 | |
to_openai(reasoning_field_format=None, **kwargs)
Convert this request to OpenAI ChatCompletion API format.
Note
Interleaved thinking (a ThinkChunk after other content in an assistant message) is not supported: only leading ThinkChunks can be converted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reasoning_field_format
|
ReasoningFieldFormat | None
|
Format for converting thinking/thinking chunks in
assistant messages to OpenAI's reasoning field. If |
None
|
**kwargs
|
Any
|
Additional OpenAI-specific parameters to include in the output
(e.g., stream, temperature, |
{}
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary matching the OpenAI ChatCompletion request schema. |
Raises:
| Type | Description |
|---|---|
InvalidAssistantMessageException
|
If an assistant message has interleaved thinking. |
ValueError
|
If kwargs contains duplicate or conflicting keys. |
NotImplementedError
|
If |
Examples:
>>> from mistral_common.protocol.instruct.messages import UserMessage
>>> from mistral_common.protocol.instruct.tool_calls import Tool, Function
>>> request = ChatCompletionRequest(messages=[UserMessage(content="Hello, how are you?")], temperature=0.15)
>>> request.to_openai(stream=True)
{'temperature': 0.15, 'top_p': 1.0, 'response_format': {'type': 'text'}, 'continue_final_message': False, 'messages': [{'role': 'user', 'content': 'Hello, how are you?'}], 'stream': True}
>>> request = ChatCompletionRequest(messages=[UserMessage(content="Hello, how are you?")], tools=[
... Tool(function=Function(
... name="get_current_weather",
... description="Get the current weather in a given location",
... parameters={
... "type": "object",
... "properties": {
... "location": {
... "type": "string",
... "description": "The city and state, e.g. San Francisco, CA",
... },
... "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
... },
... "required": ["location"],
... },
... ),
... )])
>>> request.to_openai()
{'temperature': 0.7, 'top_p': 1.0, 'response_format': {'type': 'text'}, 'continue_final_message': False, 'messages': [{'role': 'user', 'content': 'Hello, how are you?'}], 'tools': [{'type': 'function', 'function': {'name': 'get_current_weather', 'description': 'Get the current weather in a given location', 'parameters': {'type': 'object', 'properties': {'location': {'type': 'string', 'description': 'The city and state, e.g. San Francisco, CA'}, 'unit': {'type': 'string', 'enum': ['celsius', 'fahrenheit']}}, 'required': ['location']}, 'strict': False}}], 'parallel_tool_calls': True, 'tool_choice': 'auto'}
Source code in src/mistral_common/protocol/instruct/request.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 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 | |
InstructRequest(**data)
Bases: MistralBase, Generic[ChatMessageType, ToolType]
Internal representation of a validated instruct request ready for tokenization.
This class represents the fully normalized and validated request that will be passed to the tokenizer. It separates system prompts from regular messages and handles tool configuration.
Note
This class is intended for internal use only. External users should use ChatCompletionRequest to build requests and convert to/from OpenAI format.
Attributes:
| Name | Type | Description |
|---|---|---|
messages |
list[ChatMessageType]
|
List of chat messages (user and assistant only; system is separate). |
system_prompt |
str | None
|
System prompt string, or |
available_tools |
list[ToolType] | None
|
List of tools available to the assistant. If |
truncate_at_max_tokens |
int | None
|
Maximum token count for truncation. If |
settings |
ModelSettings
|
Model configuration settings. Defaults to all |
Examples:
>>> from mistral_common.protocol.instruct.messages import UserMessage, SystemMessage
>>> request = InstructRequest(
... messages=[UserMessage(content="Hello, how are you?")],
... system_prompt="You are a helpful assistant."
... )
Source code in pydantic/main.py
JsonSchema(**data)
Bases: MistralBase
A named JSON schema for structured responses.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The schema name. |
description |
str | None
|
An optional description of the schema. |
custom_schema |
dict[str, Any]
|
The JSON schema (aliased |
strict |
bool
|
Whether grammar generation uses the custom schema. When |
Examples:
Source code in pydantic/main.py
validate_custom_schema(value)
classmethod
Validate the schema against JSON Schema Draft 7.
Source code in src/mistral_common/protocol/instruct/request.py
ModelSettings(**data)
Bases: MistralBase
Model configuration settings for instruct requests.
Encapsulates model-specific settings that influence inference behavior. Currently supports reasoning effort and response format configuration.
Attributes:
| Name | Type | Description |
|---|---|---|
reasoning_effort |
ReasoningEffort | None
|
Controls reasoning effort. If |
json_schema |
dict[str, Any] | None
|
The JSON schema to enforce on the response, derived from the request's
response format. If |
Source code in pydantic/main.py
none()
staticmethod
Create a ModelSettings instance with all fields set to None.
Returns:
| Type | Description |
|---|---|
ModelSettings
|
An instance with all fields set to |
ReasoningEffort
Bases: StrEnum
Controls the amount of reasoning effort the model applies during generation.
Attributes:
| Name | Type | Description |
|---|---|---|
none |
No reasoning effort; model generates responses directly. |
|
minimal |
Minimal reasoning effort, with only a small amount of additional computation. |
|
low |
Low reasoning effort; the model spends little compute on reasoning. |
|
medium |
Medium reasoning effort, between low and high. |
|
high |
High reasoning effort for complex tasks; model spends more compute on reasoning before generating the final response. |
|
xhigh |
Extra-high reasoning effort, above high, for the most demanding tasks. |
|
max |
Maximum reasoning effort the model supports. |
Note
Supported for tokenizer >= v15 only. Earlier versions will raise an error.
ResponseFormat(**data)
Bases: MistralBase
Configuration for the response format.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
ResponseFormats
|
The response format type. Use |
json_schema |
JsonSchema | None
|
The JSON schema when |
Examples:
Source code in pydantic/main.py
get_schema(purpose)
Resolve the response schema for grammar generation or model settings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
purpose
|
SchemaRenderingMode
|
Whether the schema is used to constrain a grammar or encode model settings. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any] | None
|
The schema selected for this purpose, or None for |
Source code in src/mistral_common/protocol/instruct/request.py
validate_schema_presence()
Require a schema when the response format selects json_schema.
Source code in src/mistral_common/protocol/instruct/request.py
ResponseFormats
Bases: StrEnum
Enum of the different formats for an instruct response.
Attributes:
| Name | Type | Description |
|---|---|---|
text |
Response will be plain text. |
|
json_object |
Response will be a valid JSON object. |
|
json_schema |
Response will follow a custom JSON schema. |
Examples:
SchemaRenderingMode
Bases: StrEnum
Select how a response-format schema is applied.
Attributes:
| Name | Type | Description |
|---|---|---|
grammar |
The schema constrains decoding; non-strict schemas fall back to any valid JSON. |
|
model_settings |
The schema is encoded in model settings unchanged. |