--- name: fastapi-pydantic-v2 description: Writes modern FastAPI endpoints using Pydantic v2 syntax and Annotated dependencies. Use whenever generating or refactoring API routes. --- # FastAPI & Pydantic v2 Standards ## Pydantic v2 Rules - NEVER use `.dict()` or `.json()`. ALWAYS use `.model_dump()` and `.model_dump_json()`. - NEVER use `@validator` or `@root_validator`. ALWAYS use `@field_validator` and `@model_validator(mode="before|after")`. - NEVER use `schema_extra`. Use `json_schema_extra` inside the `model_config` dict. - Use `pydantic_settings.BaseSettings` for all environment variable configurations. ## FastAPI Rules - ALWAYS use `typing.Annotated` for dependency injection (e.g., `db: Annotated[AsyncSession, Depends(get_db)]`). - Do not use `def` for endpoints that perform I/O; always use `async def`. - Raise `fastapi.HTTPException` for expected client errors instead of returning standard dictionaries.