FastAPI
Path operations, Pydantic, dependency injection, async, and FastAPI patterns asked in Python backend interviews.
Fundamentals
What is FastAPI
Modern async Python web framework built on Starlette and Pydantic, with auto-generated OpenAPI docs.
FastAPI vs Flask vs Django
How the three big Python web frameworks compare on async, validation, performance, and use cases.
ASGI vs WSGI
Sync vs async Python server interfaces, why FastAPI needs uvicorn, and what gunicorn+uvicorn-workers actually does.
First FastAPI App
Spinning up a FastAPI app with uvicorn and exploring the auto-generated /docs and /redoc.
Path Operations
Path Operations
Registering routes with @app.get/post/put/delete/patch, adding metadata for docs, and why route order matters.
Path Parameters
Capturing typed values from the URL path, validating with Path(), and constraining with Enum.
Query Parameters
Reading typed query params, marking them optional, validating with Query(), and accepting lists.
Request Body & Pydantic
Defining body schemas with Pydantic models, mixing multiple body params, and using Body() to embed single fields.
Pydantic
Pydantic Models
Define data shapes with type hints, get validation and serialization for free.
Field Validation
Add constraints to fields with Field() — lengths, ranges, regex, defaults.
Custom Validators
When Field() isn't enough — @field_validator and @model_validator in Pydantic v2.
Nested Models
Compose models inside models — Pydantic validates the whole tree and FastAPI documents it.
Dependency Injection
Depends()
FastAPI's killer feature — declare what your handler needs, FastAPI builds and injects it.
Class Dependencies
Use a class as a dependency when you want grouped params, shared state, or testability.
Sub-dependencies
Dependencies that depend on dependencies — FastAPI builds the whole graph per request.
Yield Dependencies
Dependencies with setup and teardown — the FastAPI way to do DB sessions cleanly.
Async & Responses
async vs sync Routes
When to use def vs async def in FastAPI, and the one mistake that destroys performance.
Response Models & Status Codes
Control what FastAPI sends back, filter out secrets, and return the right HTTP status.
Auth & Security
Production
Middleware & CORS
Wrap every request with custom logic, and let browsers talk to your API from another origin.
Background Tasks
Run work after sending the response, and know when to reach for Celery instead.
WebSockets
Bidirectional connections in FastAPI — chat, live updates, and when SSE is the better call.
Testing FastAPI
TestClient for sync, httpx.AsyncClient for async, and dependency overrides to mock anything.
Settings & Config
Typed config via pydantic-settings, .env files, and why lru_cache wraps the whole thing.