Mistral as a drop-in for OpenAI
Mistral's /v1/chat/completions endpoint accepts the same JSON schema as OpenAI's, so most SDKs and frameworks work by swapping the base URL and API key. Here's the mapping.
1. Base URL & auth
OPENAI_API_BASE=https://api.mistral.ai/v1
OPENAI_API_KEY=<your-mistral-key> # get one at console.mistral.ai
Authorization: Bearer <your-mistral-key>2. OpenAI Python SDK
from openai import OpenAI
client = OpenAI(
base_url="https://api.mistral.ai/v1",
api_key="<your-mistral-key>",
)
resp = client.chat.completions.create(
model="mistral-large-latest",
messages=[{"role": "user", "content": "Hello!"}],
)
print(resp.choices[0].message.content)3. curl
curl https://api.mistral.ai/v1/chat/completions \
-H "Authorization: Bearer $MISTRAL_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "mistral-large-latest",
"messages": [{"role": "user", "content": "Say hi"}],
"stream": false
}'4. Model-name mapping
| OpenAI | Closest Mistral |
|---|---|
| gpt-4o | mistral-large-latest |
| gpt-4o-mini | mistral-small-latest |
| gpt-3.5-turbo | open-mistral-7b |
| text-embedding-3-small | mistral-embed |
| code assistant | codestral-latest |
5. Supported endpoints
/v1/chat/completions— full parity, including streaming and tool calls./v1/embeddings— modelmistral-embed./v1/models— list available models on your key.- Not mirrored: Assistants, vision inputs on OpenAI schema, audio, image gen. Use Mistral's native SDK for agents and file inputs.
6. Common gotchas
- Response format: use
{ "type": "json_object" }— same as OpenAI. - System prompts: supported, but Mistral models weight them slightly differently — retest your prompts.
- Tool choice:
tool_choice: "auto" | "any" | "none"—"any"replaces OpenAI's"required". - Rate limits: per-key and per-model, in requests/min and tokens/min — see console.mistral.ai → Usage.
FAQ
Does Mistral support the OpenAI SDK?
Yes. Point the OpenAI SDK's base_url at https://api.mistral.ai/v1 and pass your Mistral API key. Chat completions and embeddings work out of the box.
Which OpenAI endpoints map cleanly?
/v1/chat/completions, /v1/embeddings, /v1/models. Streaming (SSE) is supported. /v1/completions (legacy) is not — use chat completions.
What differs vs OpenAI?
Model names differ (mistral-large-latest, mistral-small-latest, codestral-latest, mistral-embed). Tool-calling schema is compatible. Vision, audio and Assistants API are not mirrored.
Can I use LangChain or LiteLLM?
Yes — both ship first-class Mistral providers, and either OpenAI-compat mode also works. LiteLLM auto-routes when you prefix the model as mistral/mistral-large-latest.