All concepts

FastAPI

Type-hinted async endpoints with auto validation and docs—the default for ML APIs.

Python · Intermediate · ~5 min

In plain English

A web framework where the function signature IS the API contract — types give you validation, docs and editor support for free.

Why it's worth your time

It's the default way to put a Python model behind an HTTP endpoint, and it's async-native, which matters for LLM calls.

If you remember three things

  • Pydantic models define request and response shapes
  • Interactive docs generated from the code
  • async def endpoints for IO-bound work

Overview

FastAPI is an async Python web framework where you declare endpoints ('path operations') as type-hinted functions. From the hints it validates and parses requests via Pydantic and auto-generates OpenAPI/Swagger docs. Depends() provides dependency injection, and it runs on ASGI/uvicorn for concurrent I/O—making it the default choice for serving ML models.

In an interview

FastAPI builds a web API from type hints. You write a path operation—an async def decorated with @app.post—and annotate its parameters, using a Pydantic model for the body. FastAPI then validates every request automatically, returns a 422 on bad input, and generates interactive OpenAPI docs for free. Depends() injects shared resources like a DB session or a loaded model, and because it's ASGI-based on uvicorn it handles I/O concurrently. That combination of speed, typing, and free docs is why it's the go-to for ML model serving.

Production defaults

Endpoints
async def for anything calling a model, a database or an HTTP service
Blocking work
run_in_executor or a background worker. A blocking call in an async endpoint stalls the whole loop
Response models
declare them — they validate output and filter fields you didn't mean to return
Long jobs
return a job id and poll or stream. Don't hold an HTTP connection open for a 60-second generation

What breaks

  • Throughput collapses under load — A blocking call inside an async endpoint. One blocked coroutine freezes every other request.
  • Timeouts on model calls — Stream the response or move to a job queue. Synchronous long generations don't survive real proxies.

Watch it explained

Learn FastAPI in 3 Minutes | Python FastAPI Tutorial For Beginners (Hands-on Tutorials) — LimeGuru, 3:53

Related