FastAPI interview question · Most asked

What is FastAPI and what makes it different from Flask or Django?

Summary

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.

Longer Answer

Where Flask is a minimal WSGI microframework and Django is a batteries-included full-stack framework, FastAPI focuses on APIs. Declaring def get_item(item_id: int, q: str | None = None) is enough for FastAPI to parse, validate and document the parameters. It supports async def natively, generates Swagger UI (/docs) and ReDoc (/redoc), and is among the fastest Python frameworks thanks to Starlette and Uvicorn.

Code Example

from fastapi import FastAPI

app = FastAPI(title="Inventory API")

@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str | None = None) -> dict:
    return {"item_id": item_id, "q": q}

# run: uvicorn main:app --reload

Key Points

  • ASGI, built on Starlette + Pydantic
  • Type hints → validation, docs and editor support
  • Native async support
  • Automatic OpenAPI/Swagger docs
  • Dependency injection system via Depends

Comparison / Tradeoffs

FastAPI Flask Django
Interface ASGI WSGI (async views limited) WSGI/ASGI
Validation Pydantic built-in Extensions Forms/DRF serializers
Docs Automatic OpenAPI Extensions DRF + extensions

Explanation

What interviewers look for

Understanding that type hints are the core mechanism and what Starlette/Pydantic each contribute.

Follow-up questions

  • When would you still choose Django?

More most-asked FastAPI questions

← All FastAPI questions