Structured JSON output
Constrain output to a schema and validate it before using it in application logic.
Request structured JSON
Send response_format when the selected model route advertises structured_output in GET /v1/models. Kendr managed routing filters its candidates for that capability. An exact alias without the capability fails instead of silently dropping the schema.
curl https://api.kendr.org/v1/chat/completions \
-H "Authorization: Bearer $KENDR_API_KEY" \
-H "Idempotency-Key: structured-ticket-001" \
-H "Content-Type: application/json" \
-d '{
"model": "kendr-intelligent",
"messages": [{"role": "user", "content": "Classify: Checkout fails after payment."}],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "support_ticket",
"strict": true,
"schema": {
"type": "object",
"properties": {
"category": {"type": "string"},
"urgent": {"type": "boolean"}
},
"required": ["category", "urgent"],
"additionalProperties": false
}
}
}
}'
Treat the returned assistant content as untrusted input until your application parses and validates it against the same schema. Compatibility support routes the request to a capable model; it does not replace application-side validation.
Validate before acting
Check for refusal and a non-success finish reason before parsing. A schema constrains shape, not factual correctness. Validate required fields, types, enums, and unexpected properties before storing a result.
import json
# response is returned by client.chat.completions.create(...).
choice = response.choices[0]
if choice.finish_reason != "stop" or getattr(choice.message, "refusal", None):
raise ValueError("Response needs review")
value = json.loads(choice.message.content or "null")
if not isinstance(value, dict) or set(value) != {"category", "urgent"}:
raise ValueError("Unexpected fields")
if not isinstance(value["category"], str) or type(value["urgent"]) is not bool:
raise ValueError("Unexpected field types")