What is Pydantic v2 and what changed from v1?
Answer
Pydantic v2 (released 2023) is a complete rewrite of the core validation logic in Rust via the pydantic-core package, resulting in 5-50x performance improvements. Key API changes: orm_mode renamed to model_config = ConfigDict(from_attributes=True). @validator replaced by @field_validator (with different signature). @root_validator replaced by @model_validator. Schema serialization: .dict() and .json() replaced by .model_dump() and .model_dump_json(). model_fields instead of __fields__. FastAPI 0.100+ supports Pydantic v2 with the new patterns. Most changes are backward-compatible with a compatibility layer; the pydantic.v1 module retains v1 behavior for gradual migration. Pydantic v2 also introduces @computed_field for derived properties and improved discriminated unions.
Previous
How do you implement background tasks in FastAPI?
Next
How do you implement middleware in FastAPI?
More FastAPI / Flask Questions
View all →- Intermediate How do you implement JWT authentication in FastAPI?
- Intermediate How does SQLAlchemy async work with FastAPI?
- Intermediate What is Flask's application factory pattern?
- Intermediate How do you implement background tasks in FastAPI?
- Intermediate How do you implement middleware in FastAPI?