Structured output
Get JSON that matches your schema with response_format — enforced token by token while the model writes, not checked afterwards.
For developers
Send response_format with a JSON Schema and the reply is JSON that matches it. Midium enforces the schema while the model writes: at each step, tokens that would break the schema can't be chosen (constrained decoding with Outlines). You don't need a validate-and-retry loop.
Request#
curl http://127.0.0.1:9100/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "YOUR_MODEL",
"messages": [{"role": "user", "content": "Classify: \"The update broke my login.\""}],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "ticket",
"schema": {
"type": "object",
"properties": {
"reasoning": {"type": "string"},
"category": {"type": "string", "enum": ["bug", "billing", "question"]},
"urgent": {"type": "boolean"}
},
"required": ["reasoning", "category", "urgent"]
}
}
}
}'
Parameters#
| Name | Type | Description |
|---|---|---|
response_format.type | string | json_schema: match a schema. json_object: any JSON object. |
response_format.json_schema.schema | object | The JSON Schema the reply must match: objects, arrays, nesting, enum, required, number and string types. |
response_format.json_schema.name | string | Accepted; not used. strict is accepted too — the schema is always enforced. |
On Anthropic Messages, send output_config: {"format": {"type": "json_schema", "schema": {...}}}.
Response#
The JSON is the message's content, as a string:
JSON
{
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "{\"reasoning\": \"A broken login after an update is a defect.\", \"category\": \"bug\", \"urgent\": true}"
},
"finish_reason": "stop"
}
]
}
Parse it with json.loads or JSON.parse.
Notes#
- Put a reasoning field first. The schema decides what the model may write, so a model held to
{"category": …}has no room to think before it picks. A string field such asreasoningorthought, listed first and required, lets it work the answer out before the constrained fields. Midium doesn't add one for you. - Streaming works. With
"stream": true, the JSON streams indelta.contentlike any reply. - First use of a schema. A new schema is compiled the first time it's used, so that request starts a little slower. Later requests with the same schema reuse it.
- Not with tools. When a request has
tools, the tool-call grammar takes the place ofresponse_format. See Tool calling. - Keep schemas reasonable. Very large or deeply nested schemas take longer to compile. Smaller models follow simple schemas better.
- A cut-off reply isn't valid JSON. If the reply hits its token cap (
finish_reason: "length"), the JSON is incomplete. Leavemax_tokensout, or set it high.
Errors#
| Status | When |
|---|---|
404 | model_not_found — no model by that name. |
See Errors & limits.
Something here wrong or out of date? Tell us.
