Architecture

FastAPI interview question · Medium difficulty

How does a request flow through FastAPI's Uvicorn/Starlette/Pydantic stack?medium

Status
✓
Type
conceptual
Topic
architecture
Frequency
common
Tags
FastAPI, Uvicorn, Starlette, ASGI, WSGI
Answer

Uvicorn, an ASGI server, accepts the raw HTTP/WebSocket connection; Starlette routes the request to the matching path operation; FastAPI uses Pydantic to parse and validate the path, query, and body data against your type hints; your endpoint function runs; and the return value is serialized back to JSON.

Explanation

FastAPI doesn't handle raw sockets itself — it sits on top of a small stack where each layer has one job. ASGI (Asynchronous Server Gateway Interface) is the async successor to WSGI and is what makes native async def endpoints possible; Uvicorn implements that server side. Starlette provides routing, middleware, background tasks, and the request/response objects that FastAPI builds on. Pydantic isn't just documentation — it actually parses and validates data at runtime against your type hints and BaseModel classes. Interview line: Uvicorn runs it, Starlette routes it, Pydantic validates it, and FastAPI is the layer that wires the three together around your type hints.

Follow-upWhat is ASGI and how does it differ from WSGI?
Follow-upWhat does Starlette provide that FastAPI builds on top of?
Follow-upWhy can't a WSGI framework support async def natively?