FastAPI
A modern, fast Python web framework for building APIs. FastAPI uses Python type hints for automatic validation, serialization, and OpenAPI docs. Async support, great developer experience, and excellent performance. The best choice for new Python APIs.
- New Python APIs.
- ML model serving.
- Typed, documented APIs.
- Full web apps with templates (use Django).
- CPU-bound services (use Go/Rust).
Definition
FastAPI is a Python web framework built on Starlette and Pydantic. Type-annotated endpoints get automatic request validation, response serialization, and interactive docs (Swagger/ReDoc). Async/await for high concurrency. Dependency injection for shared logic.
Why it exists
Flask is simple but untyped and slow. Django is heavy. FastAPI gives modern, typed, async, fast APIs with automatic docs — the best of both worlds for API-only backends.
How it works
Define Pydantic models for request/response. Decorate functions with @app.get('/path'). Type hints validate and serialize automatically. async def for async endpoints. Dependencies inject shared logic. uvicorn runs the server. Auto docs at /docs.
Scaling limits
Python GIL limits CPU parallelism (use async for I/O). Single-process; scale with workers (gunicorn -w N). Pydantic v2 is fast but v1 was slow. Not as fast as Go/Rust for raw throughput.
Cost shape
Free and open source.
Alternatives
- Django — Full framework (admin, ORM, auth) but heavier.
- Flask — Simpler, less built-in (no auto validation/docs).
Failure modes
- GIL limiting CPU work.
- Blocking calls in async endpoints.
- Pydantic v1/v2 migration issues.