Module: RubyLLM::BedrockInvoke::EventStream

Included in:
Protocol, Providers::BedrockInvoke
Defined in:
lib/ruby_llm/bedrock_invoke/event_stream.rb

Overview

Decodes AWS event-stream framing (application/vnd.amazon.eventstream) from InvokeModelWithResponseStream into plain Anthropic streaming events.

Each chunk event carries a PayloadPart whose base64 bytes field decodes to one standard Anthropic SSE event (message_start, content_block_delta, ...). Failures surface three ways, all handled before the payload is trusted: frames with :message-type 'exception' (typed via :exception-type, payload may be empty), frames with :message-type 'error' (:error-code/:error-message headers, empty payload), and exception-suffixed keys inside chunk payloads.

Constant Summary collapse

EXCEPTION_STATUS =
{
  'throttlingException' => 429,
  'validationException' => 400,
  'modelTimeoutException' => 408,
  'modelStreamErrorException' => 424,
  'serviceUnavailableException' => 503,
  'internalServerException' => 500
}.freeze

Instance Method Summary collapse

Instance Method Details

#each_event_stream_event(decoder, raw_chunk) ⇒ Object

Yields one decoded Anthropic event Hash per event in the raw chunk. Raises through ErrorMiddleware when the stream carries an exception event.



35
36
37
38
39
40
41
42
# File 'lib/ruby_llm/bedrock_invoke/event_stream.rb', line 35

def each_event_stream_event(decoder, raw_chunk, &)
  message, eof = decoder.decode_chunk(raw_chunk)
  process_event_stream_message(message, &) if message
  until eof
    message, eof = decoder.decode_chunk
    process_event_stream_message(message, &) if message
  end
end

#event_stream_decoderObject



28
29
30
31
# File 'lib/ruby_llm/bedrock_invoke/event_stream.rb', line 28

def event_stream_decoder
  require 'aws-eventstream'
  ::Aws::EventStream::Decoder.new
end