Implementation:Mistralai Client python Chat Complete
| Knowledge Sources | |
|---|---|
| Domains | NLP, LLM_Inference |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
Concrete tool for sending synchronous and asynchronous chat completion requests provided by the mistralai SDK's Chat resource.
Description
The Chat.complete() and Chat.complete_async() methods send a list of conversation messages to the Mistral API and return a complete ChatCompletionResponse. The methods handle request serialization, HTTP transport, error handling, and response deserialization. They support structured output via response_format, function calling via tools, and safety prompts via safe_prompt.
Usage
Call client.chat.complete() for synchronous usage or client.chat.complete_async() with await for async usage. This is the primary method for non-streaming chat interactions.
Code Reference
Source Location
- Repository: client-python
- File: src/mistralai/client/chat.py
- Lines: L108-283 (sync), L285-460 (async)
Signature
class Chat:
def complete(
self,
*,
model: str,
messages: List[ChatCompletionRequestMessage],
temperature: Optional[float] = None,
top_p: Optional[float] = None,
max_tokens: Optional[int] = None,
response_format: Optional[ResponseFormat] = None,
tools: Optional[List[Tool]] = None,
tool_choice: Optional[Union[ToolChoiceEnum, ToolChoice]] = None,
safe_prompt: Optional[bool] = None,
random_seed: Optional[int] = None,
prediction: Optional[Prediction] = None,
parallel_tool_calls: Optional[bool] = None,
) -> ChatCompletionResponse:
...
async def complete_async(
self,
*,
model: str,
messages: List[ChatCompletionRequestMessage],
# Same parameters as complete()
) -> ChatCompletionResponse:
...
Import
from mistralai import Mistral
# Access via: client.chat.complete(...)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| model | str | Yes | Model ID (e.g., "mistral-large-latest") |
| messages | List[ChatCompletionRequestMessage] | Yes | Conversation messages |
| temperature | Optional[float] | No | Sampling temperature (0.0-0.7) |
| top_p | Optional[float] | No | Nucleus sampling threshold |
| max_tokens | Optional[int] | No | Maximum output tokens |
| response_format | Optional[ResponseFormat] | No | JSON or text output format |
| tools | Optional[List[Tool]] | No | Tool definitions for function calling |
| safe_prompt | Optional[bool] | No | Inject safety system prompt |
Outputs
| Name | Type | Description |
|---|---|---|
| response | ChatCompletionResponse | Contains choices, usage info, model metadata |
| response.choices | List[ChatCompletionChoice] | Generated completions (usually 1) |
| response.usage | UsageInfo | Token counts (prompt, completion, total) |
Usage Examples
Synchronous Chat
import os
from mistralai import Mistral
from mistralai.models import UserMessage
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
response = client.chat.complete(
model="mistral-large-latest",
messages=[
UserMessage(content="What is the capital of France?"),
],
)
print(response.choices[0].message.content)
Async Chat
import asyncio
import os
from mistralai import Mistral
from mistralai.models import UserMessage
async def main():
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])
response = await client.chat.complete_async(
model="mistral-large-latest",
messages=[
UserMessage(content="Explain quantum computing briefly."),
],
)
print(response.choices[0].message.content)
asyncio.run(main())