| 方式 | response_format = {type: json_schema, json_schema: {..., strict: true}} |
|---|---|
| JSON mode | 仅保证输出是合法 JSON,不保证结构 |
| Anthropic 变通 | tool_choice 强制指定工具,参数即结构化结果 |
| 兜底 | 服务端仍要做 schema 校验与重试 |
抽取为固定结构(curl)
抽取为固定结构(curl)
curl -s https://api.openai.com/v1/chat/completions -H "Authorization: Bearer $OPENAI_API_KEY" -H "Content-Type: application/json" -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"抽取:张三,32 岁"}],"response_format":{"type":"json_schema","json_schema":{"name":"person","strict":true,"schema":{"type":"object","properties":{"name":{"type":"string"},"age":{"type":"integer"}},"required":["name","age"],"additionalProperties":false}}}}'schema 约束只保证结构一致,不保证业务正确:数值范围、枚举合法性与幻觉内容仍需程序校验。