openai.BadRequestError: 使用 response_format 参数时不支持 logprobs
openai.BadRequestError: logprobs is not supported when using response_format parameter
ID: llm/logprobs-request-fails-on-structured-output
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 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.
官方文档
https://platform.openai.com/docs/guides/structured-outputs/supported-models解决方案
-
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`. -
If logprobs are needed, use a separate API call without response_format and parse JSON manually with error handling.
-
Switch to a model or provider that supports both features simultaneously (e.g., some open-source models with guided decoding).
无效尝试
常见但无效的做法:
-
70% 失败
The error occurs when logprobs is present at all, even if set to false; the API still validates the field presence.
-
90% 失败
Older API versions may not support structured output at all, or have different bugs; the restriction is intentional and consistent across versions.
-
50% 失败
This works functionally but defeats the purpose of using structured output for guaranteed JSON.