[Capabilities]

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.

Usage

Usage

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

Define the Data Model

Define the Data Model

First, define the structure of the output using a Pydantic, Zod or a JSON Schema:

from pydantic import BaseModel

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

Start the completion

Next, make a request and ensure the response adheres to the defined structure using response_format set to the corresponding 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.

Cat head

¡Meow! Click one of the tabs above to learn more.

note

To better guide the model, the following is being always prepended to the System Prompt when using this method:

Your output should be an instance of a JSON object following this schema: {{ json_schema }}

However, it is recommended to add more explanations and iterate on your system prompt to better clarify the expected schema and behavior.

FAQ

FAQ