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-deprecatedCommon 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
- Replace
@validatorwith@field_validator(and@root_validatorwith@model_validator). - Replace
class Configwithmodel_config = ConfigDict(...). - 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-pydanticto mechanically migrate large codebases.
Related guides
Pydantic v2 "BaseSettings has been moved" in CIFix the Pydantic v1-to-v2 "BaseSettings has been moved to pydantic-settings" error in CI - settings classes m…
pydantic "ValidationError" in tests in CIFix pydantic "ValidationError" in tests in CI - data did not match the model schema, often from a v1-to-v2 be…
Python "AttributeError: module X has no attribute Y" in CIFix "AttributeError: module X has no attribute Y" in CI - the imported module loaded but the name you accesse…