{
  "id": "llm/langchain-output-parser-optional-field-missing",
  "signature": "ValidationError: 1 validation error for ResponseModel\nname\n  Field required [type=missing, input_value={'title': 'Test'}, input_type=dict]",
  "signature_zh": "ValidationError: ResponseModel验证错误1个\nname\n  字段必填 [type=missing, input_value={'title': 'Test'}, input_type=dict]",
  "regex": "ValidationError: 1 validation error for \\w+\\n\\w+\\n  Field required \\[type=missing",
  "domain": "llm",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "LLM output in JSON mode omits a required field defined in the Pydantic output parser schema, causing validation failure when the response is parsed.",
  "root_cause_type": "generic",
  "root_cause_zh": "LLM在JSON模式下的输出省略了Pydantic输出解析器模式中定义的必填字段，导致解析响应时验证失败。",
  "versions": [
    {
      "version": "langchain 0.1.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "langchain 0.1.5",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "langchain 0.2.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "pydantic 2.5.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "pydantic 2.6.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Defeats the purpose of schema enforcement; LLM may skip critical fields entirely, leading to downstream data inconsistency.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "LLM behavior is non-deterministic; retrying the same prompt often yields the same omission pattern, especially with temperature=0.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Add explicit field instructions in the system prompt to ensure LLM includes all required fields:\n\nfrom langchain.output_parsers import PydanticOutputParser\nparser = PydanticOutputParser(pydantic_object=ResponseModel)\nprompt = PromptTemplate(\n    template=\"Generate JSON output. Ensure the following fields are always present: {format_instructions}\\n{query}\",\n    input_variables=[\"query\"],\n    partial_variables={\"format_instructions\": parser.get_format_instructions()},\n)",
      "success_rate": 0.9,
      "how": "Add explicit field instructions in the system prompt to ensure LLM includes all required fields:\n\nfrom langchain.output_parsers import PydanticOutputParser\nparser = PydanticOutputParser(pydantic_object=ResponseModel)\nprompt = PromptTemplate(\n    template=\"Generate JSON output. Ensure the following fields are always present: {format_instructions}\\n{query}\",\n    input_variables=[\"query\"],\n    partial_variables={\"format_instructions\": parser.get_format_instructions()},\n)",
      "condition": "",
      "sources": []
    },
    {
      "action": "Implement a fallback parser that fills missing fields with None or defaults and logs the omission for monitoring:\n\ntry:\n    parsed = parser.parse(llm_output)\nexcept ValidationError:\n    import json\n    data = json.loads(llm_output)\n    data.setdefault('name', 'unknown')\n    parsed = ResponseModel(**data)",
      "success_rate": 0.85,
      "how": "Implement a fallback parser that fills missing fields with None or defaults and logs the omission for monitoring:\n\ntry:\n    parsed = parser.parse(llm_output)\nexcept ValidationError:\n    import json\n    data = json.loads(llm_output)\n    data.setdefault('name', 'unknown')\n    parsed = ResponseModel(**data)",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "在系统提示中添加显式字段指令，确保LLM包含所有必填字段：\n\nfrom langchain.output_parsers import PydanticOutputParser\nparser = PydanticOutputParser(pydantic_object=ResponseModel)\nprompt = PromptTemplate(\n    template=\"生成JSON输出。确保以下字段始终存在：{format_instructions}\\n{query}\",\n    input_variables=[\"query\"],\n    partial_variables={\"format_instructions\": parser.get_format_instructions()},\n)",
    "实现回退解析器，用None或默认值填充缺失字段，并记录遗漏以供监控：\n\ntry:\n    parsed = parser.parse(llm_output)\nexcept ValidationError:\n    import json\n    data = json.loads(llm_output)\n    data.setdefault('name', 'unknown')\n    parsed = ResponseModel(**data)"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://python.langchain.com/docs/modules/model_io/output_parsers/pydantic",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.87,
  "fix_success_rate": 0.82,
  "resolvable": "true",
  "first_seen": "2024-03-05",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}