ReqLLM.Providers.AmazonBedrock.AWSEventStream (ReqLLM v1.0.0)

View Source

Parser for AWS Event Stream protocol.

AWS Event Stream is a binary protocol used by various AWS services for streaming responses. It includes CRC checksums and a specific binary format for framing messages.

This module provides functions to parse the binary stream into decoded events.

Format

Each event in the stream has the following structure:

  • 4 bytes: total message length (big-endian)
  • 4 bytes: headers length (big-endian)
  • 4 bytes: prelude CRC32
  • N bytes: headers (key-value pairs)
  • M bytes: payload/body
  • 4 bytes: message CRC32

Example

data = <<binary_aws_event_stream_data>>
case ReqLLM.AWSEventStream.parse_binary(data) do
  {:ok, events, rest} -> 
    # Process events (list of decoded JSON maps)
    # Keep rest for next chunk
  {:incomplete, data} ->
    # Need more data, buffer it
  {:error, reason} ->
    # Handle error
end

Summary

Functions

Create a Stream that processes AWS event stream chunks from a process mailbox.

Parse binary AWS event stream data into decoded events.

Functions

create_stream(opts \\ [])

Create a Stream that processes AWS event stream chunks from a process mailbox.

This is useful when using Req's :into :self option to collect streaming responses. The stream will receive messages of the form {ref, {:data, chunk}} and {ref, :done}.

Options

  • :timeout - Timeout in milliseconds waiting for chunks (default: 5000)
  • :process_event - Function to process each decoded event (default: identity)

Example

stream = ReqLLM.AWSEventStream.create_stream(
  process_event: fn event ->
    # Transform the event
    %{data: event}
  end
)

Enum.each(stream, fn chunk ->
  IO.inspect(chunk)
end)

parse_binary(data)

Parse binary AWS event stream data into decoded events.

Returns:

  • {:ok, events, rest} - Successfully parsed events with remaining data
  • {:incomplete, data} - Not enough data to parse complete event
  • {:error, reason} - Parse error