Structured output
Schema validation and truncation still need to be performed after requesting JSON.
Select output mode
Models that support JSON schema can use their native structured output capabilities; when only JSON schema is supported, fields and types still need to be specified in the prompt word. Models that do not support these parameters should remove the relevant fields to avoid mistaking “prompt to return JSON” as a protocol guarantee.
Chat request fragment
{
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "answer",
"strict": true,
"schema": {
"type": "object",
"properties": {
"answer": {
"type": "string"
}
},
"required": [
"answer"
],
"additionalProperties": false
}
}
}
}
This is an example of optional fields. First confirm the target model and channel support. Responses use its own format configuration structure and Chat fields should not be copied directly.
Verification process
Read the complete response, check the end reason, then parse the JSON and run schema validation. Reject responses, overlong truncation, empty replies, and upstream errors are all handled independently. Business constraints such as number ranges, dates, and resource ownership cannot be guaranteed by schema descriptions alone.
Failure handling
Save the failure category after desensitization. Only limited retries are performed on recoverable errors, and additional costs are counted. Don’t use string replacement to quietly fix critical financial fields, and don’t write substandard results to formal business data.
Complete Chat example
curl --fail-with-body https://dunduntoken.com/v1/chat/completions -H "Authorization: Bearer $DUNDUN_API_KEY" -H "Content-Type: application/json" -d '{"model":"MODEL_ID","messages":[{"role":"user","content":"返回 answer 字段,内容为 OK"}],"response_format":{"type":"json_schema","json_schema":{"name":"answer","strict":true,"schema":{"type":"object","properties":{"answer":{"type":"string"}},"required":["answer"],"additionalProperties":false}}}}'
The model needs to support the modes shown. The returned content is JSON text, and the HTTP response itself is still a Chat packaging structure. The entire response cannot be directly used as a business answer.
Verification is divided into three levels
- Check the HTTP status, model end reason, and rejection information.
- Extract the complete text, parse JSON and verify it against the expected schema.
- Check business rules such as allowed enumerations, time ranges, and resource permissions.
During streaming, complete results are accumulated first, and unclosed objects are not written into formal data. When only JSON object mode is supported, the prompt word must still state the output requirements and retain the same verification.
How to deal with incompatibility
When 400 prompts that the format is unknown, check the model capabilities first. You cannot silently remove the constraints and then mark the free text as structured successfully. The application explicitly chooses to downgrade when it is absolutely necessary, and returns an identifiable error when verification fails; limited retries also need to be factored into the budget.