Skip to main content

JSON mode

Users have the option to set response_format to {"type": "json_object"} to enable JSON mode. It's important to explicitly ask the model to generate JSON output in your message. Currently, JSON mode is available for all of our models through API.

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 cheese? Return the product and produce location in JSON format")
]

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

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