module

Anthropic::Bedrock::EventStream

Bedrock's invoke-with-response-stream returns application/vnd.amazon.eventstream (AWS binary event-stream framing), not SSE. The SDK's MessageStream consumer parses SSE only — without this transcoder a Bedrock stream would yield zero events.

Each :message-type: event frame carries a JSON payload {"bytes":"<base64>"} wrapping a standard Anthropic event JSON; this re-emits those as event:/data: SSE bytes so existing stream helpers work unchanged. Exception frames become SSE error events.

Wire format (Smithy Amazon Event Stream / botocore): prelude: total_len(u32 BE) | headers_len(u32 BE) | prelude_crc(u32 BE) headers + payload message_crc(u32 BE)

Constants

AWS_CONTENT_TYPE = "application/vnd.amazon.eventstream"
MAX_HEADERS_LENGTH = 128 * 1024
MAX_PAYLOAD_LENGTH = (24 * 1024) * 1024
PRELUDE_LENGTH = 12

Instance methods

encode_chunk_event(anthropic_event_json : String) : Bytes

Encode a Bedrock "chunk" event wrapping Anthropic SSE event JSON.

Source
encode_error(error_code : String, error_message : String) : Bytes

Encode an unmodeled error frame.

Source
encode_exception(exception_type : String, message : String) : Bytes

Encode an exception frame (Bedrock model/stream errors).

Source
encode_message(headers : Hash(String, String), payload : Bytes | String) : Bytes

Encode a complete AWS event-stream message with string headers.

Source
eventstream?(content_type : String | Nil) : Bool

True when the response is AWS event-stream framed (not SSE).

Source
to_sse(io : IO) : String

Transcode an entire AWS event-stream body into SSE text.

Source
to_sse(data : Bytes) : String
Source
to_sse(data : String) : String
Source

Nested types