Skip to content
Latchkey

Pydantic v1 vs v2 API skew breaks CI

Pydantic v2 is a major rewrite: @validator became @field_validator, class Config became model_config, and .dict()/.json() were renamed. Code written for v1 fails or warns-as-error once v2 is installed.

What this error means

After a pydantic v2 bump, models fail with errors like "PydanticUserError: @validator is deprecated, use @field_validator" or attribute errors on removed methods.

python
pydantic.errors.PydanticUserError: `@validator` is deprecated.
Use `@field_validator` instead.

For further information visit https://errors.pydantic.dev/2.0/u/validator-deprecated

Common causes

v1 APIs used under pydantic v2

Renamed validators, config classes, and serialization methods no longer exist as before, so v1 patterns error out.

An indirect dependency forced the v2 upgrade

Another library required pydantic>=2, pulling your environment to v2 before your own code migrated.

How to fix it

Migrate the code to v2 APIs

  1. Replace @validator with @field_validator (and @root_validator with @model_validator).
  2. Replace class Config with model_config = ConfigDict(...).
  3. Replace .dict()/.json() with .model_dump()/.model_dump_json().
Python
from pydantic import BaseModel, field_validator, ConfigDict

class User(BaseModel):
    model_config = ConfigDict(populate_by_name=True)
    name: str

    @field_validator("name")
    @classmethod
    def non_empty(cls, v): return v or "anon"

Pin to v1 to stage the migration

Cap pydantic below 2 while you migrate incrementally.

Terminal
pip install "pydantic>=1.10,<2"

How to prevent it

  • Read the official pydantic v2 migration guide before bumping.
  • Pin pydantic so major upgrades are deliberate, lockfile-driven changes.
  • Run bump-pydantic to mechanically migrate large codebases.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →