Mistral AI Python SDK
Official Python SDK for Mistral AI API. Has gone through two major breaking rewrites: v0 → v1 (MistralClient removed) and v1 → v2 (in pre-release on GitHub, not yet on PyPI). Most LLM-generated code references v0 patterns which no longer work.
Warnings
- breaking MistralClient and MistralAsyncClient are removed. Use Mistral class for both sync and async.
- breaking client.chat() call signature removed. Now client.chat.complete() for sync, client.chat.complete_async() for async.
- breaking Streaming chunk structure changed. chunk.choices[0] no longer works — must use chunk.data.choices[0].
- breaking ChatMessage class removed from mistralai.models.chat_completion. Use plain dicts or new typed message classes.
- breaking tool_call_id is now mandatory in messages with role 'tool'. Omitting it causes API error.
- gotcha pip install mistralai installs v1 (stable). GitHub main branch shows v2 docs which are NOT on PyPI yet. Don't follow GitHub README — it documents unreleased v2.
- gotcha Agents API requires Python 3.10+ and mistralai[agents] extra. Base install does not include it.
- gotcha Azure and GCP deployments require separate packages: mistralai-azure and mistralai-gcp. Not included in base mistralai.
Install
-
pip install mistralai -
uv add mistralai -
pip install 'mistralai[agents]' -
pip install mistralai-azure -
pip install mistralai-gcp
Imports
- Mistral
from mistralai import Mistral
- chat completion
client.chat.complete(model=..., messages=[...])
- streaming chunk
chunk.data.choices[0].delta.content
- ChatMessage
{'role': 'user', 'content': '...'}
Quickstart
import os
from mistralai import Mistral
client = Mistral(api_key=os.environ['MISTRAL_API_KEY'])
response = client.chat.complete(
model='mistral-large-latest',
messages=[{'role': 'user', 'content': 'Hello'}]
)
print(response.choices[0].message.content)