Interview Q&A · questions with model answers
FastAPI Interview Questions
Ordered by how often they're asked. Most-asked answers are free to read in full; members see every answer and can rate their confidence on each.
-
Most asked What is FastAPI and what makes it different from Flask or Django?
FastAPI is a modern ASGI Python web framework built on Starlette and Pydantic. It uses type hints to drive request validation, serialization, dependency injection and automatic OpenAPI docs.
-
Most asked What does `response_model` (or a return annotation) do in FastAPI?
It declares the output schema: FastAPI validates and filters the returned data through that model, so extra fields (like password hashes) are excluded, and it documents the response in OpenAPI.
-
What changed between Pydantic v1 and v2 that affects FastAPI code?
Pydantic v2 rewrote the core in Rust (much faster) and renamed many APIs: .dict() → .model_dump(), .json() → .model_dump_json(), parse_obj → model_validate, @validator → @field_validator, Config →…
-
What are discriminated unions in Pydantic and why are they useful in APIs?
A discriminated (tagged) union uses a literal field such as type to decide which model in a union to validate against, giving precise errors and faster validation than trying each member in turn.
-
What does Starlette provide underneath FastAPI?
Starlette provides the ASGI toolkit: routing, Request/Response classes, middleware, WebSockets, background tasks, lifespan, static files, sessions and the test client. FastAPI adds Pydantic…
-
What is Schemathesis and how does it help test FastAPI apps?
Schemathesis generates test cases from your OpenAPI schema (property-based testing with Hypothesis) and sends them to the API, finding 500 errors, schema violations and validation gaps.
-
What are uvloop and httptools, and how do they relate to Uvicorn?
uvloop is a fast drop-in replacement for asyncio's event loop built on libuv, and httptools is a fast HTTP parser; uvicorn[standard] installs both, and Uvicorn uses them automatically for better…
-
What does dependency caching mean in FastAPI and when would you disable it?
Within one request, if several parameters or sub-dependencies use the same dependency, FastAPI calls it once and reuses the result. Use Depends(fn, use_cache=False) when each usage needs a fresh…
-
What does FastAPI's default 422 validation error look like, and how can you customize it?
It's JSON with a detail array; each item has loc (e.g., ['body', 'email']), msg, type and input. Override it with an @app.exception_handler(RequestValidationError) handler to match your API's error…
-
What are the trade-offs of soft deletes in a FastAPI/SQLAlchemy API?
Soft deletes (a deleted_at column) allow recovery and auditing but require every query to filter deleted rows, complicate unique constraints, and can conflict with privacy deletion requirements.
-
What role does AnyIO play in FastAPI?
Starlette and FastAPI use AnyIO, an async compatibility layer over asyncio (and Trio), for threadpool offloading, task groups and test utilities, so the framework isn't tightly bound to one event…
-
What does a good CI pipeline for a FastAPI service include?
Linting/formatting (Ruff), type checking (mypy/pyright), unit and integration tests with coverage (pytest, a Postgres service container), security scanning (pip-audit, Bandit), OpenAPI schema…
-
What is the transactional outbox pattern and why use it with FastAPI?
You write domain changes and an 'event to publish' row in the same DB transaction; a separate relay process publishes outbox rows to the message broker and marks them sent, guaranteeing events…
-
What Gunicorn/Uvicorn settings matter in production?
Worker count, worker class (uvicorn.workers.UvicornWorker with Gunicorn), timeouts (--timeout, --graceful-timeout, keep-alive), max requests with jitter to recycle workers, and proxy header settings.