首页/AI/AI 应用速查/结构化输出(json_schema / JSON Mode)
本包目录(159 条)

结构化输出(json_schema / JSON Mode)

约束模型输出符合 JSON Schema:OpenAI 提供 response_format 的 json_schema(strict)与 JSON mode;Anthropic 常用强制工具调用变通。适合抽取、分类、入库场景。

AI 应用速查AI更新于 2026-10-12
方式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 约束只保证结构一致,不保证业务正确:数值范围、枚举合法性与幻觉内容仍需程序校验。

易错点

  • 以为 strict schema 能保证字段内容正确
  • 对不支持该参数的模型/接口调用导致 400

数据来源与参考

  • OpenAI Structured Outputs 文档

相关工具

​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌