FastAPI Internals — Marcelo Trylesinski

Dive deep into FastAPI's architecture: ASGI specs, middleware processing, route matching, dependencies, WebSocket handling, and server behaviors. Learn implementation details.

Key takeaways
  • FastAPI is built on two main dependencies: Pydantic for data validation and Starlet for web-related functionality

  • The ASGI (Asynchronous Server Gateway Interface) specification defines how FastAPI communicates between server and application through three parameters:

    • Scope: Contains connection data and request information
    • Receive: Callable for getting data from server
    • Send: Callable for sending data back to server
  • Middleware processing occurs before routing in FastAPI:

    • Server error middleware handles uncaught exceptions
    • Exception middleware manages custom exception handlers
    • Custom middlewares can be added but won’t have access to endpoint information
  • Route matching follows a “first match wins” principle:

    • When multiple routes could match a request, the first registered route takes precedence
    • This applies across multiple routers with same prefixes
  • Dependencies in FastAPI:

    • Results are cached based on function hash
    • Can be async or sync
    • For non-CPU intensive tasks, async is more efficient
    • CPU-bound tasks should use sync with thread pool
  • WebSocket handling:

    • Must explicitly accept WebSocket handshake
    • Need to properly close connections to prevent memory leaks
    • Based primarily on Starlet implementation
  • Server behavior variations:

    • Different servers (Uvicorn, Hypercorn) may handle response timing differently
    • Some servers send headers immediately upon status code
    • Can lead to situations where 200 status is sent before errors occur