async def vs def in FastAPI
Answer: You can mix def and async def in your path operation functions as much as you need and define each one using the best option for you. In any of the cases, FastAPI will still work asynchronously and be extremely fast. (S1)
The rules
- You can only use await inside of functions created with async def. (S1)
- If your application doesn't have to communicate with anything else and wait for it to respond, use async def, even if you don't need to use await inside. (S1)
- Modern versions of Python have support for "asynchronous code" using something called "coroutines", with async and await syntax. (S1)
- With async def, Python knows that, inside that function, it has to be aware of await expressions. (S1)
Related
- FastAPI generates a "schema" with all your API using the OpenAPI standard for defining APIs. (S2)
- This schema definition includes your API paths, the possible parameters they take, etc. (S2)
Claims
- id: C1 claim: await only works inside async def. source: S1 evidence: "You can only use await inside of functions created with async def."
- id: C2 claim: def and async def can be mixed. source: S1 evidence: "declare your path operation functions with async def like"
- id: C3 claim: Python uses coroutines for async code. source: S1 evidence: "with async and await syntax."
- id: C4 claim: FastAPI generates an OpenAPI schema. source: S2 evidence: "FastAPI automatically generates a JSON (schema) with the descriptions of all your API."
Sources
- [S1] Concurrency and async / await - FastAPI. FastAPI docs. https://fastapi.tiangolo.com/async/ . License: MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE). Accessed 2026-11-04.
- [S2] First Steps - FastAPI. FastAPI docs. https://fastapi.tiangolo.com/tutorial/first-steps/ . License: MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE). Accessed 2026-11-04.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted).
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: FastAPI docs, Concurrency and async / await - FastAPI, MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE); FastAPI docs, First Steps - FastAPI, MIT (fastapi/fastapi LICENSE; Copyright (c) 2018 Sebastián Ramírez; https://github.com/fastapi/fastapi/blob/master/LICENSE).
Cite this
iinventedlight. 2026. "async def vs def in FastAPI." iinventedlight Library, iil:note:hznwdxfr, version of 2026-11-04. https://iinventedlight.com/library/notes/fastapi-async-await/. License: CC-BY-4.0.
@misc{iil_note_hznwdxfr,
author = {{iinventedlight}},
title = {{async def vs def in FastAPI}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-04},
url = {https://iinventedlight.com/library/notes/fastapi-async-await/},
note = {Permanent ID iil:note:hznwdxfr. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.
For language models and agents: start at /llms.txt. This page in Markdown: /library/notes/fastapi-async-await.md.