RepoMistral AIMistral AIpublished Dec 7, 2023seen 6d

mistralai/client-python

Python

Open original ↗

Captured source

source ↗
published Dec 7, 2023seen 6dcaptured 8hhttp 200method plain

mistralai/client-python

Description: Python client library for Mistral AI platform

Language: Python

License: Apache-2.0

Stars: 745

Forks: 194

Open issues: 18

Created: 2023-12-07T10:09:51Z

Pushed: 2026-06-10T22:13:43Z

Default branch: main

Fork: no

Archived: no

README:

Mistral Python Client

Migrating from v1

If you are upgrading from v1 to v2, check the migration guide for details on breaking changes and how to update your code.

API Key Setup

Before you begin, you will need a Mistral AI API key.

1. Get your own Mistral API Key: 2. 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

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.

Table of Contents

  • [Mistral Python Client](#mistral-python-client)
  • [Migrating from v1](#migrating-from-v1)
  • [API Key Setup](#api-key-setup)
  • [SDK Installation](#sdk-installation)
  • [SDK Example Usage](#sdk-example-usage)
  • [Providers' SDKs Example Usage](#providers-sdks-example-usage)
  • [Available Resources and Operations](#available-resources-and-operations)
  • [Server-sent event streaming](#server-sent-event-streaming)
  • [Pagination](#pagination)
  • [File uploads](#file-uploads)
  • [Retries](#retries)
  • [Error Handling](#error-handling)
  • [Server Selection](#server-selection)
  • [Custom HTTP Client](#custom-http-client)
  • [Authentication](#authentication)
  • [Resource Management](#resource-management)
  • [Debugging](#debugging)
  • [IDE Support](#ide-support)
  • [Development](#development)
  • [Contributions](#contributions)

SDK Installation

> [!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

*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:

#!/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.

Agents extra dependencies

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 packages

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")

# 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())

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.",…

Excerpt shown — open the source for the full document.