設定 response_format
JSON 模式適合簡單物件。JSON Schema 還會加入結構描述名稱、欄位結構、必填屬性,以及拒絕額外屬性的方式。
嚴格結構描述可減少防禦性解析,但絕不能取代應用程式端驗證。
JSON Schema 回覆格式
json
{
"model": "openai/gpt-5.6-sol",
"messages": [{"role": "user", "content": "Extract an order summary."}],
"response_format": {
"type": "json_schema",
"json_schema": {
"name": "order_summary",
"strict": true,
"schema": {
"type": "object",
"properties": {"items": {"type": "array"}, "total": {"type": "number"}},
"required": ["items", "total"],
"additionalProperties": false
}
}
}
}設計結構描述
- 欄位與說明應簡潔且符合領域需求。
- 不接受未知欄位時,請設定 additionalProperties: false。
- 請在自己的程式碼中為結構描述建立版本,不要暗中變更消費端合約。
處理結果
使用 API 或資料庫採用的同一驗證器來驗證傳回的 JSON。
若模型或路由不接受所需格式,請顯示受控錯誤,並透過模型目錄選擇相容選項。