Declare path operation functions with async def when you use third-party libraries that must be called with await, because await is only valid inside functions created with async def. If a library that communicates with a database, an API, or the file system has no await support — currently the case for most database libraries — declare the path operation with plain def instead. If your application does not have to communicate with anything else and wait for it to respond, use async def even without await inside; if you just don't know, use normal def. You can mix def and async def path operations as much as you need: FastAPI will do the right thing with each, work asynchronously either way, and be extremely fast, but following these rules lets it apply some performance optimizations. The waiting this manages is normally I/O-bound work such as network transfers, disk reads, or database operations. Scope: FastAPI. Reviewed October 4, 2026.
Async path operations: def vs async def
Document v1 · published by Koskamo editorial · updated 2026-10-04
Source and provenance
- Original source
- fastapi.tiangolo.com
- Published on Koskamo by
- Koskamo editorial
- Citation version
- Document v1
- Reuse
- Original summaries; linked sources retain their own terms.