Skip to main content

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.

Quick answer: Mistral provides an OpenAI-compatible API at the base URL https://api.mistral.ai/v1. You can use standard OpenAI SDKs by setting baseURL to Mistral's endpoint and providing your Mistral API key as the bearer token, utilizing standard chat completions and embedding endpoints seamlessly.

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

OpenAIClosest Mistral
gpt-4omistral-large-latest
gpt-4o-minimistral-small-latest
gpt-3.5-turboopen-mistral-7b
text-embedding-3-smallmistral-embed
code assistantcodestral-latest

5. Supported endpoints

  • /v1/chat/completions — full parity, including streaming and tool calls.
  • /v1/embeddings — model mistral-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

What is the Mistral OpenAI-compatible base URL?
The base URL for Mistral's OpenAI-compatible endpoints is https://api.mistral.ai/v1. Configure this URL in standard OpenAI client libraries alongside your Mistral API key to switch providers seamlessly.
Which OpenAI endpoints does Mistral support?
Mistral supports /v1/chat/completions, /v1/embeddings, and /v1/models. It accepts standard OpenAI chat parameters including streaming, system messages, temperature, and JSON mode tool definitions.
How do you swap OpenAI SDK client to Mistral?
In Python or Node.js, instantiate the OpenAI client with baseURL set to https://api.mistral.ai/v1 and apiKey set to your Mistral token. Then specify Mistral models like mistral-large-latest.
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.
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.
How do you use Mistral with the OpenAI Python SDK?
Initialize the OpenAI client with base_url='https://api.mistral.ai/v1' and api_key=os.environ['MISTRAL_API_KEY']. You can then call client.chat.completions.create() using Mistral model identifiers like mistral-large-latest or mistral-small-latest.
Which OpenAI SDK features work with Mistral?
Mistral supports chat completions, streaming, function calling (tool use), JSON mode, and embeddings through the standard OpenAI API structure. Assistants API and fine-tuning endpoints use Mistral-specific APIs instead.
Can you use the OpenAI SDK directly with Mistral AI?
Yes. By configuring baseURL to https://api.mistral.ai/v1 and apiKey to your Mistral token in the official OpenAI SDK, chat completions work without code rewrites.
What Mistral endpoints are OpenAI compatible?
Mistral supports /v1/chat/completions, /v1/embeddings, and /v1/models adhering to standard OpenAI request and response schema formats.