import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
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.
1import os
2from mistralai import Mistral
3
4api_key = os.environ["MISTRAL_API_KEY"]
5model = "mistral-large-latest"
6
7client = Mistral(api_key=api_key)
8messages = [
9 {
10 "role": "user",
11 "content": "What is the best French meal? Return the name and the ingredients in short JSON object.",
12 }
13]
14chat_response = client.chat.complete(
15 model = model,
16 messages = messages,
17 response_format = {
18 "type": "json_object",
19 }
20)
21
22print(chat_response.choices[0].message.content)
23
24
Example output:
1{"name": "Coq au Vin", "ingredients": ["chicken", "red wine", "bacon", "mushrooms", "onions", "garlic", "chicken broth", "thyme", "bay leaf", "flour", "butter", "olive oil", "salt", "pepper"]}
const apiKey = process.env.MISTRAL_API_KEY;
const mistral = new Mistral({apiKey: apiKey});
const chatResponse = await mistral.chat.complete({ model: "mistral-large-latest", messages: [{role: 'user', content: 'What is the best French meal? Return the name and the ingredients in JSON format.'}], responseFormat: {type: 'json_object'}, } );
console.log('JSON:', chatResponse.choices[0].message.content)
1 </TabItem>
2 <TabItem value="curl" label="curl">
3```bash
4curl --location "https://api.mistral.ai/v1/chat/completions" \
5 --header 'Content-Type: application/json' \
6 --header 'Accept: application/json' \
7 --header "Authorization: Bearer $MISTRAL_API_KEY" \
8 --data '{
9 "model": "mistral-large-latest",
10 "messages": [
11 {
12 "role": "user",
13 "content": "What is the best French cheese? Return the product and produce location in JSON format"
14 }
15 ],
16 "response_format": {"type": "json_object"}
17 }'