python module_error ai_generated true

pydantic.errors.PydanticUserError: `@validator` cannot be applied to instance methods. Use `@field_validator` instead.

ID: python/pydantic-deprecated-validator-v1

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-08-30First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
2.x active — — —

Root Cause

Pydantic v2 removed the v1 `@validator` decorator; using it (or importing from `pydantic` top-level) raises a user error.

generic

中文

Pydantic v2 移除了 v1 的 `@validator` 装饰器;使用它(或从 `pydantic` 顶层导入)会触发用户错误。

Workarounds

  1. 95% success
    Replace `@validator('x')` with `@field_validator('x')` and `@classmethod`; note `values` dict becomes a second positional arg `info`.
  2. 90% success
    Use `@model_validator(mode='after')` for cross-field validation.

Dead Ends

Common approaches that don't work:

  1. 30% fail

    Pinning to Pydantic v1 avoids the error but loses v2 performance and features.

  2. 50% fail

    Importing `validator` from `pydantic.deprecated` works temporarily but emits DeprecationWarning and will be removed.