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. |
truncate_for_context_length |
bool
|
If |
reasoning_effort |
ReasoningEffort | None
|
Controls reasoning effort (none or high). Requires tokenizer
|
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
to_openai(reasoning_field_format=None, **kwargs)
Convert this request to OpenAI ChatCompletion API format.
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 |
|---|---|
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?'}], 'tool_choice': 'auto', '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}}], 'tool_choice': 'auto'}
Source code in src/mistral_common/protocol/instruct/request.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 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 | |
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
ModelSettings(**data)
Bases: MistralBase
Model configuration settings for instruct requests.
Encapsulates model-specific settings that influence inference behavior. Currently supports reasoning effort configuration.
Attributes:
| Name | Type | Description |
|---|---|---|
reasoning_effort |
ReasoningEffort | None
|
Controls reasoning effort. If |
Source code in pydantic/main.py
none()
staticmethod
Create a ModelSettings instance with all fields set to None.
Returns:
| Type | Description |
|---|---|
ModelSettings
|
ModelSettings with |
ReasoningEffort
Controls the amount of reasoning effort the model applies during generation.
Attributes:
| Name | Type | Description |
|---|---|---|
none |
No reasoning effort; model generates responses directly. |
|
high |
High reasoning effort for complex tasks; model spends more compute on reasoning before generating the final response. |
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 |
Examples: