Set your Mistral API Key as an environment variable. You only need to do this once.
# set Mistral API Key (using zsh for example)
$ echo 'export MISTRAL_API_KEY=[your_key_here]' >> ~/.zshenv
# reload the environment (or just quit and open a new terminal)
$ source ~/.zshenv
Service-account token
Workloads with a mounted service-account token can set MISTRAL_SA_TOKEN_PATH instead. The file is re-read on every request, so rotation is picked up. Credentials resolve in this order:
an Authorization header passed per request via http_headers
Mistral(api_key=...), which also accepts a callable
the token at MISTRAL_SA_TOKEN_PATH
MISTRAL_API_KEY
An unreadable or empty token file raises ServiceAccountTokenError.
Summary
Mistral AI API: Our Chat Completion and Embeddings APIs specification. Create your account on La Plateforme to get access and read the docs to learn how to use it.
Once a Python version reaches its official end of life date, a 3-month grace period is provided for users to upgrade. Following this grace period, the minimum python version supported in the SDK will be updated.
The SDK can be installed with uv, pip, or poetry package managers.
uv
uv is a fast Python package installer and resolver, designed as a drop-in replacement for pip and pip-tools. It's recommended for its speed and modern Python tooling capabilities.
uv add mistralai
PIP
PIP is the default package installer for Python, enabling easy installation and management of packages from PyPI via the command line.
pip install mistralai
Poetry
Poetry is a modern tool that simplifies dependency management and package publishing by using a single pyproject.toml file to handle project metadata and dependencies.
poetry add mistralai
Shell and script usage with uv
You can use this SDK in a Python shell with uv and the uvx command that comes with it like so:
uvx --from mistralai python
It's also possible to write a standalone Python script without needing to set up a whole project like so:
Additional mistralai-* packages (e.g. mistralai-workflows) can be installed separately and are available under the mistralai namespace:
pip install mistralai-workflows
SDK Example Usage
Create Chat Completions
This example shows how to create chat completions.
# Synchronous Example
from mistralai.client import Mistral
import os
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = mistral.chat.complete(model="mistral-large-latest", messages=[
{
"role": "user",
"content": "Who is the best French painter? Answer in one short sentence.",
},
], stream=False, response_format={
"type": "text",
})
# Handle response
print(res)
The same SDK client can also be used to make asynchronous requests by importing asyncio.
# Asynchronous Example
import asyncio
from mistralai.client import Mistral
import os
async def main():
async with Mistral(
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = await mistral.chat.complete_async(model="mistral-large-latest", messages=[
{
"role": "user",
"content": "Who is the best French painter? Answer in one short sentence.",
},
], stream=False, response_format={
"type": "text",
})
# Handle response
print(res)
asyncio.run(main())
Upload a file
This example shows how to upload a file.
# Synchronous Example
from mistralai.client import Mistral
import os
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = mistral.files.upload(file={
"file_name": "example.file",
"content": open("example.file", "rb"),
}, visibility="workspace", purpose="batch")
# Handle response
print(res)
The same SDK client can also be used to make asynchronous requests by importing asyncio.
# Asynchronous Example
import asyncio
from mistralai.client import Mistral
import os
async def main():
async with Mistral(
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = await mistral.files.upload_async(file={
"file_name": "example.file",
"content": open("example.file", "rb"),
}, visibility="workspace", purpose="batch")
# Handle response
print(res)
asyncio.run(main())
Create Agents Completions
This example shows how to create agents completions.
# Synchronous Example
from mistralai.client import Mistral
import os
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = mistral.agents.complete(messages=[
{
"role": "user",
"content": "Who is the best French painter? Answer in one short sentence.",
},
], agent_id="<id>", stream=False, response_format={
"type": "text",
})
# Handle response
print(res)
The same SDK client can also be used to make asynchronous requests by importing asyncio.
# Asynchronous Example
import asyncio
from mistralai.client import Mistral
import os
async def main():
async with Mistral(
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = await mistral.agents.complete_async(messages=[
{
"role": "user",
"content": "Who is the best French painter? Answer in one short sentence.",
},
], agent_id="<id>", stream=False, response_format={
"type": "text",
})
# Handle response
print(res)
asyncio.run(main())
Create Embedding Request
This example shows how to create embedding request.
# Synchronous Example
from mistralai.client import Mistral
import os
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = mistral.embeddings.create(model="mistral-embed", inputs=[
"Embed this sentence.",
"As well as this one.",
])
# Handle response
print(res)
The same SDK client can also be used to make asynchronous requests by importing asyncio.
# Asynchronous Example
import asyncio
from mistralai.client import Mistral
import os
async def main():
async with Mistral(
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = await mistral.embeddings.create_async(model="mistral-embed", inputs=[
"Embed this sentence.",
"As well as this one.",
])
# Handle response
print(res)
asyncio.run(main())
More examples
You can run the examples in the examples/ directory using uv run.
import os
from mistralai.azure.client import MistralAzure
# The SDK automatically injects api-version as a query parameter
client = MistralAzure(
api_key=os.environ["AZURE_API_KEY"],
server_url=os.environ["AZURE_ENDPOINT"],
api_version="2024-05-01-preview", # Optional, this is the default
)
res = client.chat.complete(
model=os.environ["AZURE_MODEL"],
messages=[
{
"role": "user",
"content": "Hello there!",
}
],
)
print(res.choices[0].message.content)
Google Cloud
Prerequisites
Before you begin, you will need to create a Google Cloud project and enable the Mistral API. To do this, follow the instructions here.
To run this locally you will also need to ensure you are authenticated with Google Cloud. You can do this by running
gcloud auth application-default login
Step 1: Install
pip install mistralai
# For GCP authentication support (required):
pip install "mistralai[gcp]"
Server-sent events are used to stream content from certain
operations. These operations will expose the stream as Generator that
can be consumed using a simple for loop. The loop will
terminate when the server no longer has any events to send and closes the
underlying connection.
The stream is also a Context Manager and can be used with the with statement and will close the
underlying connection when the context is exited.
from mistralai.client import Mistral
import os
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = mistral.beta.conversations.start_stream(inputs=[
{
"object": "entry",
"type": "function.result",
"tool_call_id": "<id>",
"result": "<value>",
},
], completion_args={
"response_format": {
"type": "text",
},
})
with res as event_stream:
for event in event_stream:
# handle event
print(event, flush=True)
Json Streaming
Json Streaming (jsonl / x-ndjson) content type can be used to stream content from certain operations. These operations will expose the stream as Generator that
can be consumed using a simple for loop. The loop will
terminate when the server no longer has any events to send and closes the
underlying connection.
The stream is also a Context Manager and can be used with the with statement and will close the
underlying connection when the context is exited.
from mistralai.client import Mistral
import os
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = mistral.beta.context.search_indexes.generate_index_summary(language="en", index_name="<value>", force=False)
with res as jsonl_stream:
for event in jsonl_stream:
# handle event
print(event, flush=True)
Pagination
Some of the endpoints in this SDK support pagination. To use pagination, you make your SDK calls as usual, but the
returned response object will have a Next method that can be called to pull down the next group of results. If the
return value of Next is None, then there are no more pages to be fetched.
Here's an example of one such pagination call:
from mistralai.client import Mistral
import os
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = mistral.beta.prompts.list()
while res is not None:
# Handle items
res = res.next()
File uploads
Certain SDK methods accept file objects as part of a request body or multi-part request. It is possible and typically recommended to upload files as a stream rather than reading the entire contents into memory. This avoids excessive memory consumption and potentially crashing with out-of-memory errors when working with very large files. The following example demonstrates how to attach a file stream to a request.
[!TIP]
For endpoints that handle file uploads bytes arrays can also be used. However, using streams is recommended for large files.
from mistralai.client import Mistral
import os
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = mistral.beta.libraries.documents.upload(library_id="a02150d9-5ee0-4877-b62c-28b1fcdf3b76", file={
"file_name": "example.file",
"content": open("example.file", "rb"),
})
# Handle response
print(res)
Retries
Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.
To change the default retry strategy for a single API call, simply provide a RetryConfig object to the call:
from mistralai.client import Mistral
from mistralai.client.utils import BackoffStrategy, RetryConfig
import os
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = mistral.beta.prompts.list(,
RetryConfig("backoff", BackoffStrategy(1, 50, 1.1, 100), False))
while res is not None:
# Handle items
res = res.next()
If you'd like to override the default retry strategy for all operations that support retries, you can use the retry_config optional parameter when initializing the SDK:
from mistralai.client import Mistral
from mistralai.client.utils import BackoffStrategy, RetryConfig
import os
with Mistral(
retry_config=RetryConfig("backoff", BackoffStrategy(1, 50, 1.1, 100), False),
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = mistral.beta.prompts.list()
while res is not None:
# Handle items
res = res.next()
Error Handling
MistralError is the base class for all HTTP error responses. It has the following properties:
Property
Type
Description
err.message
str
Error message
err.status_code
int
HTTP response status code eg 404
err.headers
httpx.Headers
HTTP response headers
err.body
str
HTTP body. Can be empty string if no body is returned.
err.raw_response
httpx.Response
Raw HTTP response
err.data
Optional. Some errors may contain structured data. See Error Classes.
Example
from mistralai.client import Mistral, errors
import os
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = None
try:
res = mistral.beta.service_accounts.create(name="<value>", workspace_id="cf2146d0-158c-4b19-b8b1-ca0c68f41143")
# Handle response
print(res)
except errors.MistralError as e:
# The base class for HTTP error responses
print(e.message)
print(e.status_code)
print(e.body)
print(e.headers)
print(e.raw_response)
# Depending on the method different errors may be thrown
if isinstance(e, errors.HTTPValidationError):
print(e.data.detail) # Optional[List[models.ValidationError]]
Error Classes
Primary error:
MistralError: The base class for HTTP error responses.
ResponseValidationError: Type mismatch between the response data and the expected Pydantic model. Provides access to the Pydantic validation error via the cause attribute.
You can override the default server globally by passing a server name to the server: str optional parameter when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the names associated with the available servers:
Name
Server
Description
global
https://api.mistral.ai
Global Production server
eu
https://api.eu.mistral.ai
EU Production server
us
https://api.us.mistral.ai
US Production server
Example
from mistralai.client import Mistral
import os
with Mistral(
server="global",
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = mistral.beta.prompts.list()
while res is not None:
# Handle items
res = res.next()
Override Server URL Per-Client
The default server can also be overridden globally by passing a URL to the server_url: str optional parameter when initializing the SDK client instance. For example:
from mistralai.client import Mistral
import os
with Mistral(
server_url="https://api.mistral.ai",
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = mistral.beta.prompts.list()
while res is not None:
# Handle items
res = res.next()
Custom HTTP Client
The Python SDK makes API calls using the httpx2 HTTP library. In order to provide a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration, you can initialize the SDK client with your own HTTP client instance.
Depending on whether you are using the sync or async version of the SDK, you can pass an instance of HttpClient or AsyncHttpClient respectively, which are Protocol's ensuring that the client has the necessary methods to make API calls.
This allows you to wrap the client with your own custom logic, such as adding custom headers, logging, or error handling, or you can just pass an instance of httpx.Client or httpx.AsyncClient directly.
For example, you could specify a header for every request that this sdk makes as follows:
from mistralai.client import Mistral
import httpx2 as httpx
http_client = httpx.Client(headers={"x-custom-header": "someValue"})
s = Mistral(client=http_client)
or you could wrap the client with your own custom logic:
This SDK is generated against httpx2, Pydantic's maintained fork of httpx. The fork keeps the same public API, so everything above applies unchanged - httpx2.Client and httpx2.AsyncClient are what the SDK expects.
Authentication
Per-Client Security Schemes
This SDK supports the following security scheme globally:
Name
Type
Scheme
Environment Variable
api_key
http
HTTP Bearer
MISTRAL_API_KEY
To authenticate with the API the api_key parameter must be set when initializing the SDK client instance. For example:
from mistralai.client import Mistral
import os
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
res = mistral.beta.prompts.list()
while res is not None:
# Handle items
res = res.next()
Per-Operation Security Schemes
Some operations in this SDK require the security scheme to be specified at the request level. For example:
from mistralai.client import Mistral, models
with Mistral() as mistral:
res = mistral.beta.users.get_identity(security=models.UsersAPIGetIdentitySecurity(
))
# Handle response
print(res)
Resource Management
The Mistral class implements the context manager protocol and registers a finalizer function to close the underlying sync and async HTTPX clients it uses under the hood. This will close HTTP connections, release memory and free up other resources held by the SDK. In short-lived Python programs and notebooks that make a few SDK method calls, resource management may not be a concern. However, in longer-lived programs, it is beneficial to create a single SDK instance via a context manager and reuse it across the application.
from mistralai.client import Mistral
import os
def main():
with Mistral(
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
# Rest of application here...
pass
# Or when using async:
async def amain():
async with Mistral(
api_key=os.getenv("MISTRAL_API_KEY"),
) as mistral:
# Rest of application here...
pass
Debugging
You can setup your SDK to emit debug logs for SDK requests and responses.
You can pass your own logger class directly into your SDK.
from mistralai.client import Mistral
import logging
logging.basicConfig(level=logging.DEBUG)
s = Mistral(debug_logger=logging.getLogger("mistralai.client"))
You can also enable a default debug logger by setting an environment variable MISTRAL_DEBUG to true.
IDE Support
PyCharm
Generally, the SDK will work well with most IDEs out of the box. However, when using PyCharm, you can enjoy much better integration with Pydantic by installing an additional plugin.
The SDK can emit OpenTelemetry traces for the API calls it makes (chat, agents, embeddings, OCR, …), following the
GenAI semantic conventions.
Spans capture the operation, model, token usage, and — unless redacted — the input/output messages and tool calls. Telemetry is opt-in and lives in the mistralai.extra.observability module.
Either set an environment variable before creating the client:
export MISTRAL_SDK_TELEMETRY=dedicated # dedicated | global | false
or configure it in code:
import os
from mistralai.client import Mistral
from mistralai.extra.observability import configure_telemetry
with Mistral(api_key=os.environ["MISTRAL_API_KEY"]) as client:
# Dedicated mode (default): the SDK creates and owns an OTLP exporter that
# ships spans to the Mistral telemetry endpoint. Spans are redacted before
# export.
configure_telemetry(client)
client.chat.complete(
model="mistral-small-latest",
messages=[{"role": "user", "content": "Hello!"}],
)
In dedicated mode, traces go to /telemetry/v1/traces on the client's server_url if one is set, otherwise to https://api.mistral.ai, including for the named eu/us servers. The regional hosts do not serve that route, so use server="eu" rather than server_url="https://api.eu.mistral.ai". Set MISTRAL_OTLP_TRACES_ENDPOINT to send traces to a separate collector.
The exporter authenticates like API calls: an explicit api_key, then the token at MISTRAL_SA_TOKEN_PATH (re-read on every export, so rotation is picked up), then MISTRAL_API_KEY.
Telemetry never fails an API call. If telemetry enabled through MISTRAL_SDK_TELEMETRY cannot be set up, for example because no credential is available or the telemetry extra is not installed, the client logs one warning and keeps working without telemetry. An explicit configure_telemetry(client) call raises instead.
Provider modes
configure_telemetry(client, provider=...) selects where spans go and who owns the export pipeline:
provider
Who owns the exporter
Where spans go
Redaction
"dedicated" (default)
The SDK
Mistral telemetry endpoint
Applied automatically
"global"
Your application
Your global OpenTelemetry provider
Not applied — you need to wrap your own exporter
a TracerProvider
Your application
The provider you pass
Not applied — you need to wrap your own exporter
In global/custom modes your application owns the pipeline, so the redaction argument is ignored (a warning is logged). Wrap your own exporter with RedactingSpanExporter to redact spans there:
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from mistralai.extra.observability import RedactingSpanExporter, configure_telemetry
provider = TracerProvider()
provider.add_span_processor(
BatchSpanProcessor(RedactingSpanExporter(OTLPSpanExporter()))
)
trace.set_tracer_provider(provider)
# SDK spans now flow through your global provider (already redacted above).
configure_telemetry(client, provider="global")
Redaction
In dedicated mode, redaction is on by default. Control it with the redaction argument, which also accepts any of the reusable policies from mistralai.extra.observability:
from mistralai.extra.observability import AttributeRedactionPolicy
configure_telemetry(client) # default policy (regex)
configure_telemetry(client, redaction=AttributeRedactionPolicy()) # very conservative key-oriented policy
configure_telemetry(client, redaction=False) # disabled - no redaction
configure_telemetry( # custom callback to control how attributes are redacted
client,
redaction=lambda key, value: None if "email" in key else value,
)
Policy
Strategy
Trade-off
RegexRedactionPolicy (default, redaction=True)
Content-oriented: keeps keys and structure, redacts matched substrings (secret tokens plus PII — emails, card-like sequences, IPv4).
Redacts most sensitive data while preserving observability value; may miss free-form PII or secrets not in the pattern set.
AttributeRedactionPolicy
Key-oriented: redacts whole values for sensitive keys (explicit set, fragment match, or non-primitive value), then scans kept values for secret token patterns.
Very conservative, but erases most prompt/response content.
CallbackRedactionPolicy (redaction=<callable>)
Your (key, value) -> value | None masker per attribute; return None to drop the attribute.
Full control; you own the logic.
The built-in defaults are exported as constants, so you can extend them instead of replacing them wholesale:
import re
from mistralai.extra.observability import (
DEFAULT_PII_SECRET_PATTERNS,
DEFAULT_SENSITIVE_ATTRIBUTE_KEYS,
AttributeRedactionPolicy,
RegexRedactionPolicy,
)
# Content-oriented: add a custom secret pattern to the default set.
configure_telemetry(
client,
redaction=RegexRedactionPolicy(
patterns=(*DEFAULT_PII_SECRET_PATTERNS, re.compile(r"\bacme-[a-z0-9]{16}\b")),
),
)
# Key-oriented: mask an extra application attribute on top of the defaults.
configure_telemetry(
client,
redaction=AttributeRedactionPolicy(
sensitive_keys=DEFAULT_SENSITIVE_ATTRIBUTE_KEYS | {"app.customer.email"},
),
)
Note: the RedactingSpanExporter primitive is reusable by any OpenTelemetry application, independent of the Mistral client.
Environment variables
Variable
Description
Default
MISTRAL_SDK_TELEMETRY
Auto-enable telemetry: dedicated, global, or false.
unset (disabled)
MISTRAL_OTLP_TRACES_ENDPOINT
Override the OTLP traces endpoint used in dedicated mode.
Client server_url + /telemetry/v1/traces if set, otherwise https://api.mistral.ai/telemetry/v1/traces
MISTRAL_SDK_DEBUG_TRACING
Set to true for verbose tracing logs.
false
MISTRAL_API_KEY
Used as the bearer token for the dedicated-mode exporter when the client has no api_key and no service-account token.
client.audio.sessions.init() opens a live, bidirectional voice session on top of the generated audio.sessions REST methods, which stay available for plain session CRUD. Install the extra; system audio also needs PortAudio (brew install portaudio on macOS).
pip install "mistralai[voice-agents]"
import asyncio
from mistralai.client import Mistral
from mistralai.extra.voice_agents import DefaultAudioInterface, WebSearchTool, function_tool
@function_tool(filler={"text": "Let me check the weather."}, timeout_ms=15000)
async def get_weather(city: str) -> dict[str, object]:
"""Return the current weather for a city."""
return {"city": city, "temp_c": 21, "conditions": "sunny"}
async def main() -> None:
async with Mistral() as client:
session = await client.audio.sessions.init(
stt={"model": "voxtral-mini-transcribe-realtime-3"},
tts={"model": "voxtral-mini-tts-3"},
instructions="Be concise. Use get_weather for weather.",
welcome_message="Hey. Ask me about the weather somewhere.",
tools=[WebSearchTool(), get_weather],
audio_interface=DefaultAudioInterface(),
)
session.on("transcript", lambda event: print(event.role, event.content))
session.on("error", lambda event: print(event.model_dump_json()))
async with session:
await session.wait()
asyncio.run(main())
init() creates the remote session and returns it ready to connect; it does not join the room or open audio. Register callbacks, then call await session.connect() or enter async with session within the session's join window. await session.close() gives the session up without connecting. A missing microphone or a missing extra fails init() before any session is created.
Omit audio_interface to drive 48 kHz mono signed 16-bit PCM yourself: register an audio callback and call session.send_audio() after connect().
Tool declarations, saved agents and versions, event semantics and migration from the standalone mistralai-voice-agents package are covered in the voice agents guide. The runnable examples live in examples/audio/voice_agents.
Development
Contributions
While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation.
We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.
Alternatives
Use instead of mistralai: tools that do the same job, closest first.