1 min read 178 words Updated Sep 24, 2026 Created Sep 24, 2026
#AI#APIs#LLM#mistral

SDK in Python

It is recommended to create a central instance of the Mistral SDK in Python with a context manager and share it across the application.
See: https://book.pythontips.com/en/latest/context_managers.html

Structured outputs

With JSON

Mistral supports structured outputs for almost all models:

from mistralai import Mistral
from dotenv import load_dotenv
import os

load_dotenv()

MISTRALAPIKEY = os.getenv("MISTRALAPIKEY")

mistralClient = Mistral(api_key=MISTRALAPIKEY)

model = "ministral-3b-latest"

prompt = "Give me the five biggest European cities by population. Respond in JSON format with the keys 'city' and 'population'."

response = mistralClient.chat.complete(
    model=model,
    messages=[
        {"role": "user", "content": prompt}
    ],
    response_format={
        "type": "json_object"
    }
)

# We are returned JSON 
resExtracted = response.choices[0].message.content

print("Extracted JSON Response:")
print(resExtracted)

You can parse it to an Python object:

import json

out = response.choices[0].message.content

out_json = json.loads(out)

see: https://docs.mistral.ai/capabilities/structured_output/json_mode

Tokens & Pricing

The example above, showcasing the use of structured output for the european cities used:

  • 27 prompt tokens
  • 123 completion_tokens
  • A total of 150 tokens

The model used, Ministral 3B 24.10, costs 4 cents for 1 million tokens.