JSON Mode
Set response_format to json_object so the model always returns a valid JSON object, whatever the prompt.
Users have the option to set response_format to {"type": "json_object"} to enable JSON mode.
This mode ensures that the model's response is formatted as a valid JSON object regardless of the content of the prompt, however we still recommend to explicitly ask the model to return a JSON object and the format.
Usage
How to generate JSON consistently
Below is an example of how to use JSON mode with the Mistral API.
import os
from mistralai import Mistral
api_key = os.environ["MISTRAL_API_KEY"]
model = "mistral-large-latest"
client = Mistral(api_key=api_key)
messages = [
{
"role": "user",
"content": "What is the best French meal? Return the name and the ingredients in short JSON object.",
}
]
chat_response = client.chat.complete(
model = model,
messages = messages,
response_format = {
"type": "json_object",
}
)
Output
Output
{
"id": "25e3f46176674414a76a173bb9294529",
"object": "chat.completion",
"model": "mistral-large-2512",
"usage": {
"prompt_tokens": 21,
"completion_tokens": 107,
"total_tokens": 128
},
"created": 1764347485,
"choices": [
{
"index": 0,
"message": {
"content": "{\n \"meal\": \"Boeuf Bourguignon\",\n \"ingredients\": [\n \"beef\",\n \"red wine (Burgundy)\",\n \"onions\",\n \"carrots\",\n \"garlic\",\n \"mushrooms\",\n \"bacon\",\n \"beef stock\",\n \"tomato paste\",\n \"thyme\",\n \"bay leaf\",\n \"butter\",\n \"flour\",\n \"salt\",\n \"pepper\"\n ]\n}",
"tool_calls": null,
"prefix": false,
"role": "assistant"
},
"finish_reason": "stop"
}
]
}
The output will always be enforced to be valid JSON, and the content field will be a stringified JSON object. In this case:
{
"meal": "Boeuf Bourguignon",
"ingredients": [
"beef",
"red wine (Burgundy)",
"onions",
"carrots",
"garlic",
"mushrooms",
"bacon",
"beef stock",
"tomato paste",
"thyme",
"bay leaf",
"butter",
"flour",
"salt",
"pepper"
]
}
FAQ
All currently available models except for codestral-mamba are supported.