{
  "id": "python/pydantic-int-parsing-string",
  "signature": "pydantic_core._pydantic_core.ValidationError: 1 validation error for Model\nage\n  Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='twenty', input_type=str]",
  "signature_zh": "pydantic_core._pydantic_core.ValidationError：1 个验证错误（Model）\nage\n  输入应为有效的整数，无法将字符串解析为整数 [type=int_parsing, input_value='twenty', input_type=str]",
  "regex": "pydantic_core\\._pydantic_core\\.ValidationError:\\ 1\\ validation\\ error\\ for\\ Model\\\nage\\\n\\ \\ Input\\ should\\ be\\ a\\ valid\\ integer,\\ unable\\ to\\ parse\\ string\\ as\\ an\\ integer\\ \\[type=int_parsing,\\ input_value='twenty',\\ input_type=str\\]",
  "domain": "python",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "A string like 'twenty' or '12.5' was passed to an int field. Pydantic v2 strict parsing refuses to coerce non-numeric strings.",
  "root_cause_type": "generic",
  "root_cause_zh": "向 int 字段传入了诸如 'twenty' 或 '12.5' 的字符串。Pydantic v2 的严格解析拒绝强制转换非数字字符串。",
  "versions": [
    {
      "version": "2.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "int() raises ValueError inside validators, producing a confusing 500 rather than a clean ValidationError.",
      "fail_rate": 0.62,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "strict=False still won't parse 'twenty' into an int; it only relaxes numeric-string coercion of digits.",
      "fail_rate": 0.48,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Loses field-level detail and downstream code relying on .errors() breaks.",
      "fail_rate": 0.35,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "from pydantic import BaseModel, field_validator\nclass M(BaseModel):\n    age: int\n    @field_validator('age', mode='before')\n    @classmethod\n    def coerce(cls, v):\n        if isinstance(v, str) and v.isdigit():\n            return int(v)\n        return v",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.85,
      "how": "from fastapi import FastAPI, HTTPException\nfrom pydantic import ValidationError\ntry:\n    M(**payload)\nexcept ValidationError as e:\n    raise HTTPException(422, e.errors())",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-03-12",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}