python migration_error ai_generated true

PydanticUserError: A non-annotated attribute was detected. All model fields require a type annotation.

ID: python/pydantic-v2-field-validation-error

Also available as: JSON · Markdown
88%Fix Rate
90%Confidence
60Evidence
2023-06-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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.

generic

Workarounds

  1. 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.

    Sources: https://docs.pydantic.dev/latest/migration/

  2. 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.

    Sources: https://docs.pydantic.dev/latest/concepts/config/

  3. 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:

  1. 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.

  2. 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.

  3. 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.

Error Chain

Leads to:
Frequently confused with: