{
  "id": "python/pydantic-validation-error-input-type",
  "signature": "pydantic_core._pydantic_core.ValidationError: 1 validation error for User\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 个验证错误，字段 age 输入应为有效整数，无法将字符串解析为整数",
  "regex": "pydantic_core\\._pydantic_core\\.ValidationError:\\ 1\\ validation\\ error\\ for\\ User\\\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": "Pydantic v2 strict type coercion: a string field was passed where an int was declared, and the string is not a numeric literal so Pydantic refuses to coerce it. Unlike v1, v2 no longer silently coerces arbitrary strings to ints by default.",
  "root_cause_type": "generic",
  "root_cause_zh": "Pydantic v2 严格类型转换：声明为 int 的字段收到了字符串，且该字符串不是数字字面量，因此 Pydantic 拒绝转换。与 v1 不同，v2 默认不再将任意字符串静默转换为整数。",
  "versions": [
    {
      "version": "2.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Silently swallowing ValidationError means the invalid object never gets constructed; downstream code receives None or crashes with AttributeError later.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "strict=False only relaxes some coercions; 'twenty' is not a valid integer representation and will still raise int_parsing.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "v1 also raises for non-numeric strings like 'twenty'; the behavior difference is only for numeric-looking strings such as '25'.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.85,
      "how": "from pydantic import BaseModel, field_validator\n\nclass User(BaseModel):\n    age: int\n\n    @field_validator('age', mode='before')\n    @classmethod\n    def parse_age(cls, v):\n        if isinstance(v, str) and v.isalpha():\n            words = {'twenty': 20, 'thirty': 30}\n            return words.get(v.lower(), v)\n        return v",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.8,
      "how": "from typing import Union\nfrom pydantic import BaseModel, BeforeValidator\nfrom typing_extensions import Annotated\n\nCoercedInt = Annotated[int, BeforeValidator(lambda v: int(v) if str(v).isdigit() else v)]\n\nclass User(BaseModel):\n    age: CoercedInt",
      "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-06-12",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}