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 / event-streams

Event streams

Event streams let an asynchronous AWS operation return a live sequence of service events instead of one complete body. The stream turns framed network data into parsed event records that callers can consume with async for, while preserving botocore-compatible response and error behavior. This page is a child of Asynchronous service features and relates to Paginators and waiters.

Stream lifecycle

An event-stream operation has two related results: an initial response and an event stream. The initial response establishes the operation result. The stream then carries the later events from the same service call.

Operation response

The asynchronous response parser creates an AioEventStream from the operation's modeled event-stream member. It reads the required initial-response event first, parses its payload as the normal operation result, and places the still-open stream in the result under the modeled event-stream name. This lets callers receive setup data and then process live events through one response.

The relationship between the operation result and the live stream follows this path:

flowchart LR
    A[AWS streaming response] --> B[Initial response event]
    B --> C[Normal operation result]
    A --> D[Remaining event frames]
    D --> E[Async event stream]
    C --> F[Caller]
    E --> F

References: aiobotocore/parsers.py• L73-76 aiobotocore/parsers.py• L137-147 aiobotocore/parsers.py• L181-191

Event consumption

The stream owns the remaining response body while it is being consumed. Each yielded value is a parsed event mapped to the event name defined by the service model. Callers should finish consuming the stream or close it when they stop early so the underlying response can be released.

References: aiobotocore/eventstream.py• L16-20 tests/test_eventstreams.py• L61-75 tests/…/test_eventstream.py• L522-527

Binary message framing

AWS event streams carry a sequence of binary messages. Framing separates one message from the next and allows the transport to deliver data in network chunks that do not match message boundaries.

Frame boundaries

Every message declares its total length and header length in a prelude. The decoder uses those lengths to identify the headers and payload, then checks the prelude and message checksums before it exposes the message. A partial frame stays in the buffer until the next transport chunk completes it.

References: tests/…/test_eventstream.py• L285-305 tests/…/test_eventstream.py• L374-380

Header and payload values

Headers carry routing and content metadata such as the message type and event type. They support the typed values defined by the event-stream format. The payload remains bytes until the selected service protocol parser turns it into the event's modeled value.

References: tests/…/test_eventstream.py• L147-177 tests/…/test_eventstream.py• L383-401

Message decoding

After framing, the stream presents each message as a response-like record. The event-stream parser uses the service protocol and the event shape to convert that record into the dictionary returned to the caller.

Protocol payloads

The client selects an event-stream parser that matches the operation protocol. JSON, XML, and Smithy RPC v2 CBOR streams therefore use the same asynchronous stream contract while decoding their payloads according to their service format.

References: aiobotocore/parsers.py• L99-118 aiobotocore/parsers.py• L121-122 aiobotocore/parsers.py• L172-179 aiobotocore/parsers.py• L200-207

Event shape

The service model tells the parser which event members and payload fields are valid. A successfully decoded message becomes the modeled event dictionary. The stream does not add normal response metadata to each event because the event itself is the operation's unit of data.

References: aiobotocore/eventstream.py• L29-37 aiobotocore/parsers.py• L53-70 aiobotocore/parsers.py• L145-147

Asynchronous iteration

Event streams follow Python's asynchronous iteration model. Reading an event can wait for more network data, so the stream supports async for and does not provide a synchronous iterator.

Async-only contract

Callers consume events with async for event in response['EventStream']. Each iteration reads enough raw data to produce the next complete event, parses it, and yields it. A normal for loop raises NotImplementedError instead of silently blocking an asynchronous application.

References: aiobotocore/eventstream.py• L9-20 tests/test_eventstreams.py• L67-75 tests/test_eventstreams.py• L78-92

Chunk buffering

The network layer supplies arbitrary chunks rather than complete event messages. The stream adds each chunk to a message buffer and yields every complete message that the buffer can extract. One chunk can contain several events, and one event can span several chunks.

References: aiobotocore/eventstream.py• L22-27 tests/test_eventstreams.py• L13-23 tests/…/test_eventstream.py• L285-326

Error handling

Errors can occur while the client decodes the wire format or while the service reports a failure as an event. The stream raises these errors during the operation stage or during later iteration, so callers must handle both points.

Framing errors

A corrupt frame fails before it becomes an event. Checksum mismatches, invalid header lengths, and duplicate headers are examples of rejected input. These failures protect the caller from using data whose boundaries or integrity are not trusted.

References: tests/…/test_eventstream.py• L216-272 tests/…/test_eventstream.py• L329-344

Service event errors

An event with an error message type is converted to an error response instead of a normal event. The asynchronous stream raises EventStreamError with the decoded service error and operation name. This can happen during async for after earlier events have already been delivered.

References: aiobotocore/eventstream.py• L29-37 tests/…/test_eventstream.py• L179-201 tests/…/test_eventstream.py• L511-519

Missing initial response

Modeled streaming operations require their first event to have the initial-response type. An empty stream or a stream that begins with another event raises NoInitialResponseError, which the response parser exposes as ResponseParserError because it cannot build the operation's initial result.

References: aiobotocore/eventstream.py• L39-49 aiobotocore/parsers.py• L137-147 tests/…/test_eventstream.py• L529-567

Change checklist

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

  • Keep the event-stream parser class aligned with every service protocol that can model an event stream. The operation parser must select the matching payload decoder.
  • Preserve the initial-response contract when changing result parsing. The top-level operation result and the returned stream depend on the same first frame.
  • Keep frame buffering and checksum validation compatible with the transport's chunk iterator. A change in chunk shape can affect both partial and multi-message reads.
  • Preserve the async-only iterator contract. Synchronous iteration must not become a hidden blocking path.
  • Keep service error status mapping aligned with EventStreamError handling so error frames do not appear as successful events.

Important failure modes

TriggerConsequenceGuard
The first frame is not an initial-response event.The client cannot build the normal operation result and raises ResponseParserError.Validate the initial event type before returning the stream to the caller.
A frame has a bad length, header structure, or checksum.Decoding stops and the invalid event is not yielded.Keep the event buffer and wire-format validation together.
The service sends an error event after normal events.Async iteration raises EventStreamError and stops.Handle errors around the async for loop, not only around the initial call.
A caller uses synchronous iteration.NotImplementedError is raised.Use async for and keep the client operation inside an async context.

Key files

FileWhy to read it
aiobotocore/eventstream.pyDefines the asynchronous stream contract, chunk-to-message flow, initial-response lookup, and service error conversion.
aiobotocore/parsers.pyShows how modeled streaming operation responses create the stream and choose JSON, XML, or CBOR event decoders.
tests/test_eventstreams.pyDemonstrates complete event consumption, chunked input, synchronous-iteration rejection, and a live Kinesis stream shape.
tests/…/test_eventstream.pyDocuments the framing rules, typed headers, checksum checks, initial-response behavior, and stream-level failures.