Skip to main content

ADR-003: FastAPI as Web Framework

Status

Accepted

Context

The original Heka platform was built on Flask with Gunicorn, which led to several limitations:
  • Synchronous by default: Poor performance for I/O heavy operations
  • No native async support: Difficult to implement websockets, SSE
  • Manual API documentation: OpenAPI specs maintained separately from code
  • Limited type safety: No automatic request/response validation
  • Concurrency issues: Problems with worker processes and shared state
For Tasteful, we needed a modern web framework that could address these issues while maintaining developer productivity.

Decision

We selected FastAPI as the foundation web framework for Tasteful.

Key Advantages

  1. Native Async Support: Built on ASGI (Starlette) for true asynchronous processing
  2. Automatic API Documentation: OpenAPI/Swagger docs generated from code
  3. Type Safety: Pydantic integration for request/response validation
  4. Performance: Among the fastest Python web frameworks
  5. Modern Standards: Built-in support for modern web standards (JSON, WebSockets, etc.)

Implementation Pattern

Integration with Tasteful Architecture

Consequences

Positive

  • Async Performance: Better handling of I/O operations and concurrent requests
  • Developer Experience: Automatic API docs, type hints, IDE support
  • Validation: Built-in request/response validation reduces bugs
  • Standards Compliance: OpenAPI 3.0, JSON Schema support out of the box
  • Future-Proof: Modern async architecture supports websockets, SSE, etc.
  • Ecosystem: Large ecosystem of FastAPI extensions and middleware

Negative

  • Learning Curve: Team needs to understand async/await patterns
  • Complexity: More complex than synchronous frameworks for simple use cases
  • Debugging: Async stack traces can be more difficult to debug
  • Dependencies: Heavier dependency footprint than minimal frameworks

Trade-offs

  • Performance vs. Simplicity: Better performance but requires async understanding
  • Type Safety vs. Flexibility: More rigid but catches errors earlier
  • Auto-documentation vs. Control: Generated docs are convenient but less customizable

Alternatives Considered

  1. Flask + Flask-RESTX: Continue with existing stack
    • Rejected: Doesn’t address async/performance issues
  2. Django REST Framework: Full-featured framework
    • Rejected: Too opinionated, heavy for our modular architecture
  3. Starlette: Direct use of ASGI framework
    • Rejected: Too low-level, would require building features FastAPI provides
  4. Tornado: Async web framework
    • Rejected: Less modern, smaller ecosystem
  5. Sanic: Fast async framework
    • Rejected: Less mature ecosystem, no automatic API documentation

Implementation Guidelines

Async by Default

Type Safety with Pydantic

Error Handling

References