llm config_error ai_generated true

openai.BadRequestError:在使用'tools'参数时不支持'response_format'参数

openai.BadRequestError: 'response_format' parameter is not supported when using 'tools' parameter

ID: llm/function-call-json-mode-conflict

其他格式: JSON · Markdown 中文 · English
85%修复率
88%置信度
1证据数
2024-04-15首次发现

版本兼容性

版本状态引入弃用备注
openai>=1.0.0 active
gpt-4-turbo-2024-04-09 active
gpt-4o-2024-05-13 active

根因分析

OpenAI API 强制要求通过 response_format 进行结构化输出与函数/工具调用互斥,同一请求中不能同时指定两者。

English

OpenAI API enforces mutual exclusivity between structured output via response_format and function/tool calling; both cannot be specified in the same request.

generic

官方文档

https://platform.openai.com/docs/guides/structured-outputs/supported-models

解决方案

  1. 使用单个工具的函数调用包装所需的结构化输出:定义一个函数,其参数为输出模式,并将函数调用参数解析为结构化结果。
  2. 使用带有 response_format 的 JSON 模式,通过提示工程手动处理工具调用:在系统消息中包含工具描述,并解析 LLM 的自然语言响应以提取工具调用。
  3. 如果使用 OpenAI Python SDK v1.x,在使用 response_format 时显式传递 tools=None,并在后处理步骤中单独管理工具逻辑。

无效尝试

常见但无效的做法:

  1. 75% 失败

    LLM cannot reliably execute tool calls without explicit tools parameter; output format may not match expected schema

  2. 90% 失败

    response_format enforces strict output schema; tool calls have reserved structure that conflicts with arbitrary schemas

  3. 60% 失败

    Older API versions may lack the feature entirely or have different buggy behavior; not a sustainable fix