llm
data_error
ai_generated
true
ValidationError: ResponseModel验证错误1个 name 字段必填 [type=missing, input_value={'title': 'Test'}, input_type=dict]
ValidationError: 1 validation error for ResponseModel name Field required [type=missing, input_value={'title': 'Test'}, input_type=dict]
ID: llm/langchain-output-parser-optional-field-missing
82%修复率
87%置信度
1证据数
2024-03-05首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| langchain 0.1.0 | active | — | — | — |
| langchain 0.1.5 | active | — | — | — |
| langchain 0.2.0 | active | — | — | — |
| pydantic 2.5.0 | active | — | — | — |
| pydantic 2.6.0 | active | — | — | — |
根因分析
LLM在JSON模式下的输出省略了Pydantic输出解析器模式中定义的必填字段,导致解析响应时验证失败。
English
LLM output in JSON mode omits a required field defined in the Pydantic output parser schema, causing validation failure when the response is parsed.
官方文档
https://python.langchain.com/docs/modules/model_io/output_parsers/pydantic解决方案
-
在系统提示中添加显式字段指令,确保LLM包含所有必填字段: from langchain.output_parsers import PydanticOutputParser parser = PydanticOutputParser(pydantic_object=ResponseModel) prompt = PromptTemplate( template="生成JSON输出。确保以下字段始终存在:{format_instructions}\n{query}", input_variables=["query"], partial_variables={"format_instructions": parser.get_format_instructions()}, ) -
实现回退解析器,用None或默认值填充缺失字段,并记录遗漏以供监控: try: parsed = parser.parse(llm_output) except ValidationError: import json data = json.loads(llm_output) data.setdefault('name', 'unknown') parsed = ResponseModel(**data)
无效尝试
常见但无效的做法:
-
50% 失败
Defeats the purpose of schema enforcement; LLM may skip critical fields entirely, leading to downstream data inconsistency.
-
70% 失败
LLM behavior is non-deterministic; retrying the same prompt often yields the same omission pattern, especially with temperature=0.