python
runtime_error
ai_generated
true
PydanticUserError: `@validator` cannot be applied to instance methods. Use `@field_validator` instead.
ID: python/fastapi-pydantic-v2-validator-removed
80%Fix Rate
88%Confidence
0Evidence
2024-04-18First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.x | active | — | — | — |
Root Cause
Code written for Pydantic v1 uses the removed `validator` decorator under Pydantic v2.
generic中文
为 Pydantic v1 编写的代码在 Pydantic v2 下使用了已移除的 `validator` 装饰器。
Workarounds
-
92% success
Migrate to field_validator: from pydantic import field_validator @field_validator('name') @classmethod def check(cls, v): ... -
85% success
Use bump-pydantic to automate migration.
Dead Ends
Common approaches that don't work:
-
60% fail
FastAPI 0.100+ expects v2; downgrading causes other incompatibilities.
-
70% fail
Loses validation logic and may accept bad data.