llm api_error ai_generated true

openai.BadRequestError: 使用 response_format 参数时不支持 logprobs

openai.BadRequestError: logprobs is not supported when using response_format parameter

ID: llm/logprobs-request-fails-on-structured-output

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

版本兼容性

版本状态引入弃用备注
openai-python>=1.30.0 active
gpt-4o-2024-08-06 active
gpt-4o-mini-2024-07-18 active

根因分析

OpenAI API 不允许在启用结构化输出(response_format)时请求 logprobs,因为约束解码机制无法生成 token 级别的概率。

English

OpenAI API does not allow requesting logprobs when structured output (response_format) is enabled, as the constrained decoding mechanism cannot produce token-level probabilities.

generic

官方文档

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

解决方案

  1. Remove logprobs from the request entirely when using response_format. Example: `response = client.chat.completions.create(model="gpt-4o", messages=messages, response_format={ "type": "json_object" })` without `logprobs`.
  2. If logprobs are needed, use a separate API call without response_format and parse JSON manually with error handling.
  3. Switch to a model or provider that supports both features simultaneously (e.g., some open-source models with guided decoding).

无效尝试

常见但无效的做法:

  1. 70% 失败

    The error occurs when logprobs is present at all, even if set to false; the API still validates the field presence.

  2. 90% 失败

    Older API versions may not support structured output at all, or have different bugs; the restriction is intentional and consistent across versions.

  3. 50% 失败

    This works functionally but defeats the purpose of using structured output for guaranteed JSON.