Skip to content

Mistral

Install

To use MistralModel, you need to either install pydantic-ai, or install pydantic-ai-slim with the mistral optional group:

Terminal
pip install "pydantic-ai-slim[mistral]"

Configuration

To use Mistral through their API, go to console.mistral.ai/api-keys/ and follow your nose until you find the place to generate an API key.

LatestMistralModelNames lists the latest Mistral models for autocomplete. Any other model id the API accepts works too.

Environment variable

Once you have the API key, you can set it as an environment variable:

Terminal
export MISTRAL_API_KEY='your-api-key'

You can then use MistralModel by name:

from pydantic_ai import Agent

agent = Agent('mistral:mistral-large-latest')
...

Or initialise the model directly with just the model name:

from pydantic_ai import Agent
from pydantic_ai.models.mistral import MistralModel

model = MistralModel('mistral-small-latest')
agent = Agent(model)
...

provider argument

You can provide a custom Provider via the provider argument:

from pydantic_ai import Agent
from pydantic_ai.models.mistral import MistralModel
from pydantic_ai.providers.mistral import MistralProvider

model = MistralModel(
    'mistral-large-latest', provider=MistralProvider(api_key='your-api-key', base_url='https://<mistral-provider-endpoint>')
)
agent = Agent(model)
...

You can also customize the provider with a custom httpx2.AsyncClient:

from httpx2 import AsyncClient

from pydantic_ai import Agent
from pydantic_ai.models.mistral import MistralModel
from pydantic_ai.providers.mistral import MistralProvider

custom_http_client = AsyncClient(timeout=30)
model = MistralModel(
    'mistral-large-latest',
    provider=MistralProvider(api_key='your-api-key', http_client=custom_http_client),
)
agent = Agent(model)
...

The Mistral SDK sends one timeout for every phase of each request: the client’s read timeout, or the timeout model setting when it is set. A client made with create_async_httpx2_client(), like the one the provider creates when you don’t pass one, keeps its own connect and pool timeouts when they are shorter. A client that keeps httpx’s default timeout, or has no read timeout, gets the SDK’s own default instead.

The extra_headers model setting is sent with each request. The user-agent goes out as mistral-client-python/<version> pydantic-ai/<version>. A user-agent you send in extra_headers, in any spelling, takes the place of the pydantic-ai/<version> part and goes out behind the SDK’s label, since the Mistral SDK requires a user-agent to start with mistral-client-python/.

The Mistral provider also accepts a legacy httpx.AsyncClient during Pydantic AI v2, but emits a deprecation warning. Use httpx2.AsyncClient for new code; legacy HTTPX client support will be removed in Pydantic AI v3.

Tool call IDs

Mistral’s tool-calling chat template uses tool call IDs of exactly 9 ASCII letters and digits, and models that validate them reject any other ID with a 400 error. That includes IDs from another provider, like OpenAI’s call_... or Anthropic’s toolu_..., and the pyd_ai_... IDs Pydantic AI generates. A message history started on another model, or a FallbackModel that switches to Mistral, can contain such IDs.

To keep these histories working, every Mistral model profile sets tool_call_id_length to 9. Before each request, an ID that doesn’t fit is replaced with a stable hash of it that does, and an ID that already fits is sent unchanged. Other providers that serve Mistral models, like Azure AI Foundry, OpenRouter or vLLM, use the same profile. The stored message history keeps the original IDs.