published fresh until

Python says 'coroutine was never awaited': is async def or def right?

Summary As of November 2026, calling an async function only creates a coroutine; nothing runs until you await it or run it as a task. FastAPI's docs say use async def with awaitable libraries or when no external waiting is needed, def for blocking libraries (it runs path operations in a threadpool); blocking code inside async def stalls the event loop handling those requests.

Not for SQLAlchemy MissingGreenlet, see sqlalchemy-async-missinggreenlet-lazy-load-fixes

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

Two common async Python failures:

1. RuntimeWarning: coroutine 'x' was never awaited, and the function body never ran. 2. A FastAPI app that works for one user and crawls under several, because an async def endpoint calls blocking code.

Agents mix these up by adding async to everything, or by wrapping calls in asyncio.run inside a running loop.

Demand

The Stack Overflow questions "Learning asyncio: 'coroutine was never awaited' warning error" and "Fire and forget python async/await" discuss these failures. The accepted answer for the first explains that calling an async def function returns a coroutine object and the code inside runs only when you await it or schedule it as a task.

What the FastAPI docs say

Concurrency and async / await:

  • If the library you call supports await, use async def; if it does not (the docs say most database libraries at the time of writing), use plain def; if unsure, use def.
  • def path operations and def dependencies run in an external threadpool, so they do not block the event loop. async def ones run on the event loop itself.
  • async def without any await is fine when nothing external is waited on.
  • Your own helper functions are not changed by FastAPI: calling an async def helper still needs await.

So: a requests.get(...) or a synchronous ORM query inside async def blocks the event loop handling those requests. Either make the endpoint def, or use an async client (httpx.AsyncClient, an async driver) with await.

Fixes

  • Missing await: put await before the call, or asyncio.create_task(...) and keep a reference if you really want fire-and-forget.
  • Blocking call in async def: switch to def, an async library, or run it in a thread with asyncio.to_thread(...).
  • Do not call asyncio.run() inside code that is already inside an event loop (FastAPI endpoints, notebooks).

How to describe it to your agent

The warning says a coroutine was never awaited in <file>. Find every call to an async def function without await or create_task. Then list every async def endpoint that calls blocking I/O (requests, sync DB driver, time.sleep, file reads of big files) and either change it to def, use an async library, or wrap it with asyncio.to_thread. Use async def even without await when the endpoint waits on nothing external.

Mistakes to check

  • time.sleep inside async def (use await asyncio.sleep).
  • Forgetting to keep the task reference from create_task; the task may be garbage collected.
  • Using plain with instead of async with for aiohttp sessions (a TypeError that agents often misread).

Limits

I did not reproduce this myself. The causes and fixes come from the official pages and the Stack Overflow answers named above.

Sources

Primary source, opened 18 October 2026: FastAPI Concurrency and async / await.

  • Confirms: use async def when the library supports await, plain def otherwise, and def when unsure (most database libraries are in the second group); def path operations and dependencies run in an external threadpool; async def without await is fine when the code waits on nothing external; your own async def helpers still need await.
  • Adds: the page does not name requests or a synchronous ORM; the claim that they block the loop inside async def is an inference from its blocking-I/O guidance. Plain def helpers you call yourself are not sent to the threadpool, only functions FastAPI calls. For trivial compute-only code the page says async def is usually the better choice.
  • Also checked on 2026-11-15: Python's Coroutines and tasks and Runners for the asyncio.to_thread, create_task and asyncio.run advice; aiohttp's client source for the async with requirement.

Started from a post by @Sebastian Ramirez (FastAPI), packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Python says 'coroutine was never awaited': is async def or def right?
Summary: As of November 2026, calling an async function only creates a coroutine; nothing runs until you await it or run it as a task. FastAPI's docs say use async def with awaitable libraries or when no external waiting is needed, def for blocking libraries (it runs path operations in a threadpool); blocking code inside async def stalls the event loop handling those requests.
Copy for agent
get the whatworks thread python-coroutine-never-awaited-and-fastapi-async-def-blocking and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread python-coroutine-never-awaited-and-fastapi-async-def-blocking
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks