Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Mistralai Client python Mistral Init

From Leeroopedia
Knowledge Sources
Domains SDK_Setup, API_Client
Last Updated 2026-02-15 14:00 GMT

Overview

Concrete tool for creating an authenticated Mistral API client instance provided by the mistralai SDK.

Description

The Mistral class is the main entry point for the Mistral AI Python SDK. It initializes an HTTP client with bearer token authentication and exposes lazy-loaded sub-SDKs for different API resources: .chat, .embeddings, .files, .fine_tuning, .ocr, and .models. The client supports both synchronous and asynchronous operations. Authentication can be provided as a string API key or a callable that returns a key (for dynamic credential refresh).

Usage

Import and instantiate this class when you need to interact with any Mistral AI API endpoint via the standard (non-Azure, non-GCP) API. The API key defaults to the MISTRAL_API_KEY environment variable if not explicitly provided.

Code Reference

Source Location

  • Repository: client-python
  • File: src/mistralai/client/sdk.py
  • Lines: L70-162

Signature

class Mistral:
    def __init__(
        self,
        api_key: Optional[str] = None,
        server_url: Optional[str] = None,
        timeout_ms: Optional[int] = None,
        retry_config: OptionalNullable[RetryConfig] = UNSET,
        http_client: Optional[HttpClient] = None,
        async_http_client: Optional[AsyncHttpClient] = None,
        debug_logger: Optional[Logger] = None,
    ):
        """
        Args:
            api_key: API key for authentication (or env MISTRAL_API_KEY).
            server_url: Override the default server URL.
            timeout_ms: Request timeout in milliseconds.
            retry_config: Configuration for automatic request retries.
            http_client: Custom sync HTTP client instance.
            async_http_client: Custom async HTTP client instance.
            debug_logger: Logger for debug output.
        """

Import

from mistralai import Mistral

I/O Contract

Inputs

Name Type Required Description
api_key Optional[str] No API key string; falls back to MISTRAL_API_KEY env var
server_url Optional[str] No Override base URL (default: https://api.mistral.ai)
timeout_ms Optional[int] No Request timeout in milliseconds
retry_config OptionalNullable[RetryConfig] No Retry settings with backoff strategy

Outputs

Name Type Description
Mistral instance Mistral Configured client with lazy-loaded .chat, .embeddings, .files, .fine_tuning, .ocr, .models sub-SDKs

Usage Examples

Basic Initialization

import os
from mistralai import Mistral

# Using environment variable (recommended)
client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])

# Or with explicit key
client = Mistral(api_key="your-api-key")

With Custom Configuration

from mistralai import Mistral
from mistralai.utils import RetryConfig, BackoffStrategy

client = Mistral(
    api_key="your-api-key",
    server_url="https://custom-endpoint.example.com",
    timeout_ms=30000,
    retry_config=RetryConfig(
        strategy="backoff",
        backoff=BackoffStrategy(
            initial_interval=500,
            max_interval=60000,
            exponent=1.5,
            max_elapsed_time=300000,
        ),
        retry_connection_errors=True,
    ),
)

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment