If you are upgrading from v1 to v2, check the migration guide for details on breaking changes and how to update your code.
Before you begin, you will need a Mistral AI API key.
# 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
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:
Authorization header passed per request via http_headersMistral(api_key=...), which also accepts a callableMISTRAL_SA_TOKEN_PATHMISTRAL_API_KEYAn unreadable or empty token file raises ServiceAccountTokenError.
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.
[!NOTE] Python version upgrade policy
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 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 is the default package installer for Python, enabling easy installation and management of packages from PyPI via the command line.
pip install mistralai
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
uvYou 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:
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "mistralai",
# ]
# ///
from mistralai.client import Mistral
sdk = Mistral(
# SDK arguments
)
# Rest of script here...
Once that is saved to a file, you can run it with uv run script.py where
script.py can be replaced with the actual file name.
When using the agents related feature it is required to add the agents extra dependencies. This can be added when
installing the package:
pip install "mistralai[agents]"
Note: These features require Python 3.10+ (the SDK minimum).
Additional mistralai-* packages (e.g. mistralai-workflows) can be installed separately and are available under the mistralai namespace:
pip install mistralai-workflows
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())
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")
# 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")
# Handle response
print(res)
asyncio.run(main())
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())
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())
You can run the examples in the examples/ directory using uv run.
Prerequisites
Before you begin, ensure you have AZURE_ENDPOINT and an AZURE_API_KEY. To obtain these, you will need to deploy Mistral on Azure AI.
See instructions for deploying Mistral on Azure AI here.
Step 1: Install
pip install mistralai
Step 2: Example Usage
Here's a basic example to get you started. You can also run the example in the examples directory.
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)
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]"
Step 2: Example Usage
Here's a basic example to get you started. You can also run the example in the examples directory.
The SDK automatically:
google.auth.default()project_id and regionimport os
from mistralai.gcp.client import MistralGCP
# The SDK auto-detects credentials and builds the Vertex AI URL
client = MistralGCP(
project_id=os.environ.get("GCP_PROJECT_ID"), # Optional: auto-detected from credentials
region="us-central1", # Default: europe-west4
)
res = client.chat.complete(
model="mistral-small-2503",
messages=[
{
"role": "user",
"content": "Hello there!",
}
],
)
print(res.choices[0].message.content)
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)
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()
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)
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()
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. |
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]]
Primary error:
MistralError: The base class for HTTP error responses.Network errors:
httpx.RequestError: Base class for request errors.
httpx.ConnectError: HTTP client was unable to make a request to a server.httpx.TimeoutException: HTTP request timed out.Inherit from MistralError:
HTTPValidationError: Validation Error. Status code 422. Applicable to 165 of 278 methods.*ObservabilityError: Bad Request - Invalid request parameters or data. Applicable to 69 of 278 methods.*ResponseValidationError: Type mismatch between the response data and the expected Pydantic model. Provides access to the Pydantic validation error via the cause attribute.* Check the method documentation to see if the error is applicable.
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 |
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()
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()
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:
from mistralai.client import Mistral
from mistralai.client.httpclient import AsyncHttpClient
import httpx2 as httpx
class CustomClient(AsyncHttpClient):
client: AsyncHttpClient
def __init__(self, client: AsyncHttpClient):
self.client = client
async def send(
self,
request: httpx.Request,
*,
stream: bool = False,
auth: Union[
httpx._types.AuthTypes, httpx._client.UseClientDefault, None
] = httpx.USE_CLIENT_DEFAULT,
follow_redirects: Union[
bool, httpx._client.UseClientDefault
] = httpx.USE_CLIENT_DEFAULT,
) -> httpx.Response:
request.headers["Client-Level-Header"] = "added by client"
return await self.client.send(
request, stream=stream, auth=auth, follow_redirects=follow_redirects
)
def build_request(
self,
method: str,
url: httpx._types.URLTypes,
*,
content: Optional[httpx._types.RequestContent] = None,
data: Optional[httpx._types.RequestData] = None,
files: Optional[httpx._types.RequestFiles] = None,
json: Optional[Any] = None,
params: Optional[httpx._types.QueryParamTypes] = None,
headers: Optional[httpx._types.HeaderTypes] = None,
cookies: Optional[httpx._types.CookieTypes] = None,
timeout: Union[
httpx._types.TimeoutTypes, httpx._client.UseClientDefault
] = httpx.USE_CLIENT_DEFAULT,
extensions: Optional[httpx._types.RequestExtensions] = None,
) -> httpx.Request:
return self.client.build_request(
method,
url,
content=content,
data=data,
files=files,
json=json,
params=params,
headers=headers,
cookies=cookies,
timeout=timeout,
extensions=extensions,
)
s = Mistral(async_client=CustomClient(httpx.AsyncClient()))
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.
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()
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)
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...
# Or when using async:
async def amain():
async with Mistral(
api_key=os.getenv("MISTRAL_API_KEY", ""),
) as mistral:
# Rest of application here...
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.
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.
Install the telemetry extra:
pip install "mistralai[telemetry]"
# or: uv add "mistralai[telemetry]"
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!"}],
)
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")
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.
| 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. | 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. | — |
Runnable examples live in examples/mistral/observability.
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.
19 followers · starred May 2024
61 followers · starred Oct 2025
70 followers · starred Feb 2025
61 followers · starred Mar 2025
If you are upgrading from v1 to v2, check the migration guide for details on breaking changes and how to update your code.
Before you begin, you will need a Mistral AI API key.
# 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
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:
Authorization header passed per request via http_headersMistral(api_key=...), which also accepts a callableMISTRAL_SA_TOKEN_PATHMISTRAL_API_KEYAn unreadable or empty token file raises ServiceAccountTokenError.
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.
[!NOTE] Python version upgrade policy
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 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 is the default package installer for Python, enabling easy installation and management of packages from PyPI via the command line.
pip install mistralai
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
uvYou 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:
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "mistralai",
# ]
# ///
from mistralai.client import Mistral
sdk = Mistral(
# SDK arguments
)
# Rest of script here...
Once that is saved to a file, you can run it with uv run script.py where
script.py can be replaced with the actual file name.
When using the agents related feature it is required to add the agents extra dependencies. This can be added when
installing the package:
pip install "mistralai[agents]"
Note: These features require Python 3.10+ (the SDK minimum).
Additional mistralai-* packages (e.g. mistralai-workflows) can be installed separately and are available under the mistralai namespace:
pip install mistralai-workflows
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())
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")
# 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")
# Handle response
print(res)
asyncio.run(main())
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())
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())
You can run the examples in the examples/ directory using uv run.
Prerequisites
Before you begin, ensure you have AZURE_ENDPOINT and an AZURE_API_KEY. To obtain these, you will need to deploy Mistral on Azure AI.
See instructions for deploying Mistral on Azure AI here.
Step 1: Install
pip install mistralai
Step 2: Example Usage
Here's a basic example to get you started. You can also run the example in the examples directory.
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)
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]"
Step 2: Example Usage
Here's a basic example to get you started. You can also run the example in the examples directory.
The SDK automatically:
google.auth.default()project_id and regionimport os
from mistralai.gcp.client import MistralGCP
# The SDK auto-detects credentials and builds the Vertex AI URL
client = MistralGCP(
project_id=os.environ.get("GCP_PROJECT_ID"), # Optional: auto-detected from credentials
region="us-central1", # Default: europe-west4
)
res = client.chat.complete(
model="mistral-small-2503",
messages=[
{
"role": "user",
"content": "Hello there!",
}
],
)
print(res.choices[0].message.content)
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)
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()
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)
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()
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. |
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]]
Primary error:
MistralError: The base class for HTTP error responses.Network errors:
httpx.RequestError: Base class for request errors.
httpx.ConnectError: HTTP client was unable to make a request to a server.httpx.TimeoutException: HTTP request timed out.Inherit from MistralError:
HTTPValidationError: Validation Error. Status code 422. Applicable to 165 of 278 methods.*ObservabilityError: Bad Request - Invalid request parameters or data. Applicable to 69 of 278 methods.*ResponseValidationError: Type mismatch between the response data and the expected Pydantic model. Provides access to the Pydantic validation error via the cause attribute.* Check the method documentation to see if the error is applicable.
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 |
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()
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()
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:
from mistralai.client import Mistral
from mistralai.client.httpclient import AsyncHttpClient
import httpx2 as httpx
class CustomClient(AsyncHttpClient):
client: AsyncHttpClient
def __init__(self, client: AsyncHttpClient):
self.client = client
async def send(
self,
request: httpx.Request,
*,
stream: bool = False,
auth: Union[
httpx._types.AuthTypes, httpx._client.UseClientDefault, None
] = httpx.USE_CLIENT_DEFAULT,
follow_redirects: Union[
bool, httpx._client.UseClientDefault
] = httpx.USE_CLIENT_DEFAULT,
) -> httpx.Response:
request.headers["Client-Level-Header"] = "added by client"
return await self.client.send(
request, stream=stream, auth=auth, follow_redirects=follow_redirects
)
def build_request(
self,
method: str,
url: httpx._types.URLTypes,
*,
content: Optional[httpx._types.RequestContent] = None,
data: Optional[httpx._types.RequestData] = None,
files: Optional[httpx._types.RequestFiles] = None,
json: Optional[Any] = None,
params: Optional[httpx._types.QueryParamTypes] = None,
headers: Optional[httpx._types.HeaderTypes] = None,
cookies: Optional[httpx._types.CookieTypes] = None,
timeout: Union[
httpx._types.TimeoutTypes, httpx._client.UseClientDefault
] = httpx.USE_CLIENT_DEFAULT,
extensions: Optional[httpx._types.RequestExtensions] = None,
) -> httpx.Request:
return self.client.build_request(
method,
url,
content=content,
data=data,
files=files,
json=json,
params=params,
headers=headers,
cookies=cookies,
timeout=timeout,
extensions=extensions,
)
s = Mistral(async_client=CustomClient(httpx.AsyncClient()))
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.
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()
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)
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...
# Or when using async:
async def amain():
async with Mistral(
api_key=os.getenv("MISTRAL_API_KEY", ""),
) as mistral:
# Rest of application here...
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.
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.
Install the telemetry extra:
pip install "mistralai[telemetry]"
# or: uv add "mistralai[telemetry]"
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!"}],
)
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")
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.
| 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. | 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. | — |
Runnable examples live in examples/mistral/observability.
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.
19 followers · starred May 2024
61 followers · starred Oct 2025
70 followers · starred Feb 2025
61 followers · starred Mar 2025