Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Python refuses to await a Response that is already finished

Your error

TypeError: object Response can't be used in 'await' expression2 more lines it can show

Fix it fast

Most common cause, 1 of 3

A synchronous HTTP client sits behind the await

Inside async code, open an httpx.AsyncClient and await its methods.

Fix
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()
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to Python 3.14.6 and 3.12.13 (both message forms), httpx 0.28.1, FastAPI 0.165.0
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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.

Run this first
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:

  1. 1A synchronous HTTP client sits behind the awaitThe awaited call is requests.get(), requests.post() or a method of httpx.Client, outside any test
  2. 2An async test still uses FastAPI's TestClientThe failing line is response = await client.get(...) inside an async def test, and client is a TestClient
  3. 3A MagicMock stands in for an async methodThe 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' expression
  • TypeError: 'Response' object can't be awaited
  • TypeError: 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.

Check
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.

Fix
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.

Verify
python -c "import asyncio, app; print(asyncio.run(app.fetch_profile(1)))"

Confirmed by python-httpx.org

Did this fix it?

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.

Check
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.

Fix
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 == 200

Verify

pytest reports the test as passed, with no TypeError about awaiting a Response.

Verify
python -m pytest tests/test_main.py -q

Confirmed by fastapi.tiangolo.com

Did this fix it?

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.

Check
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.

Fix
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 = fake

Verify

await client.get(...) inside the test now yields the fake Response and the assertion on its JSON passes.

Verify
python -m pytest tests/ -q -k fetch

Confirmed by docs.python.org

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.