微信扫码
添加专属顾问
我要投稿
今天OpenAI的API新增加了一个功能,支持结构化输出,通俗的来说API的输出支持稳定的JSON模式。
为什么强调稳定呢?其实在去年的DevDay,OpenAI就介绍了JSON models,尽管它提高了模型输出是JSON的可靠性,但它不能百分之百确保输出是JSON。而今天提出的结构化输出终于确保API输出是JSON模式了。
其实这个功能非常非常有用,对于调用API的应用程序来说,从非结构化输入生成结构化输出是最核心的用例,想当年我们在调用OpenAI接口的时候,通过prompt让接口输出JSON,可有的时候行,有的时候又不行,根本找不到规律,现在这个问题终于解决了!
OpenAI经过测试,gpt-4o-2024-08-06支持结构化输出,其输出JSON的稳定性是100%,而gpt-4-0613得分不到40%,如下图:
说了这么多,如何使用API的结构化输出呢?有两种模式。
1:Function calling
在函数定义中设置 strict: true,即可通过tools进行结构化输出,如下图:
其输出大概如下:
2:response_format 参数
上述Function calling的方式个人觉得非常复杂,所以也可以在API中使用新增的response_format 参数:
POST /v1/chat/completions{"model": "gpt-4o-2024-08-06","messages": [{"role": "system","content": "You are a helpful math tutor."},{"role": "user","content": "solve 8x + 31 = 2"}],"response_format": {"type": "json_schema","json_schema": {"name": "math_response","strict": true,"schema": {"type": "object","properties": {"steps": {"type": "array","items": {"type": "object","properties": {"explanation": {"type": "string"},"output": {"type": "string"}},"required": ["explanation", "output"],"additionalProperties": false}},"final_answer": {"type": "string"}},"required": ["steps", "final_answer"],"additionalProperties": false}}}}输出如下:
{"steps": [{"explanation": "Subtract 31 from both sides to isolate the term with x.","output": "8x + 31 - 31 = 2 - 31"},{"explanation": "This simplifies to 8x = -29.","output": "8x = -29"},{"explanation": "Divide both sides by 8 to solve for x.","output": "x = -29 / 8"}],"final_answer": "x = -29 / 8"}虽然API request看上去很复杂,比如Python官方包做了封装,实现上述功能的代码如下:
from pydantic import BaseModelfrom openai import OpenAIclass Step(BaseModel):explanation: stroutput: strclass MathResponse(BaseModel):steps: list[Step]final_answer: strclient = OpenAI()completion = client.beta.chat.completions.parse(model="gpt-4o-2024-08-06",messages=[{"role": "system", "content": "You are a helpful math tutor."},{"role": "user", "content": "solve 8x + 31 = 2"},],response_format=MathResponse,)message = completion.choices[0].messageif message.parsed:print(message.parsed.steps)print(message.parsed.final_answer)else:print(message.refusal)
此外OpenAI特别强调了结构化输出功能是安全的,必须符合现有的安全策略,所以特别在API中新增加了一个输出字段refusal,用于描述是不符合JSON还是不安全!
这篇文章描述了如何使用结构化输出功能,其实OpenAI也介绍了如何实现该功能,这个有机会再讲,对于理解模型训练很有帮助!
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2026-02-06
Qwen3-Coder-Next 上线模力方舟:仅 3B 激活参数,媲美主流大模型的 Agent 编码能力
2026-02-06
给自己搭一个 AI 搭档:OpenClaw 安装使用全记录
2026-02-06
Qwen3-TTS:2026年最强开源文本转语音模型完全指南
2026-02-06
OpenClaw 爆火之后,我们看到了这些创业信号
2026-02-05
从Clawdbot到OpenClaw:爆款本地AI Agent的产品逻辑与争议
2026-02-05
Clawdbot 如何实现像人一样的长期记忆?
2026-02-05
全球最多下载中文开源数据集更新|OpenCSG持续打造中文高质量数据集开源底座
2026-02-05
【开源】12.1K Star!用 Markdown 给大模型装上“外挂大脑”,不写后端、不搭平台,这个开源项目让你用 Git 管理 AI 任务流
2025-11-19
2026-01-27
2025-12-22
2026-01-12
2026-01-29
2025-12-10
2025-11-17
2026-01-28
2025-12-23
2026-01-06
2026-02-05
2026-01-28
2026-01-26
2026-01-21
2026-01-21
2026-01-20
2026-01-16
2026-01-02