Large downloads
Download large files or response bodies incrementally without eagerly materializing the entire payload.
HTTPX · Function
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.
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.
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)| Name | Type | Description |
|---|---|---|
method | str | HTTP method to use for the request. |
url | URL | str | Request URL. |
params | QueryParamTypes | None | Query parameters. |
content | RequestContent | None | Request body content. |
data | RequestData | None | Form data. |
files | RequestFiles | None | Files to upload. |
json | Any | None | JSON request body. |
headers | HeaderTypes | None | Request headers. |
cookies | CookieTypes | None | Request cookies. |
auth | AuthTypes | UseClientDefault | Authentication configuration. |
proxy | ProxyTypes | None | Proxy configuration. |
timeout | TimeoutTypes | UseClientDefault | Request timeout. |
An asynchronous context manager yielding an httpx.Response whose body can be consumed incrementally.
AsyncClient.stream() with the HTTP method, target URL, and optional request arguments.aiter_bytes() to asynchronously iterate through response content as bytes.aiter_text() when the consumer expects streamed text rather than byte chunks.aiter_lines() to process a streamed response one text line at a time.aiter_raw() when content decoding must be bypassed.aread() provides conditional full-response reading while remaining inside the streaming block.aclose() when using the stream context block; it closes the response on exit.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.
Download large files or response bodies incrementally without eagerly materializing the entire payload.
Process newline-delimited or other text output as it arrives.
Handle raw byte streams where HTTPX content decoding should be avoided.
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.
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.
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.AsyncClientResponse.aiter_bytesResponse.aiter_textResponse.aiter_linesResponse.aiter_rawResponse.areadResponse.acloseAsyncClient.send