# pydantic_core._pydantic_core.ValidationError: 1 validation error for User
age
  输入应为有效整数，无法将字符串解析为整数 [type=int_parsing, input_value='abc', input_type=str]

- **ID:** `python/pydantic-validation-error-model-type`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

Pydantic v2 默认严格模式下，将非数字字符串 'abc' 传入 int 字段时会因无法解析而抛出验证错误。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 2.x | active | — | — |

## 解决方案

1. **** (92% 成功率)
   ```
   from pydantic import BaseModel, field_validator
class User(BaseModel):
    age: int
    @field_validator('age', mode='before')
    @classmethod
    def parse_age(cls, v):
        if isinstance(v, str) and not v.isdigit():
            raise ValueError('age must be numeric')
        return int(v)
   ```
2. **** (95% 成功率)
   ```
   from pydantic import ValidationError
try:
    User(age='abc')
except ValidationError as e:
    return {'errors': e.errors()}, 422
   ```

## 无效尝试

- **** — strict=False still refuses to parse 'abc' into int because no valid numeric representation exists. (75% 失败率)
- **** — This hides the error but downstream code expecting int will crash with TypeError. (60% 失败率)
