PydanticUserError: A non-annotated attribute was detected. All model fields require a type annotation.
ID: python/pydantic-v2-field-validation-error
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 311 | active | — | — | — |
Root Cause
Pydantic v1 to v2 migration error. Class-level attributes, Config inner class, and @validator decorators must be updated to v2 syntax. The bump-pydantic tool can automate ~80% of the migration.
genericWorkarounds
-
85% success Run bump-pydantic to auto-migrate models
pip install bump-pydantic && bump-pydantic . This tool handles ~80% of the migration automatically: renames Config to model_config dict, @validator to @field_validator, orm_mode to from_attributes, and more.
-
92% success Replace class Config with model_config = ConfigDict(...)
Change 'class Config: orm_mode = True' to 'model_config = ConfigDict(from_attributes=True)'. Import ConfigDict from pydantic. This is a class-level variable, not an inner class.
-
88% success Update validators from @validator to @field_validator
Change @validator('field_name') to @field_validator('field_name') with mode='before' or mode='after'. The new validators receive FieldValidationInfo instead of cls as the first argument after self.Sources: https://docs.pydantic.dev/latest/concepts/validators/
Dead Ends
Common approaches that don't work:
-
Pinning pydantic<2 in requirements
75% fail
This blocks FastAPI >=0.100, SQLModel, and all new pydantic-dependent libraries. It creates cascading dependency conflicts and technical debt. Pydantic v1 is no longer maintained.
-
Adding class Config: orm_mode = True
88% fail
In Pydantic v2, orm_mode was renamed to from_attributes in model_config. The old class Config with orm_mode is silently ignored — your model loses ORM support without any error or warning.
-
Using @validator decorator from pydantic v1
70% fail
@validator is importable in v2 for backward compatibility but produces subtly different behavior. It does not support mode='before'/'after' and has different argument order. Use @field_validator instead.