{
  "id": "python/pydantic-string-type-coercion-strict",
  "signature": "pydantic_core._pydantic_core.ValidationError: 1 validation error for Config\nport\n  Input should be a valid integer, unable to parse string as an integer [type=int_parsing, input_value='8080abc', input_type=str]",
  "signature_zh": "pydantic_core._pydantic_core.ValidationError: Config 校验失败\nport\n  输入应为有效整数，无法将字符串解析为整数 [type=int_parsing]",
  "regex": "pydantic_core\\._pydantic_core\\.ValidationError:\\ 1\\ validation\\ error\\ for\\ Config\\\nport\\\n\\ \\ Input\\ should\\ be\\ a\\ valid\\ integer,\\ unable\\ to\\ parse\\ string\\ as\\ an\\ integer\\ \\[type=int_parsing,\\ input_value='8080abc',\\ input_type=str\\]",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "A string that cannot be coerced to int was supplied for an int field; Pydantic's lax mode attempts parsing but fails on non-numeric content.",
  "root_cause_type": "generic",
  "root_cause_zh": "为 int 字段提供了无法转换为整数的字符串，宽松模式尝试解析但遇到非数字内容而失败。",
  "versions": [
    {
      "version": "2.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Shifts the failure to a later int() call and loses Pydantic's validation guarantees at the boundary.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "validate_assignment only affects attribute assignment, not constructor validation of input data.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Raises the same ValueError; the underlying data is genuinely malformed.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.9,
      "how": "import re\nraw = \"8080abc\"\ncleaned = re.sub(r\"\\D\", \"\", raw) or \"0\"\ncfg = Config(port=int(cleaned))",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.88,
      "how": "from pydantic import field_validator\nclass Config(BaseModel):\n    port: int\n    @field_validator(\"port\", mode=\"before\")\n    @classmethod\n    def clean_port(cls, v):\n        if isinstance(v, str):\n            v = re.sub(r\"\\D\", \"\", v)\n        return v",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.75,
      "how": "from typing import Union\nclass Config(BaseModel):\n    port: Union[int, str]",
      "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.86,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-05-22",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}