Structured Outputs
Return structured data from your models.
Overview
Using Structured Outputs
json_scheme
import requests
import json
response = requests.post(
url="https://llm.onerouter.pro/v1/chat/completions",
headers={
"Authorization": "Bearer <API KEY>",
"Content-Type": "application/json"
},
data=json.dumps({
"model": "{{MODEL}}",
"messages": [
{"role": "user", "content": "What's the weather like in London?"}
],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "weather",
"strict": True,
"schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City or location name"
},
"temperature": {
"type": "number",
"description": "Temperature in Celsius"
},
"conditions": {
"type": "string",
"description": "Weather conditions description"
}
},
"required": ["location", "temperature", "conditions"],
"additionalProperties": False
}
}
}
})
)
print(response.json()["choices"][0]["message"]["content"])CalendarEvent
Model Support
Best Practices
Streaming with Structured Outputs
Error Handling
Last updated