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.

  1. Most asked When should an endpoint be `async def` versus plain `def` in FastAPI?

    Use async def when the endpoint only awaits async I/O (async DB drivers, httpx). Use plain def when it calls blocking libraries, because FastAPI runs def endpoints in a threadpool so they don't…

  2. Most asked What is ASGI and how does it differ from WSGI?

    WSGI is the synchronous Python interface between servers and apps (one request per call). ASGI is its async successor, supporting async request handling, WebSockets, HTTP/2 and long-lived connections.

  3. Most asked What are `BackgroundTasks` and when should you use a real task queue instead?

    BackgroundTasks runs small functions after the response is sent, in the same process; for heavy, slow, retryable or critical jobs, use a queue like Celery, RQ, Dramatiq, arq or a managed queue.

  4. How do you cache responses or data in FastAPI?

    Cache data in Redis (via redis.asyncio) with TTLs using a cache-aside pattern inside services or dependencies, and use HTTP caching headers (Cache-Control, ETag) for client and CDN caching.

  5. How do you stream large responses or handle file uploads in FastAPI?

    Use StreamingResponse with a (async) generator for large or incremental output, FileResponse for files on disk, and UploadFile (spooled temp file) for uploads, reading in chunks.

  6. How do you reject unknown fields or disable type coercion in request models?

    Set model_config = ConfigDict(extra='forbid') to reject unexpected fields, and strict=True (model-wide or per field via Field(strict=True)/StrictInt) to disable lax coercion like '5' → 5.

  7. What is SQLModel and when would you choose it?

    SQLModel (by FastAPI's author) combines SQLAlchemy and Pydantic so one class can be both a table model and a schema, reducing duplication in simple CRUD apps.

  8. How does FastAPI compare with Litestar and Django Ninja?

    All three are type-hint-driven API frameworks. Litestar is an ASGI framework with built-in DTOs, its own DI and broader batteries; Django Ninja brings FastAPI-style APIs to Django's ORM and admin.

  9. Which security settings should a production FastAPI app have?

    Disable or protect the docs, use HTTPS (HSTS at the proxy), TrustedHostMiddleware, strict CORS, security headers, request body size limits at the proxy, rate limits, input validation, and no debug…

  10. Can FastAPI run on AWS Lambda or other serverless platforms?

    Yes. Adapters like Mangum translate API Gateway/Lambda events to ASGI. Container-based serverless (Cloud Run, App Runner, Fargate) runs Uvicorn directly.

  11. What is the difference between `include_router` and `app.mount`?

    include_router merges an APIRouter's routes into the same app (shared middleware, exception handlers and OpenAPI); app.mount attaches a completely separate ASGI app at a path prefix with its own…

  12. When would you write a custom `APIRoute` class?

    To intercept request/response handling for every route in a router, for example to log raw request bodies, decompress or decrypt payloads, or customize error handling, with access to the parsed…

  13. Should a FastAPI API expose auto-increment IDs or UUIDs?

    UUIDs (or ULIDs/UUIDv7) avoid leaking record counts and enumeration attacks and can be generated client-side, while auto-increment integers are compact and index-friendly. Many systems use an…

Was this page helpful?

Report a mistake