llm data_error ai_generated true

结构化输出 JSON 模式返回意外字段:输出包含模式中未定义的键

json.decoder.JSONDecodeError: Expecting property name at position N

ID: llm/structured-output-json-mode-returns-unexpected-field

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

版本兼容性

版本状态引入弃用备注
openai>=1.0.0 active
pydantic>=2.0.0 active
python>=3.9 active

根因分析

使用 JSON 模式(response_format={type: 'json_object'})时,LLM 生成包含请求模式中不存在的键的 JSON 对象,导致下游解析失败或验证拒绝输出。

English

When using JSON mode (response_format={type: 'json_object'}), the LLM generates a JSON object with keys not present in the requested schema, causing downstream parsing to fail or validation to reject the output.

generic

官方文档

https://platform.openai.com/docs/guides/text-generation/json-mode

解决方案

  1. Use Pydantic's 'extra' parameter set to 'forbid' to reject extra fields, and re-prompt the LLM on failure.
  2. Post-process the JSON output to filter out unexpected keys before validation.

无效尝试

常见但无效的做法:

  1. Add more examples to the system prompt showing the exact JSON structure 60% 失败

    LLMs may still hallucinate extra fields even with examples; examples reduce but don't eliminate the issue.

  2. Use a stronger model like GPT-4 instead of GPT-3.5 50% 失败

    While GPT-4 is more reliable, it can still produce extra fields in JSON mode, especially with complex schemas.