Fast Python, without compromise
Python web frameworks in 2018 fall between two poles. On one side, Django and Flask — mature, with broad ecosystems, but built on WSGI, a synchronous interface that handles one request at a time per worker. On the other, asynchronous frameworks like Tornado and Sanic that offer superior performance but require more boilerplate code and provide less structure. Neither pole fully leverages one of the most significant additions to the language: type hints, introduced with Python 3.5.
FastAPI, created by Sebastián Ramírez, is designed from the ground up around Python type hints, using them not as mere documentation but as an operational mechanism for validation, serialisation and automatic documentation generation.
Type hints as a contract
In FastAPI, type hints define the API contract. A function that declares a parameter item: Item — where Item is a Pydantic model — automatically gets: input data validation (with structured errors for non-conforming data), response serialisation, documentation in the OpenAPI interface. The developer writes the type once and the framework extracts three capabilities from it.
Pydantic handles runtime validation: it converts incoming data to the declared types, verifies constraints (length, range, pattern) and returns detailed errors in JSON format. The result is that errors which in other frameworks would surface in production are caught on the first request.
Starlette and native async
FastAPI is built on Starlette, a high-performance ASGI (Asynchronous Server Gateway Interface) framework. ASGI is the asynchronous successor to WSGI: it supports async/await natively, allowing a single process to handle thousands of concurrent connections without threads. I/O operations — HTTP calls, database queries, file reads — do not block the process.
The resulting performance is comparable to frameworks written in Go and Node.js, according to available benchmarks, while retaining the productivity and readability of Python code.
Automatic documentation
FastAPI automatically generates interactive documentation in two formats: Swagger UI and ReDoc, both derived from the OpenAPI specification produced by analysing type hints. Documentation is always in sync with the code, because it is a direct derivation of it. The framework is released under the MIT licence.
Link: fastapi.tiangolo.com
