You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure OpenAI GPT-4o mini的response_format参数报错:无效参数

解决Azure OpenAI API设置response_format为json_schema时的400错误

问题原因

Azure OpenAI API目前不支持response_format参数的json_schema类型,仅允许传入json_object或text,这是与原生OpenAI API的功能差异,直接使用json_schema会触发400 BadRequestError。

解决方案

1. 改用json_object配合提示词约束格式

放弃json_schema参数,将response_format设为json_object,同时通过系统提示词明确指定输出的JSON结构,以此实现类似json_schema的格式约束效果。

2. 确保使用支持的模型

需使用Azure OpenAI支持的最新模型(如gpt-4o、gpt-4-turbo),旧模型可能不支持json_object格式。

代码示例对比

错误代码片段

response_format={
    "type": "json_schema",
    "json_schema": {
        "name": "math_tutor_response",
        "schema": {
            "type": "object",
            "properties": {
                "question": {"type": "string"},
                "step_by_step_solution": {"type": "string"},
                "final_answer": {"type": "string"}
            },
            "required": ["question", "step_by_step_solution", "final_answer"]
        }
    }
}

修正后的代码

# 设置response_format为json_object
response_format={"type": "json_object"}

# 在系统提示词中明确JSON结构要求
system_prompt = """
你是数学导师,请严格按照以下JSON格式输出结果,不得添加任何额外内容:
{
    "question": "用户提出的数学问题",
    "step_by_step_solution": "详细的解题步骤说明",
    "final_answer": "问题的最终答案"
}
"""

# 调用API
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": "求解方程2x+5=15"}
    ],
    response_format=response_format
)

额外建议

如果需要更严格的格式校验,可在获取API返回结果后,使用jsonschema等库自行验证输出结构;同时可定期查看Azure OpenAI官方更新,确认是否已支持json_schema参数。

内容的提问来源于stack exchange,提问作者YL95

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 15:12:04