View a markdown version of this page

Working with event streams - AWS SDK for Python v4

Developer Preview: This documentation covers the AWS SDK for Python v4, which is in Developer Preview and intended for evaluation and testing only. Do not use it for production workloads. For production applications, use the AWS SDK for Python (Boto3). To understand the differences between the two SDKs, see Choosing the right AWS SDK for Python.

Working with event streams

Event stream operations are available only in asynchronous clients. For how the two client types differ, see Synchronous and asynchronous clients.

Some workloads, such as live transcription and real-time model inference, produce data continuously while an operation is still running. For these workloads, services provide event stream operations. An event stream operation stays open and exchanges a sequence of modeled events with the application. The SDK supports the following types of event streams:

  • In an output event stream, the service sends events to the application. The application reads events until the service closes the output.

  • In a bidirectional duplex event stream, the application sends events through input_stream while it receives service events from the output side.

A service can also model an input-only event stream, in which only the application sends events. The pattern for sending events matches the input side of a bidirectional stream.

You call an event stream operation with keyword arguments, the same as any other operation. The keyword arguments set the members of the initial request. The events that you send after the stream opens are generated model objects. Each event stream is an asynchronous context manager. Leaving its async with block closes the stream and releases its resources. The service model defines the input and output event types and any other events required to complete the operation.

Event streams are different from streaming request and response bodies. A streaming body, such as the contents of an Amazon S3 object, is a single sequence of bytes in an operation's input or output. Operations with streaming bodies work in both synchronous and asynchronous clients. For examples, see Amazon S3.

Consuming an output-only stream

An output event stream operation returns an open stream object. Calling the operation starts the request, but event data arrives later as the application iterates output_stream. Each item is a variant of the operation's output event union. Each variant class represents one event type.

The following example calls the operation, enters the stream's asynchronous context, and waits for events with async for. It collects payload chunks and raises an error for an event type that the installed client doesn't recognize.

from aws_sdk_bedrock_runtime import AsyncBedrockRuntimeClient from aws_sdk_bedrock_runtime.models import ( ResponseStreamChunk, ResponseStreamUnknown, ) async def collect_response_chunks( client: AsyncBedrockRuntimeClient, model_id: str, body: bytes, ) -> list[bytes]: response = await client.invoke_model_with_response_stream( model_id=model_id, body=body, content_type="application/json", ) chunks: list[bytes] = [] async with response as stream: async for event in stream.output_stream: if isinstance(event, ResponseStreamChunk): if event.value.bytes_: chunks.append(event.value.bytes_) elif isinstance(event, ResponseStreamUnknown): raise RuntimeError( f"Unknown output event: {event.tag}" ) else: raise RuntimeError( f"Unexpected output event: {type(event).__name__}" ) return chunks

Leaving the asynchronous context closes the output stream even when event processing raises an exception. A modeled stream error can arrive while the application is iterating events. The SDK raises it as a generated service exception at that point. The event payloads and the signal that completes a stream vary by operation. For details, see the invoke_model_with_response_stream() API reference.

Using a bidirectional event stream

A bidirectional application sends input and receives output at the same time. The application does the following:

  1. Calls the operation to open the stream and enters the stream's asynchronous context.

  2. Runs a sender and a receiver concurrently so that neither side waits for the other side to finish first.

  3. Sends modeled input events. After the last required input or completion event, it closes input_stream to indicate that no more input will follow.

  4. Processes output events until the service closes the output side, and then leaves the context to release the stream resources.

Bidirectional operations require the AWS Common Runtime (CRT) HTTP client as the client's transport. To use it, install the client's awscrt extra and set AWSCRTHTTPClient as the transport. For more information, see Use the AWS CRT client for bidirectional streaming.

The following example uses Amazon Transcribe Streaming. The application supplies audio as an asynchronous iterable and handles transcript events in its own function. The example code opens the stream, sends and receives events, and closes the stream. It doesn't produce audio or interpret transcripts.

The following snippet contains the imports that the rest of this section shares.

import asyncio from collections.abc import AsyncIterable, Callable from smithy_core.aio.interfaces.eventstream import ( AsyncEventPublisher, AsyncEventReceiver, ) from aws_sdk_transcribe_streaming import AsyncTranscribeStreamingClient from aws_sdk_transcribe_streaming.models import ( AudioEvent, AudioStream, AudioStreamAudioEvent, LanguageCode, MediaEncoding, TranscriptEvent, TranscriptResultStream, TranscriptResultStreamTranscriptEvent, TranscriptResultStreamUnknown, )

Sending input events

A bidirectional stream exposes input_stream for events sent by the application. The operation model defines an input event union. Each generated union variant represents one event type. For Amazon Transcribe, AudioStreamAudioEvent is the variant that carries an AudioEvent.

The sender wraps each prepared payload in the generated event classes and awaits send(). The asynchronous source determines how the application produces those payloads.

async def send_audio( input_stream: AsyncEventPublisher[AudioStream], audio_chunks: AsyncIterable[bytes], ) -> None: """Send prepared audio chunks to the input side of the stream.""" try: async for chunk in audio_chunks: await input_stream.send( AudioStreamAudioEvent(value=AudioEvent(audio_chunk=chunk)) ) finally: await input_stream.close()

Closing input_stream half-closes the operation. The application can't send more input events, but the service can continue to return output events. The finally block closes the input side even if producing or sending a chunk fails. Closing this side doesn't close the output side or discard output that is still arriving.

Receiving output events

A bidirectional operation can return an initial modeled response before it starts to yield output events. This response contains the non-streaming members of the operation output. A separate receiver supplies the later events. The transcribe_audio function in Coordinating both sides calls await_output() to wait for both, ignores the initial response, and passes the receiver to receive_events().

Each received item is one variant of the generated output event union. The receiver validates the variant and passes the modeled event payload to an application-provided handler. The handler determines how to interpret or store the service-specific payload.

async def receive_events( output_stream: AsyncEventReceiver[TranscriptResultStream], handle_event: Callable[[TranscriptEvent], None], ) -> None: """Receive modeled events and pass them to an application handler.""" async for event in output_stream: if isinstance(event, TranscriptResultStreamUnknown): raise RuntimeError(f"Unknown stream event: {event.tag}") if not isinstance(event, TranscriptResultStreamTranscriptEvent): raise RuntimeError(f"Unexpected stream event: {type(event).__name__}") handle_event(event.value)

A modeled service error can also arrive as the application iterates the receiver. The SDK raises it as a generated exception. A TranscriptResultStreamUnknown value means that the service sent an event variant that the installed SDK doesn't recognize. Its tag identifies the unrecognized variant so that the application can report it without treating it as a known event.

When iteration ends, the service has closed output_stream and no more output events remain.

Coordinating both sides

If an application waits for all input to finish before it reads output, the service or client can block while output waits to be consumed. The following function starts the operation with keyword arguments for the initial request, enters the stream context, and uses asyncio.gather() to run the sender and receiver concurrently. The caller supplies the audio source, the event handler, and the sample rate of the audio.

async def transcribe_audio( client: AsyncTranscribeStreamingClient, audio_chunks: AsyncIterable[bytes], handle_event: Callable[[TranscriptEvent], None], sample_rate: int, ) -> None: """Exchange events over a bidirectional transcription stream.""" stream = await client.start_stream_transcription( language_code=LanguageCode.EN_US, media_sample_rate_hertz=sample_rate, media_encoding=MediaEncoding.PCM, ) async with stream: _, output_stream = await stream.await_output() if output_stream is None: raise RuntimeError("The service returned no output stream") await asyncio.gather( send_audio(stream.input_stream, audio_chunks), receive_events(output_stream, handle_event), )

When the source has no more chunks, send_audio closes the input side. The receive_events function continues until the service closes the output side. Leaving the asynchronous stream context then releases both sides.

The operation model defines how a bidirectional stream ends. Send any required completion event before you close input_stream.

For a runnable application, see Example 2: Stream audio bidirectionally with Amazon Transcribe. The SDK repository on GitHub also provides examples for a prerecorded file and live microphone input.

Applying the pattern to other operations

Other services also provide bidirectional event stream operations, including invoke_model_with_bidirectional_stream() in Amazon Bedrock Runtime, start_conversation() in Amazon Lex Runtime V2, and start_speech_synthesis_stream() in Amazon Polly.

Each operation defines its own input and output event variants and the signal that finishes the stream. Read the operation API reference before you reuse the Amazon Transcribe coordination pattern. For the Amazon Nova Sonic protocol that Bedrock Runtime uses, see Use bidirectional streaming.