Implementation:Mistralai Client python ToolMessage Model
| Knowledge Sources | |
|---|---|
| Domains | Function_Calling, API_Design |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
Concrete tool for packaging function execution results into tool messages for submission back to the model provided by the ToolMessage model.
Description
The ToolMessage Pydantic model wraps function execution results for the multi-turn tool calling loop. It has a fixed role of "tool", a content field (string or content chunks) containing the function result, a tool_call_id that must match the ToolCall.id from the model's response, and an optional name field for the function name.
Usage
Create a ToolMessage for each executed tool call result. Include it in the messages list alongside the original conversation and the assistant's tool-calling message when making the follow-up chat.complete() call.
Code Reference
Source Location
- Repository: client-python
- File: src/mistralai/client/models/toolmessage.py
- Lines: L1-76
Signature
class ToolMessage(BaseModel):
content: Union[str, List[ContentChunk]]
tool_call_id: Optional[str] = None
name: Optional[str] = None
role: Literal["tool"] = "tool"
Import
from mistralai.models import ToolMessage
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| content | Union[str, List[ContentChunk]] | Yes | Function execution result as string |
| tool_call_id | Optional[str] | Yes (practical) | Must match ToolCall.id from model response |
| name | Optional[str] | No | Function name |
Outputs
| Name | Type | Description |
|---|---|---|
| ToolMessage object | ToolMessage | Validated tool result message for follow-up API call |
Usage Examples
Complete Function Calling Loop
import json
from mistralai.models import UserMessage, ToolMessage
# Step 1: Initial request with tools
messages = [UserMessage(content="What's the status of transaction T001?")]
response = client.chat.complete(
model="mistral-large-latest",
messages=messages,
tools=[payment_status_tool],
)
# Step 2: Parse tool calls and execute
assistant_msg = response.choices[0].message
tool_messages = []
for tool_call in assistant_msg.tool_calls:
fn = registry[tool_call.function.name]
args = json.loads(tool_call.function.arguments)
result = fn(**args)
tool_messages.append(
ToolMessage(
content=result,
tool_call_id=tool_call.id,
name=tool_call.function.name,
)
)
# Step 3: Follow-up request with tool results
messages = messages + [assistant_msg] + tool_messages
final_response = client.chat.complete(
model="mistral-large-latest",
messages=messages,
tools=[payment_status_tool],
)
print(final_response.choices[0].message.content)