Skip to main content

Custom Structured Outputs

Custom Structured Outputs allow you to ensure the model provides an answer in a very specific JSON format by supplying a clear JSON schema. This approach allows the model to consistently deliver responses with the correct typing and keywords.

Here is an example of how to achieve this using the Mistral AI client and Pydantic:

Define the Data Model

First, define the structure of the output using a Pydantic model:

from pydantic import BaseModel

class Book(BaseModel):
name: str
authors: list[str]

Start the completion

Next, use the Mistral AI python client to make a request and ensure the response adheres to the defined structure using response_format set to the corresponding pydantic model:

import os
from mistralai import Mistral

api_key = os.environ["MISTRAL_API_KEY"]
model = "ministral-8b-latest"

client = Mistral(api_key=api_key)

chat_response = client.chat.parse(
model=model,
messages=[
{
"role": "system",
"content": "Extract the books information."
},
{
"role": "user",
"content": "I recently read 'To Kill a Mockingbird' by Harper Lee."
},
],
response_format=Book,
max_tokens=256,
temperature=0
)

In this example, the Book class defines the structure of the output, ensuring that the model's response adheres to the specified format.

There are two types of possible outputs that are easily accessible via our SDK:

  1. The raw JSON output, accessed with chat_response.choices[0].message.content:
{
"authors": ["Harper Lee"],
"name": "To Kill a Mockingbird"
}
  1. The parsed output, converted into a Pydantic object with chat_response.choices[0].message.parsed. In this case, it is a Book instance:
name='To Kill a Mockingbird' authors=['Harper Lee']

FAQ

Q: Which models support custom Structured Outputs?
A: All currently available models except for codestral-mamba are supported.