Sycophant.AWS.EventStream (sycophant v0.4.2)

Copy Markdown

Decoder for the AWS event stream binary framing protocol.

Used by Bedrock's /converse-stream endpoint which returns responses as binary-framed events rather than SSE.

Frame layout

| prelude (12 B) | headers (variable) | payload (variable) | message CRC (4 B) |

The prelude contains total_length (4 B), headers_length (4 B), and prelude_crc (4 B, CRC-32 of the first 8 bytes).

The trailing message CRC covers everything before it.

Summary

Functions

Decodes a complete AWS event stream binary into a list of event maps.

Decodes a single frame from the beginning of an event stream binary.

Functions

decode(data)

@spec decode(binary()) :: {:ok, [map()]} | {:error, term()}

Decodes a complete AWS event stream binary into a list of event maps.

Iterates through all concatenated frames in data, verifying both prelude and message CRC-32 checksums for each one. Returns {:ok, events} when every frame is valid and complete, or {:error, reason} on CRC mismatch or truncated input.

decode_frame(data)

@spec decode_frame(binary()) ::
  {:ok, map(), binary()} | {:incomplete, binary()} | {:error, term()}

Decodes a single frame from the beginning of an event stream binary.

Returns {:ok, event, rest} on success, where event is a map with :headers and :payload keys and rest is the unconsumed binary. Returns {:incomplete, data} when the buffer does not yet contain a full frame, or {:error, reason} on CRC validation failure.