Skip to main content

JSON mode

Users have the option to set response_format to {"type": "json_object"} to enable JSON mode. Currently, JSON mode is available for all of our models through API.

warning

It's important to explicitly ask the model to generate JSON output in your message.

To prevent infinite generations, users are encouraged to ask the model for short JSON objects.

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",
}
)

print(chat_response.choices[0].message.content)


Example output:

{"name": "Coq au Vin", "ingredients": ["chicken", "red wine", "bacon", "mushrooms", "onions", "garlic", "chicken broth", "thyme", "bay leaf", "flour", "butter", "olive oil", "salt", "pepper"]}