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.
- python
- javascript
- curl
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)
import MistralClient from '@mistralai/mistralai';
const apiKey = process.env.MISTRAL_API_KEY;
const client = new MistralClient(apiKey);
const chatResponse = await client.chat({
model: 'mistral-large-latest',
response_format: {'type': 'json_object'},
messages: [{role: 'user', content: 'What is the best French cheese? Return the product and produce location in JSON format'}],
});
console.log('Chat:', chatResponse.choices[0].message.content);
curl --location "https://api.mistral.ai/v1/chat/completions" \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header "Authorization: Bearer $MISTRAL_API_KEY" \
--data '{
"model": "mistral-large-latest",
"messages": [
{
"role": "user",
"content": "What is the best French cheese? Return the product and produce location in JSON format"
}
],
"response_format": {"type": "json_object"}
}'