Python refuses to await a Response that is already finished
Fix it fast
Most common cause, 1 of 3A synchronous HTTP client sits behind the await
Inside async code, open an httpx.AsyncClient and await its methods.
import httpx
async def fetch_profile(user_id: int) -> dict:
async with httpx.AsyncClient(timeout=10) as client:
r = await client.get(f"https://api.example.com/users/{user_id}")
r.raise_for_status()
return r.json()Short answer
TypeError: object Response can't be used in 'await' expression means the code awaited something that already returned a finished Response, not a coroutine. Usually a synchronous client (requests, httpx.Client, FastAPI's TestClient) sits behind the await, or a MagicMock replaced an async method. Python 3.14 words it 'Response' object can't be awaited. Use httpx.AsyncClient or AsyncMock.
Which cause do you have?
Open the line in the traceback that holds the await and find out what is being awaited. A real HTTP call through requests, httpx.Client or TestClient points to cause 1 or 2. A test where that call was patched or replaced with a mock points to cause 3.
python -m pytest -x --tb=long tests/ 2>&1 | grep -B8 "can't be"Then find the line you see in this list and open its fix:
- 1A synchronous HTTP client sits behind the await
The awaited call is requests.get(), requests.post() or a method of httpx.Client, outside any test - 2An async test still uses FastAPI's TestClient
The failing line is response = await client.get(...) inside an async def test, and client is a TestClient - 3A MagicMock stands in for an async method
The error appears only in tests, on a call such as mock_client.get.return_value = fake_response followed by await mock_client.get(...)
What does the error look like?
TypeError: object Response can't be used in 'await' expressionTypeError: 'Response' object can't be awaitedTypeError: object NoneType can't be used in 'await' expression
Cause 1 of 3
A synchronous HTTP client sits behind the await
You seeThe awaited call is requests.get(), requests.post() or a method of httpx.Client, outside any test
Check
The TypeError object Response can't be used in 'await' expression fires because requests and httpx.Client return a finished Response object, and only coroutines or other awaitables can follow await. HTTPX documents its default API as synchronous and keeps the awaitable methods on AsyncClient.
grep -rnE "await (requests|session|client)\.(get|post|put|delete|request)" app/Fix
Inside async code, open an httpx.AsyncClient and await its methods. If the function does not need to be async, drop the await and keep the synchronous call instead.
import httpx
async def fetch_profile(user_id: int) -> dict:
async with httpx.AsyncClient(timeout=10) as client:
r = await client.get(f"https://api.example.com/users/{user_id}")
r.raise_for_status()
return r.json()Verify
The function returns the parsed JSON and the traceback is gone.
python -c "import asyncio, app; print(asyncio.run(app.fetch_profile(1)))"Confirmed by python-httpx.org
Cause 2 of 3
An async test still uses FastAPI's TestClient
You seeThe failing line is response = await client.get(...) inside an async def test, and client is a TestClient
Check
FastAPI's TestClient is built for plain def tests: it runs the async app for you and hands back a ready Response. FastAPI's async testing guide says that trick stops working inside async test functions, so awaiting its result raises this TypeError.
grep -rn "TestClient" tests/Fix
In async tests, talk to the app through httpx.AsyncClient with an ASGITransport, as the FastAPI guide shows, and mark the test so it runs on an event loop. Keep TestClient for synchronous tests only.
import pytest
from httpx import ASGITransport, AsyncClient
from app.main import app
@pytest.mark.anyio
async def test_root():
async with AsyncClient(transport=ASGITransport(app=app), base_url="http://test") as ac:
response = await ac.get("/")
assert response.status_code == 200Verify
pytest reports the test as passed, with no TypeError about awaiting a Response.
python -m pytest tests/test_main.py -qConfirmed by fastapi.tiangolo.com
Cause 3 of 3
A MagicMock stands in for an async method
You seeThe error appears only in tests, on a call such as mock_client.get.return_value = fake_response followed by await mock_client.get(...)
Check
When a plain MagicMock replaces an async method, calling it returns the fake Response directly, and awaiting that Response raises the same TypeError. Python's mock docs describe AsyncMock as the async version of MagicMock whose calls return an awaitable; patch() picks it automatically only when the target is itself an async function.
grep -rnE "MagicMock\(|return_value" tests/Fix
Make the replaced method an AsyncMock, or build the mock with spec set to the real async class so its async methods become AsyncMock on their own.
from unittest.mock import AsyncMock, MagicMock
import httpx
fake = httpx.Response(200, json={"id": 1})
# option A: replace one method
client = MagicMock()
client.get = AsyncMock(return_value=fake)
# option B: spec the real class; its async methods become AsyncMock
client = MagicMock(spec=httpx.AsyncClient)
client.get.return_value = fakeVerify
await client.get(...) inside the test now yields the fake Response and the assertion on its JSON passes.
python -m pytest tests/ -q -k fetchConfirmed by docs.python.org
How do I stop it coming back?
- Pick one HTTP client per code path: httpx.AsyncClient in async def functions, requests or httpx.Client in plain def functions.
- In async tests, create mocks with spec= the real class or with AsyncMock, never a bare MagicMock for an awaited method.
- Keep outbound HTTP in one small async module that owns the AsyncClient, so tests have a single place to mock and production code a single client to await.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: stegnor.com, Menulo, ShopGo.