Introducing a new way to review your PRs.

Read the announcement

aio-libs / aiobotocore

asyncio support for botocore library using aiohttp

View on GitHub

18 documents · Updated Nov 20, 2026

docs / aws-requests-and-responses

AWS requests and responses

AWS requests and responses form the async boundary between botocore's service logic and the network transport. The boundary keeps botocore request shape and error meaning while allowing response data to arrive through async reads, streams, and event streams. Read this page with Request execution, Endpoint and HTTP transport, and Protocol parsing and checksums for the wider request path.

Request and response boundary

AWS request objects

An AWS request object carries the method, URL, headers, body, and request context that botocore creates for a service operation. The endpoint marks requests that need a response stream before it passes the prepared request to the selected async HTTP session. This lets signing, retries, hooks, and transport code keep the botocore request contract while the send step remains awaitable.

The request context also carries operation details and, when needed, checksum or timeout choices. A request with a streaming input must be safe to send again after a retry, so the request path can reset the body before it creates a new signed request.

References: aiobotocore/endpoint.py• L95-112 aiobotocore/endpoint.py• L114-139 aiobotocore/httpsession.py• L246-312 aiobotocore/httpxsession.py• L408-499

AWS response wrappers

An AWS response wrapper presents transport data in the shape that botocore expects. It exposes the final URL, status code, headers, and raw response while changing body access from sync reads to awaitable reads. The content property reads the body once and keeps the result for later users; text decodes that cached content from the response headers or UTF-8.

The wrapper is the hand-off point between the HTTP session and the endpoint. The endpoint can therefore inspect status and headers without knowing whether the response came from aiohttp or httpx.

References: aiobotocore/awsrequest.py• L5-31 aiobotocore/awsrequest.py• L33-41 aiobotocore/httpsession.py• L314-333 aiobotocore/httpxsession.py• L502-520

Response body handling

Buffered response bodies

Buffered bodies are used when the operation has no stream output and when an HTTP error must be parsed. The endpoint waits for the full body, then gives headers, status, body, and operation context to the protocol parser. This creates one complete input for normal result parsing and for modeled error parsing.

The body remains available through the async response wrapper for hooks such as checksum checks. A response with a status of 300 or higher is read before parsing even when the operation normally returns a stream, because the error payload carries the service error details.

References: aiobotocore/endpoint.py• L30-66 aiobotocore/endpoint.py• L183-251 aiobotocore/awsrequest.py• L8-26

Streaming response bodies

Streaming bodies let a caller consume a large successful result without first loading it into memory. They provide one async contract for chunk reads, line reads, reads into a byte buffer, progress through tell(), and async cleanup. They also check the declared content length when the stream reaches its end.

The body supports async for and async with. Its sync iteration and sync context-manager forms are blocked so a caller does not accidentally use a blocking botocore API on an async stream. The aiohttp and httpx backends use the same public behavior even though their raw response APIs differ.

References: aiobotocore/response.py• L36-129 aiobotocore/response.py• L131-185 aiobotocore/response.py• L187-285 tests/test_response.py• L49-251 tests/test_response.py• L279-431

Event stream bodies

Event stream output is a separate body mode from ordinary byte streaming. The endpoint passes the raw response to the async event-stream parser, which consumes the initial response and returns an async event stream for later records. This keeps the initial operation result and the later event flow connected without treating event records as one buffered body.

The operation model selects this mode. Protocol parsing owns the event shape and the conversion of each event, while the response boundary supplies the raw async source.

References: aiobotocore/endpoint.py• L45-65 aiobotocore/parsers.py• L73-76 aiobotocore/parsers.py• L121-147 aiobotocore/parsers.py• L154-191

Transport parity

Backend response parity

The aiohttp and httpx transports must produce the same botocore-facing result. Both keep response headers and status in the AWS response wrapper, preload non-streaming results, and leave streaming results open for the caller.

The httpx body adapter adds a small read buffer because httpx yields transport-sized chunks rather than accepting an arbitrary byte count. It reads raw wire bytes so content-length and checksum checks use the bytes described by the HTTP response. These backend differences stay below the shared async body interface.

References: aiobotocore/httpsession.py• L266-333 aiobotocore/httpxsession.py• L433-520 aiobotocore/response.py• L187-285 tests/test_response.py• L422-471

Transport error translation

The response boundary turns backend-specific read failures into botocore-compatible async errors. A read timeout becomes AioReadTimeoutError, while a broken connection during body consumption becomes ResponseStreamingError. Connection, proxy, TLS, and request timeout failures during send use botocore's transport error types before the response exists.

This separation gives callers one error model across the supported transports. It also preserves cancellation instead of treating task cancellation as a network failure.

References: aiobotocore/response.py• L15-33 aiobotocore/response.py• L137-176 aiobotocore/response.py• L204-259 aiobotocore/httpsession.py• L334-366 aiobotocore/httpxsession.py• L522-560

Request body adaptation

Async request bodies

Request body helpers adapt botocore's body forms to async transport input. Byte data can pass directly, while file-like or iterable bodies are exposed as async input so sending them does not block the event loop. The transport keeps the body framing and request headers that botocore selected.

For checksum trailers, the async wrapper reads the source as needed, adds AWS chunk framing, and emits the final checksum chunk. This preserves botocore's checksum contract while allowing the HTTP session to send the body as a stream.

References: aiobotocore/httpsession.py• L266-312 aiobotocore/httpxsession.py• L453-500 aiobotocore/httpchecksum.py• L19-70 aiobotocore/httpchecksum.py• L207-280

Response checksum bodies

Response checksum handling attaches validation to the same body mode that the operation uses. A buffered response is checked after it is read. A streaming response is wrapped so each read updates the checksum and the result is checked when the stream ends.

The selected algorithm is recorded in response context when validation runs. If the service returns no usable checksum header, the body remains available without a checksum wrapper and the system records a debug message.

References: aiobotocore/httpchecksum.py• L73-109 aiobotocore/httpchecksum.py• L112-183 aiobotocore/httpchecksum.py• L191-204 aiobotocore/endpoint.py• L212-220

Request and response lifecycle

The lifecycle below shows how a botocore request becomes an async transport call and how its response becomes either parsed data or a caller-owned stream.

sequenceDiagram
    participant Op as Service operation
    participant Ep as Async endpoint
    participant Tr as HTTP session
    participant Wr as AWS response wrapper
    participant P as Protocol parser
    participant C as Caller

    Op->>Ep: Provide operation request
    Ep->>Ep: Prepare, sign, and mark body mode
    Ep->>Tr: Send prepared request
    Tr-->>Wr: Return status, headers, and raw body
    alt Error or buffered output
        Wr-->>Ep: Await complete body
        Ep->>P: Parse response body
        P-->>C: Return parsed result or service error
    else Streaming output
        Wr-->>Ep: Expose async stream
        Ep->>P: Create stream-aware parsed result
        P-->>C: Return result with caller-owned stream
        C->>Wr: Read and close asynchronously
    end

The endpoint owns the choice between buffering and streaming. The transport owns network I/O. The parser owns service shape conversion. This division lets retries and response hooks work with the same response contract before the client receives the result.

References: aiobotocore/endpoint.py• L114-181 aiobotocore/endpoint.py• L183-251 aiobotocore/response.py• L297-317

Change checklist

When these files or sections are changed, remember to consider these:

  • If response body selection changes, keep the rules for errors, ordinary streaming output, event streams, and buffered output aligned in endpoint.py and the protocol parsers.
  • If the async body interface changes, update both concrete backends and preserve the public StreamingBody and HttpxStreamingBody aliases.
  • If transport exceptions change, keep aiohttp and httpx mapped to the same botocore error classes for send-time and read-time failures.
  • If request framing or checksum rules change, check the body wrapper, transfer headers, content length, and both HTTP sessions together.
  • If response checksum handling changes, preserve validation for both buffered and streaming bodies and keep wire-byte counts consistent with Content-Length.

Important failure modes

TriggerConsequenceGuard
A caller uses sync iteration or a sync context manager on a response streamThe stream raises TypeError instead of blocking the event loopUse async for and async with
The stream ends before its declared content lengthParsing or the final read raises IncompleteReadErrorTreat the response as incomplete and do not use partial data as a complete result
A read times out or the peer breaks the connection during streamingThe caller receives an async read timeout or ResponseStreamingErrorHandle the botocore-compatible error and decide whether the operation can be retried
A successful streaming response is not closedThe underlying HTTP connection can remain in useRead it to completion or call aclose() in an async context manager
A response checksum does not match the bodyThe response raises FlexibleChecksumErrorDo not trust the body; investigate the service or transport path
A retry reuses a consumed request bodyThe next signed request can send partial or empty inputReset the request stream before creating the retry request

Key files

FileWhy to read it
aiobotocore/awsrequest.pyDefines the awaitable AWS response boundary and body caching behavior.
aiobotocore/endpoint.pyShows how operation models select buffered, streaming, or event-stream response handling.
aiobotocore/response.pyDefines the shared async body contract and backend-specific response stream behavior.
aiobotocore/httpsession.pyShows how aiohttp requests become AWS response wrappers and how send-time errors map to botocore.
aiobotocore/httpxsession.pyShows the equivalent httpx request and response path.
aiobotocore/httpchecksum.pyExplains request trailer framing and response checksum validation at a high level.
aiobotocore/parsers.pyShows how buffered bodies and raw event streams become service results.