How to beginner · 3 min read

How to use Mistral JSON mode

Quick answer
Use the mistralai Python SDK or the OpenAI-compatible OpenAI client with base_url="https://api.mistral.ai/v1" to call Mistral models in JSON mode by setting the mode="json" parameter. This instructs the model to respond with structured JSON output, which you can parse directly in your Python code.

PREREQUISITES

  • Python 3.8+
  • MISTRAL_API_KEY environment variable set
  • pip install openai>=1.0 or mistralai>=1.0

Setup

Install the openai package for OpenAI-compatible usage or the mistralai package for the official SDK. Set your API key in the environment variable MISTRAL_API_KEY.

bash
pip install openai
# or
pip install mistralai

Step by step

Use the OpenAI-compatible OpenAI client with base_url set to Mistral's API endpoint. Pass mode="json" in the chat.completions.create call to get JSON-formatted responses.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["MISTRAL_API_KEY"], base_url="https://api.mistral.ai/v1")

messages = [
    {"role": "user", "content": "Generate a JSON object with name and age."}
]

response = client.chat.completions.create(
    model="mistral-large-latest",
    messages=messages,
    mode="json"
)

json_text = response.choices[0].message.content
print("JSON response from Mistral:", json_text)
output
JSON response from Mistral: {"name": "Alice", "age": 30}

Common variations

  • Use the official mistralai SDK with client.chat.complete and mode="json".
  • Switch models like mistral-small-latest for faster responses.
  • Combine mode="json" with streaming for partial JSON output (handle carefully).
python
from mistralai import Mistral
import os

client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])

response = client.chat.complete(
    model="mistral-large-latest",
    messages=[{"role": "user", "content": "Provide a JSON with city and population."}],
    mode="json"
)

print("JSON response:", response.choices[0].message.content)
output
JSON response: {"city": "New York", "population": 8419000}

Troubleshooting

  • If the response is not valid JSON, ensure mode="json" is set and the prompt clearly requests JSON output.
  • Check your API key and base_url for typos.
  • For partial or malformed JSON, try simpler prompts or post-process with a JSON parser that can handle errors.

Key Takeaways

  • Set mode="json" to get structured JSON responses from Mistral models.
  • Use the OpenAI-compatible OpenAI client with base_url="https://api.mistral.ai/v1" or the official mistralai SDK.
  • Always parse the returned JSON string safely in your application to handle any formatting issues.
Verified 2026-04 · mistral-large-latest, mistral-small-latest
Verify ↗