Skip to content

AsyncClient raises a bare RuntimeError when middleware returns a sync-stream response #176

Closed

Description

@lesnik512

A middleware that short-circuits on an AsyncClient and returns a response built from a sync iterator makes the client raise a raw RuntimeError when it reads the body before decoding:

import asyncio

import httpx2
import httpware


async def short_circuit(request: httpx2.Request, next: object) -> httpx2.Response:
    return httpx2.Response(200, content=iter([b'{"a": 1}']), request=request)


async def main() -> None:
    async with httpware.AsyncClient(base_url="http://x", middleware=[short_circuit]) as client:
        await client.get("/", response_model=dict)


asyncio.run(main())
# RuntimeError: Attempted to call an async iterator on a sync stream.

The error comes from await response.aread() in AsyncClient before decoding. It is a programming error in the middleware, but it surfaces as a bare RuntimeError from inside httpx2 that doesn't point at the middleware. Options: raise a TypeError that names the problem, or accept a sync stream by reading it with read().

Activity

  1. added
    bugSomething isn't working
    needs-triageMaintainer needs to evaluate this issue
    on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingneeds-triageMaintainer needs to evaluate this issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions