What is FastAPI and what makes it stand out?

Difficulty: Beginner

Question

What is FastAPI, and what makes it stand out compared to Flask or Django for building APIs?

Answer

FastAPI is a modern Python web framework for building APIs, built on top of Starlette (the ASGI web toolkit) and Pydantic (data validation). You describe your inputs and outputs with standard Python type hints, and FastAPI uses those hints to parse and validate requests, serialize responses and generate an OpenAPI schema with interactive docs, all from the same code.

What FastAPI is made of

FastAPI is a thin, opinionated layer over two well-tested libraries:

  • Starlette handles the web part: routing, requests, responses, middleware, WebSockets, background tasks and the ASGI interface.
  • Pydantic (v2, with its validation core written in Rust) handles data: parsing JSON into typed objects, validating constraints and turning objects back into JSON.
  • An ASGI server such as Uvicorn runs the app. ASGI is the async successor to WSGI, so a single process can juggle thousands of concurrent connections while they wait on I/O.

FastAPI glues these together through function signatures. When you write def get_order(order_id: int, q: str | None = None), FastAPI reads the signature once at startup and knows order_id is a required integer path parameter and q an optional query string.

Features interviewers expect you to name

  • Validation from type hints: send "abc" where an int is expected and the client gets a 422 with a precise error location, without you writing a single if.
  • Automatic docs: Swagger UI at /docs and ReDoc at /redoc, generated from the OpenAPI schema at /openapi.json.
  • Dependency injection with Depends(), used for DB sessions, auth, pagination and settings.
  • Native async: async def endpoints run on the event loop; plain def endpoints are run in a threadpool so blocking code does not freeze the server.
  • Editor support: because everything is typed, autocomplete and type checkers (mypy, Pyright) work across your handlers.
  • Standards based: OpenAPI and JSON Schema, so you can generate typed clients for a React or Android app.

How it compares with Flask and Django

AspectFastAPIFlaskDjango (+ DRF)
InterfaceASGI, async firstWSGI (async views possible but limited)WSGI and ASGI
ValidationBuilt in via PydanticManual or extensionsDRF serializers
API docsAutomatic OpenAPIExtensionsExtensions (drf-spectacular)
ORM, admin, authBring your ownBring your ownBatteries included
Best fitJSON APIs, ML serving, microservicesSmall apps, simple servicesFull products with admin and ORM

FastAPI is not "better" in every case. Django still wins when you need an admin panel, migrations and auth on day one.

Where you see it in real systems

FastAPI is a common choice for ML model serving (wrapping a PyTorch or scikit-learn model behind /predict), internal microservices and backends for mobile apps. A typical startup stack is FastAPI + SQLAlchemy 2.0 + PostgreSQL + Redis, deployed in Docker behind Nginx or a cloud load balancer.

Common misconceptions

  • "FastAPI is fast because it is async." Raw throughput comes mostly from Starlette and Uvicorn. Your own code, especially database calls, usually dominates latency.
  • "Using async def everywhere makes it faster." Calling a blocking library inside async def blocks the whole event loop.
  • "FastAPI is a full-stack framework." It deliberately does not ship an ORM, templating-first workflow or admin.
The FastAPI stack. Each layer has one job; FastAPI ties Starlette and Pydantic together through type hints.
  1. Your path operation functions: Typed Python functions that hold business logic.
  2. FastAPI: Reads signatures, runs dependencies, builds OpenAPI.
  3. Pydantic: Validates input and serializes output.
  4. Starlette: Routing, requests, responses, middleware, WebSockets.
  5. ASGI server (Uvicorn): Speaks HTTP and drives the event loop.

Code examples

A minimal FastAPI app with validation

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI(title="Orders API")

class Order(BaseModel):
    item: str
    quantity: int
    amount_paise: int  # store money as integer paise, never float rupees

@app.get("/")
def read_root():
    return {"message": "Hello, FastAPI!"}

@app.post("/orders", status_code=201)
def create_order(order: Order):
    # order is already parsed and validated here
    return {"item": order.item, "total_paise": order.quantity * order.amount_paise}

# Run in dev:  fastapi dev main.py   (or: uvicorn main:app --reload)
# Docs:        http://127.0.0.1:8000/docs

Type hints on the Pydantic model drive validation and the docs. Posting a string for quantity returns a 422 automatically.

Running it from the command line

pip install "fastapi[standard]"   # FastAPI + Uvicorn + fastapi CLI

fastapi dev main.py               # auto-reload, for development
fastapi run main.py               # production mode, no reload

curl -X POST http://127.0.0.1:8000/orders \
  -H "Content-Type: application/json" \
  -d '{"item": "dosa", "quantity": "two", "amount_paise": 8000}'

The fastapi CLI ships with the standard extra in recent versions. The bad quantity value triggers an automatic validation error.

Key points

  • FastAPI = Starlette (web/ASGI) + Pydantic (data) + type hints as the single source of truth
  • Validation errors return 422 Unprocessable Entity with the exact field location
  • Docs live at /docs (Swagger UI) and /redoc, schema at /openapi.json
  • def handlers run in a threadpool; async def handlers run on the event loop
  • No built-in ORM or admin: pair it with SQLAlchemy or SQLModel

Common follow-up questions

  • What is the difference between WSGI and ASGI?
  • What happens if you call a blocking function inside an async def endpoint?
  • How does FastAPI generate the OpenAPI schema?
  • When would you still pick Django over FastAPI?

Interview tips

  • Name Starlette and Pydantic explicitly; it shows you know FastAPI is a composition, not magic
  • Avoid claiming it is faster than everything; say it is fast enough and the real wins are validation, docs and developer speed
  • Mention one real use case you built or would build, such as serving an ML model

Concepts covered

FastAPI, ASGI, Starlette, Pydantic, type hints, OpenAPI

More FastAPI Interview questions