Full answer
FastAPI is a framework for HTTP APIs. You declare an endpoint as a Python function, and the parameter types tell FastAPI where to read data from and how to validate it.
Its building blocks:
- Starlette provides routing, requests and responses, middleware, WebSockets and background tasks. It speaks ASGI (Asynchronous Server Gateway Interface), the async successor to WSGI.
- Pydantic (version 2, with a core written in Rust) parses and validates data from type hints and serialises responses.
- An ASGI server such as Uvicorn runs the app.
Why "fast":
- Runtime. On an async server, one process can handle many requests that are waiting on I/O (database, other APIs) at the same time. Pydantic v2's Rust core makes validation and serialisation cheap. FastAPI is among the quicker Python frameworks, though real speed depends mostly on your database and I/O patterns.
- Development. Type hints drive request parsing, validation errors, editor autocomplete and an OpenAPI schema. The
/docspage (Swagger UI) and/redoccome for free, which helps front-end and mobile teams start early.
What FastAPI does not include: an ORM, an admin panel, migrations or a template-first web stack. You pick libraries, commonly SQLAlchemy or SQLModel with Alembic for databases.
When to choose it: JSON APIs, microservices, ML model serving, and services with lots of concurrent I/O. When Django might fit better: content-heavy sites or back-office apps that benefit from the admin, ORM and auth out of the box.
from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import BaseModel
app = FastAPI(title="Canteen API")
class MenuItem(BaseModel):
name: str
price: float
veg: bool = True
@app.get("/menu/{item_id}")
def read_item(item_id: int) -> MenuItem:
return MenuItem(name="Masala Dosa", price=60.0)
client = TestClient(app)
print(client.get("/menu/1").json())
print(client.get("/menu/abc").status_code) # validated from the type hint
print(sorted(client.get("/openapi.json").json()["paths"])) # schema generated automatically
Standalone script. Run it with `python file.py`; it calls the app through TestClient, so no server is needed. Output (ran twice with identical results; Python 3.13.5, FastAPI 0.165.1, Pydantic 2.14.4, Starlette 1.10.0, SQLAlchemy 2.1.9, httpx 0.29.0): {'name': 'Masala Dosa', 'price': 60.0, 'veg': True} 422 ['/menu/{item_id}']
Key points
- Built on Starlette (ASGI) and Pydantic
- Type hints drive parsing, validation and docs
- Async server handles many I/O-bound requests concurrently
- OpenAPI docs at /docs and /redoc automatically
- No built-in ORM or admin; you choose libraries
Common mistakes
- Claiming FastAPI is fast for every workload, including CPU-heavy ones
- Thinking FastAPI includes an ORM or admin like Django
- Using async def with blocking libraries and losing the benefit
- Ignoring the generated OpenAPI schema as a contract for clients
Follow-ups
- What is ASGI and how is it different from WSGI?
- What role does Pydantic play in a request?
- When would you choose Django over FastAPI?
Tips
- Explain both meanings of fast
- Name Starlette and Pydantic and what each does
- Be honest that database and I/O design matter more than framework speed