- Add dependencies with
yield
, a.k.a. exit steps, context managers, cleanup, teardown, ...- This allows adding extra code after a dependency is done. It can be used, for example, to close database connections.
- Dependencies with
yield
can be normal orasync
, FastAPI will run normal dependencies in a threadpool. - They can be combined with normal dependencies.
- It's possible to have arbitrary trees/levels of dependencies with
yield
and exit steps are handled in the correct order automatically. - It works by default in Python 3.7 or above. For Python 3.6, it requires the extra backport dependencies:
async-exit-stack
async-generator
- New docs at Dependencies with
yield
. - Updated database docs SQL (Relational) Databases: Main FastAPI app.
- PR #595.
- Fix
sitemap.xml
in website. PR #598 by @samuelcolvin.