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.client import MistralClient
from mistralai.models.chat_completion import ChatMessage

api_key = os.environ["MISTRAL_API_KEY"]
model = "mistral-large-latest"

client = MistralClient(api_key=api_key)

messages = [
ChatMessage(role="user", content="What is the best French meal? Return the name and the ingredients in short JSON object.")
]

chat_response = client.chat(
model=model,
response_format={"type": "json_object"},
messages=messages,
)

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"]}