Structured output
Structured output makes the model return data in the JSON format you specify, suitable for scenarios such as data extraction, classification labeling, and form filling.
JSON Mode#
The simplest form of structured output, forcing the model to return valid JSON:
Python
from openai import OpenAI
client = OpenAI(
base_url="https://as.apinoma.com/v1",
api_key="<YOUR APINOMA_API_KEY>"
)
response = client.chat.completions.create(
model="openai/gpt-4o",
messages=[
{"role": "system", "content": "You are a data extraction assistant. Return the result in JSON format."},
{"role": "user", "content": "Extract the person name, company, and title from the following text: John Smith is a senior engineer at Alibaba"}
],
response_format={"type": "json_object"}
)
import json
result = json.loads(response.choices[0].message.content)
print(result)
# {"name": "John Smith", "company": "Alibaba", "title": "Senior Engineer"}TypeScript
When using JSON Mode, the system prompt must contain the keyword “JSON”, otherwise some models may ignore the format requirement.
JSON Schema constraints#
Control the output structure more precisely, ensuring field names and types match expectations:
response = client.chat.completions.create(
model="openai/gpt-4o",
messages=[
{"role": "user", "content": "Analyze the sentiment of this user review: This product is amazing and really easy to use!"}
],
response_format={
"type": "json_schema",
"json_schema": {
"name": "sentiment_analysis",
"schema": {
"type": "object",
"properties": {
"sentiment": {
"type": "string",
"enum": ["positive", "negative", "neutral"],
"description": "Sentiment polarity"
},
"confidence": {
"type": "number",
"description": "Confidence, 0-1"
},
"keywords": {
"type": "array",
"items": {"type": "string"},
"description": "Key sentiment words"
}
},
"required": ["sentiment", "confidence", "keywords"],
"additionalProperties": False
}
}
}
)Output:
Real-world use cases#
Data extraction
# Extract structured data from unstructured text
response = client.chat.completions.create(
model="openai/gpt-4o",
messages=[{
"role": "user",
"content": """Extract the following order information:
Customer Jane Doe placed an order on January 15, 2025 for 3 MacBook Pros,
at a unit price of 18999 CNY, with the shipping address 123 XXX Road, Chaoyang District, Beijing"""
}],
response_format={
"type": "json_schema",
"json_schema": {
"name": "order_info",
"schema": {
"type": "object",
"properties": {
"customer": {"type": "string"},
"date": {"type": "string"},
"items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {"type": "string"},
"quantity": {"type": "integer"},
"unit_price": {"type": "number"}
}
}
},
"address": {"type": "string"}
},
"required": ["customer", "date", "items", "address"]
}
}
}
)Classification labeling
Supported models#
Model
JSON Mode
JSON Schema
`openai/gpt-4o`
✅
✅
`openai/gpt-4o-mini`
✅
✅
`anthropic/claude-sonnet-4.6`
✅
—
`google/gemini-pro-compatible-flash-lite-preview`
✅
✅
Models that do not support JSON Schema can achieve a similar effect by describing the expected JSON format in detail in the system prompt.
Last updated on June 23, 2026