FastAPI - Beginner to Pro / Project 2- Move Fast With FastAPI

Pydantic v1 vs Pydantic v2

🧩 Pydantic v1 vs Pydantic v2

FastAPI is compatible with both Pydantic v1 and Pydantic v2.

Depending on the version of FastAPI you are using, you may encounter minor differences in method names and configuration options.


🔄 Key Changes in Pydantic v2

The most significant updates include:

  • The .dict() method has been replaced with .model_dump()

  • The schema_extra attribute within the Config class has been renamed to json_schema_extra

  • Optional fields must be explicitly initialized with None

    Example:

    id: Optional[int] = None