Implementation:Mistralai Client python Message Models
| Knowledge Sources | |
|---|---|
| Domains | NLP, API_Design |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
Concrete tool for constructing typed conversation messages provided by the mistralai SDK's Pydantic model classes.
Description
The UserMessage, SystemMessage, and AssistantMessage classes are Pydantic models that represent individual messages in a conversation. Each accepts a content parameter (string or list of content chunks for multimodal input) and a role parameter that is auto-set based on the message type. These models validate inputs and serialize to the JSON format expected by the Mistral API.
Usage
Import these classes when constructing the message list for chat.complete(), chat.stream(), or any chat-based API call. Use SystemMessage for behavioral instructions, UserMessage for user input, and AssistantMessage for including prior model responses in multi-turn conversations.
Code Reference
Source Location
- Repository: client-python
- File: src/mistralai/client/models/usermessage.py (L1-60), systemmessage.py (L1-35), assistantmessage.py (L1-77)
Signature
class UserMessage(BaseModel):
content: Union[str, List[ContentChunk]]
role: Literal["user"] = "user"
class SystemMessage(BaseModel):
content: Union[str, List[SystemMessageContentChunks]]
role: Literal["system"] = "system"
class AssistantMessage(BaseModel):
content: Optional[Union[str, List[ContentChunk]]] = None
tool_calls: Optional[List[ToolCall]] = None
prefix: Optional[bool] = None
role: Literal["assistant"] = "assistant"
Import
from mistralai.models import UserMessage, SystemMessage, AssistantMessage
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| content | Union[str, List[ContentChunk]] | Yes | Text string or multimodal content chunks |
| role | Literal["user"/"system"/"assistant"] | No | Auto-set by message class |
Outputs
| Name | Type | Description |
|---|---|---|
| Message object | UserMessage / SystemMessage / AssistantMessage | Validated Pydantic model ready for API serialization |
Usage Examples
Basic Text Messages
from mistralai.models import UserMessage, SystemMessage
messages = [
SystemMessage(content="You are a helpful coding assistant."),
UserMessage(content="Explain Python decorators."),
]
Multi-turn Conversation
from mistralai.models import UserMessage, SystemMessage, AssistantMessage
messages = [
SystemMessage(content="You are a math tutor."),
UserMessage(content="What is 2+2?"),
AssistantMessage(content="2+2 equals 4."),
UserMessage(content="And 3+3?"),
]