Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

HTTPX · Function

stream

Streams an asynchronous HTTP response instead of loading its entire body into memory at once. Use it as an async context block and consume the response with HTTPX async iterators.

How it works

AsyncClient.stream() opens an asynchronous context block that performs the request without eagerly loading the response body. Consume the body with an async iterator such as aiter_bytes(), then let the block close the response automatically.

Syntax

AsyncClient.stream(method, url, *, params=None, content=None, data=None, files=None, json=None, headers=None, cookies=None, auth=None, proxy=None, timeout=Timeout(timeout=5.0), follow_redirects=False, verify=True, trust_env=True)

Parameters

NameTypeDescription
methodstrHTTP method to use for the request.
urlURL | strRequest URL.
paramsQueryParamTypes | NoneQuery parameters.
contentRequestContent | NoneRequest body content.
dataRequestData | NoneForm data.
filesRequestFiles | NoneFiles to upload.
jsonAny | NoneJSON request body.
headersHeaderTypes | NoneRequest headers.
cookiesCookieTypes | NoneRequest cookies.
authAuthTypes | UseClientDefaultAuthentication configuration.
proxyProxyTypes | NoneProxy configuration.
timeoutTimeoutTypes | UseClientDefaultRequest timeout.

Returns

An asynchronous context manager yielding an httpx.Response whose body can be consumed incrementally.

Key points

  • Call AsyncClient.stream() with the HTTP method, target URL, and optional request arguments.
  • Use aiter_bytes() to asynchronously iterate through response content as bytes.
  • Use aiter_text() when the consumer expects streamed text rather than byte chunks.
  • Use aiter_lines() to process a streamed response one text line at a time.
  • Use aiter_raw() when content decoding must be bypassed.
  • aread() provides conditional full-response reading while remaining inside the streaming block.

Common mistakes

  • Do not normally call aclose() when using the stream context block; it closes the response on exit.
  • If a context block cannot be used, manual streaming requires explicit response lifecycle management.

Example: Stream an asynchronous response

import httpx

async def download(url: str) -> bytes:
    async with httpx.AsyncClient() as client:
        async with client.stream("GET", url) as response:
            response.raise_for_status()
            parts = [chunk async for chunk in response.aiter_bytes()]
            return b"".join(parts)

Streams the response incrementally as bytes and automatically closes it when the context block exits.

When to use it

Large downloads

Download large files or response bodies incrementally without eagerly materializing the entire payload.

Line-oriented processing

Process newline-delimited or other text output as it arrives.

Raw byte handling

Handle raw byte streams where HTTPX content decoding should be avoided.

Interview questions

QHow does asynchronous streaming work in HTTPX?

AsyncClient.stream() is an asynchronous context block for making a request while leaving the response body streamed rather than eagerly consumed. The response should be consumed with async methods such as aiter_bytes().

Follow-up: How would you stream text lines instead of bytes? Use response.aiter_lines() inside the same asynchronous context block.

QWhy should AsyncClient.stream() be used as a context manager?

The context block owns the response lifecycle and closes it automatically when the block exits. This prevents a streamed response from remaining open after consumption.

Follow-up: What can replace the context block when it is impractical? Send a Request with client.send(..., stream=True), then manage closure explicitly.

QWhich methods can consume an asynchronously streamed response?

HTTPX provides several async response iterators: aiter_bytes() for decoded response bytes, aiter_text() for text, aiter_lines() for text lines, and aiter_raw() for raw bytes without content decoding. aread() conditionally reads a response inside the stream block.

Follow-up: When would aiter_raw() be preferable? Use it when the consumer must receive the raw network bytes without HTTPX content decoding.

  • httpx.AsyncClient
  • Response.aiter_bytes
  • Response.aiter_text
  • Response.aiter_lines
  • Response.aiter_raw
  • Response.aread
  • Response.aclose
  • AsyncClient.send

Source: Developer Interface - HTTPX. Checked 2026-10-15.