Mistral AI Python Client

2.3.0 · active · verified Sun Apr 05

The `mistralai` library is the official Python Client SDK for interacting with the Mistral AI API, providing programmatic access to Mistral's large language models and other AI services. It is currently at version 2.3.0 and actively maintained with regular updates and feature enhancements.

Warnings

Install

Imports

Quickstart

This quickstart initializes the Mistral client and performs a simple chat completion request. It assumes the `MISTRAL_API_KEY` environment variable is set for authentication. The response content is then printed.

import os
from mistralai.client import Mistral

# Ensure MISTRAL_API_KEY environment variable is set
api_key = os.environ.get('MISTRAL_API_KEY', '')

if not api_key:
    print("Error: MISTRAL_API_KEY environment variable not set.")
    print("Please set it using: export MISTRAL_API_KEY='your_api_key_here'")
else:
    try:
        client = Mistral(api_key=api_key)
        chat_response = client.chat.complete(
            model="mistral-small-latest",
            messages=[
                {"role": "user", "content": "What is the capital of France?"}
            ]
        )
        print(chat_response.choices[0].message.content)
    except Exception as e:
        print(f"An error occurred: {e}")

view raw JSON →