mistral_common.protocol.instruct.messages
AssistantMessage(**data)
Bases: BaseMessage
A message from the assistant in a conversation.
Assistant messages can contain the model's response text and/or tool calls.
Attributes:
| Name | Type | Description |
|---|---|---|
content |
str | list[ContentChunk] | None
|
The message content. Can be:
- |
tool_calls |
list[ToolCall] | None
|
List of ToolCall objects if the assistant called tools.
If |
prefix |
bool
|
If |
Examples:
Source code in pydantic/main.py
from_openai(openai_message)
classmethod
Create an AssistantMessage from OpenAI format.
Handles conversion of OpenAI's reasoning/reasoning_content fields to
Mistral's ThinkChunk format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
openai_message
|
dict[str, Any]
|
Dictionary matching OpenAI's assistant message schema.
Can contain "content", |
required |
Returns:
| Type | Description |
|---|---|
AssistantMessage
|
AssistantMessage instance with thinking content converted to ThinkChunk |
AssistantMessage
|
and |
Raises:
| Type | Description |
|---|---|
InvalidAssistantMessageException
|
If message has both thinking chunks
in |
ValueError
|
If both |
Source code in src/mistral_common/protocol/instruct/messages.py
to_openai(reasoning_field_format=None)
Convert this assistant message to OpenAI format.
Handles conversion of thinking chunks to various OpenAI-compatible formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reasoning_field_format
|
ReasoningFieldFormat | None
|
Format for serializing thinking content:
- |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with "role" set to "assistant", and |
Raises:
| Type | Description |
|---|---|
InvalidAssistantMessageException
|
If ThinkChunks are not leading (must appear before any other content chunks). |
RuntimeError
|
If content chunks are not all ThinkChunk or TextChunk
when using |
ValueError
|
If |
Source code in src/mistral_common/protocol/instruct/messages.py
BaseMessage(**data)
Bases: MistralBase
Abstract base class for all chat message types.
Provides common functionality for message serialization and validation.
Subclasses must implement to_openai() and from_openai().
Attributes:
| Name | Type | Description |
|---|---|---|
role |
Literal[system, user, assistant, tool]
|
The role of this message (system, user, assistant, or tool). |
Source code in pydantic/main.py
from_openai(openai_message)
classmethod
Create a message instance from OpenAI format.
Must be implemented by concrete subclasses.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
openai_message
|
dict[str, Any]
|
Dictionary matching OpenAI's message schema. |
required |
Returns:
| Type | Description |
|---|---|
BaseMessage
|
Message instance of the appropriate subclass. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always, as this is an abstract method. |
Source code in src/mistral_common/protocol/instruct/messages.py
to_openai()
Convert this message to OpenAI format.
Must be implemented by concrete subclasses.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary matching OpenAI's message schema. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always, as this is an abstract method. |
Source code in src/mistral_common/protocol/instruct/messages.py
FinetuningAssistantMessage(**data)
Bases: AssistantMessage
Assistant message with a weight for finetuning.
Extends AssistantMessage with a weight parameter used during finetuning to control how much this message contributes to the training loss.
Attributes:
| Name | Type | Description |
|---|---|---|
weight |
float | None
|
The weight for this message during finetuning. If |
Examples:
Source code in pydantic/main.py
ReasoningFieldFormat
Format options for serializing thinking content in AssistantMessage.to_openai().
Controls how leading ThinkChunk content is represented in the OpenAI output.
Attributes:
| Name | Type | Description |
|---|---|---|
thinking_chunks |
Keep thinking content as inline chunks (Mistral convention). This preserves the chunk structure in the output. |
|
reasoning |
Use a flat "reasoning" string field (vLLM convention). All thinking content is concatenated into a single string. |
|
reasoning_content |
Use a flat |
Roles
Enum of valid message roles in a conversation.
Attributes:
| Name | Type | Description |
|---|---|---|
system |
System message providing context or instructions to the assistant. |
|
user |
User message containing the user's input or query. |
|
assistant |
Assistant message containing the model's response. |
|
tool |
Tool message containing output from a tool/function call. |
Examples:
SystemMessage(**data)
Bases: BaseMessage
A system message providing context or instructions to the assistant.
System messages set the behavior, persona, or context for the assistant. They are typically provided at the start of a conversation.
Attributes:
| Name | Type | Description |
|---|---|---|
content |
str | list[ContentChunk]
|
The message content. Can be: - str: Plain text instructions - list of ContentChunk: Text, audio, or thinking content Valid chunk types: TextChunk, AudioChunk, ThinkChunk |
Examples:
Source code in pydantic/main.py
from_openai(openai_message)
classmethod
Create a SystemMessage from OpenAI format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
openai_message
|
dict[str, Any]
|
Dictionary with "role" and "content" keys. |
required |
Returns:
| Type | Description |
|---|---|
SystemMessage
|
SystemMessage instance with content deserialized from OpenAI format. |
Source code in src/mistral_common/protocol/instruct/messages.py
ToolMessage(**data)
Bases: BaseMessage
A message from a tool containing the result of a function call.
Tool messages are sent by tools in response to ToolCall objects from the assistant. They contain the tool's output.
Attributes:
| Name | Type | Description |
|---|---|---|
content |
str | list[ContentChunk]
|
The tool's output content. Can be: - str: Plain text output - list of ContentChunk: Multimodal output (text, images, audio) Valid chunk types: TextChunk, ImageChunk, ImageURLChunk, AudioChunk, AudioURLChunk |
tool_call_id |
str | None
|
The unique ID of the tool call this message responds to. Required for proper association with the assistant's tool call. |
name |
str | None
|
The name of the tool (deprecated in V3 tokenization). Kept for backwards compatibility but not used in newer tokenizers. |
Examples:
Source code in pydantic/main.py
from_openai(openai_message)
classmethod
Create a ToolMessage from OpenAI format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
openai_message
|
dict[str, Any]
|
Dictionary with "role", |
required |
Returns:
| Type | Description |
|---|---|
ToolMessage
|
ToolMessage instance with content and |
Source code in src/mistral_common/protocol/instruct/messages.py
to_openai()
Convert this tool message to OpenAI format.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with "role" set to "tool", |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If |
Source code in src/mistral_common/protocol/instruct/messages.py
UserMessage(**data)
Bases: BaseMessage
A message from the user in a conversation.
User messages can contain text, images, audio, or combinations thereof.
Attributes:
| Name | Type | Description |
|---|---|---|
content |
str | list[ContentChunk]
|
The message content. Can be: - str: Plain text message - list of ContentChunk: Multimodal content (text, images, audio) Valid chunk types: TextChunk, ImageChunk, ImageURLChunk, AudioChunk, AudioURLChunk |
Examples:
Source code in pydantic/main.py
from_openai(openai_message)
classmethod
Create a UserMessage from OpenAI format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
openai_message
|
dict[str, Any]
|
Dictionary with "role" and "content" keys. |
required |
Returns:
| Type | Description |
|---|---|
UserMessage
|
UserMessage instance with content deserialized from OpenAI format. |