[Capabilities]

JSON Mode

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

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

The output will always be enforced to be valid JSON, and the content field will be a stringified JSON object. In this case:

{
  "name": "Boeuf Bourguignon",
  "ingredients": [
    "beef",
    "red wine",
    "onions",
    "carrots",
    "garlic",
    "mushrooms",
    "bacon",
    "beef broth",
    "tomato paste",
    "thyme",
    "bay leaves",
    "butter",
    "flour"
  ]
}
FAQ

FAQ