Guarantee valid, structured JSON output from AI models. Perfect for data extraction, automation, and API integrations.
Here's the extracted data: Name: John Smith Email: john@example.com Company: Acme Corp Let me know if you need anything else!
{
"name": "John Smith",
"email": "john@example.com",
"company": "Acme Corp"
}
Define exactly what shape you want the output to be:
from pydantic import BaseModel # Define your schema with Pydantic class ContactInfo(BaseModel): name: str email: str company: str | None # Request structured output response = client.beta.chat.completions.parse( model="mythic-4", messages=[ {"role": "user", "content": "Extract: John Smith from Acme Corp, john@example.com"} ], response_format=ContactInfo ) # Access parsed data directly contact = response.choices[0].message.parsed print(contact.name) # "John Smith" print(contact.email) # "john@example.com" print(contact.company) # "Acme Corp"
With Pydantic models, you get full IDE autocomplete and type checking. The response is automatically parsed into your model class.
For simpler use cases where you just need valid JSON:
response = client.chat.completions.create(
model="mythic-4",
messages=[
{"role": "system", "content": "Output valid JSON"},
{"role": "user", "content": "List 3 programming languages with their use cases"}
],
response_format={"type": "json_object"}
)
# Parse the JSON response
import json
data = json.loads(response.choices[0].message.content)
print(data)
When using JSON mode, you must instruct the model to produce JSON in your prompt (system or user message). Otherwise, the model may generate endless whitespace.
| Feature | Structured Outputs | JSON Mode |
|---|---|---|
| Valid JSON | โ Always | โ Always |
| Schema validation | โ Guaranteed | Not guaranteed |
| Required keys | โ Enforced | Not enforced |
| Type safety | โ With Pydantic | Manual parsing |
| Setup | Define schema | Just enable |
| Best for | Production APIs | Quick prototypes |
Parse names, emails, and details from text
Categorize content with structured labels
Transform unstructured to structured data
Generate consistent API payloads
Create structured form data from descriptions
Generate mock data matching your schema
You can also use JSON Schema directly:
response = client.chat.completions.create(
model="mythic-4",
messages=[
{"role": "user", "content": "Generate a product with name, price, and category"}
],
response_format={
"type": "json_schema",
"json_schema": {
"name": "product",
"strict": True,
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"price": {"type": "number"},
"category": {"type": "string"}
},
"required": ["name", "price", "category"],
"additionalProperties": False
}
}
}
)