Structured outputs
Get JSON from the model with response_format, with or without a JSON Schema.
JSON that matches a schema
response_format of type json_schema asks the model for JSON that follows your schema. The router sends such a request only to routes that support structured output (the Structured output capability in the catalog); a model without one is a 400 "no route".
{
"model": "openai/gpt-4o-mini",
"messages": [{ "role": "user", "content": "Extract the city and the date: meet me in Kazan on 3 May." }],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "meeting",
"strict": true,
"schema": {
"type": "object",
"properties": { "city": { "type": "string" }, "date": { "type": "string" } },
"required": ["city", "date"],
"additionalProperties": false
}
}
}
}The answer's message.content is a JSON string: parse it.
Any valid JSON
{"type": "json_object"} asks for valid JSON without a schema. The router does not check that the model supports it: say in the prompt that you want JSON, and handle a parsing error.
Tips
- Keep the schema small: it is billed as input tokens.
- Even with
strict, check the result in your code; an answer cut bymax_tokens(finish_reason: "length") is not valid JSON.