Class: LLM::Bedrock::StreamParser Private

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/providers/bedrock/stream_parser.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Parses Bedrock Converse Stream events into a response body and emits stream callbacks (on_content, on_tool_call, etc.).

Receives decoded JSON payloads from StreamDecoder along with the AWS Event Stream event type header.

Bedrock Converse Stream event types:

messageStart — initial role
contentBlockStart — tool use or reasoning start
contentBlockDelta — text delta, tool input JSON, or reasoning text
contentBlockStop — content block finished
messageStop — final stop reason, usage metadata

Constant Summary collapse

TOOL_MARKER =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"<|DSML|function_calls"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ StreamParser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of StreamParser.

Parameters:



28
29
30
31
32
33
34
35
36
# File 'lib/llm/providers/bedrock/stream_parser.rb', line 28

def initialize(stream)
  @body = {"output" => {"message" => {"role" => "assistant", "content" => []}}}
  @stream = stream
  @text_markers = {}
  @can_emit_content = stream.respond_to?(:on_content)
  @can_emit_reasoning_content = stream.respond_to?(:on_reasoning_content)
  @can_emit_tool_call = stream.respond_to?(:on_tool_call)
  @can_push_content = stream.respond_to?(:<<)
end

Instance Attribute Details

#bodyHash (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Fully constructed response body.

Returns:

  • (Hash)

    Fully constructed response body



24
25
26
# File 'lib/llm/providers/bedrock/stream_parser.rb', line 24

def body
  @body
end

Instance Method Details

#freevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.



67
68
69
# File 'lib/llm/providers/bedrock/stream_parser.rb', line 67

def free
  @text_markers.clear
end

#parse!(payload, event_type: nil) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • payload (Hash)

    Decoded JSON from an event stream frame

  • event_type (String, nil) (defaults to: nil)

    The :event-type header value

Returns:

  • (self)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/llm/providers/bedrock/stream_parser.rb', line 42

def parse!(payload, event_type: nil)
  type = event_type || payload["type"]
  case type
  when "messageStart"
    # { "role" => "assistant" }
  when "contentBlockStart"
    # { "contentBlockIndex" => 0, "start" => { "toolUse" => {...} } }
    handle_content_block_start(payload)
  when "contentBlockDelta"
    # { "contentBlockIndex" => 0, "delta" => { "text" => "..." } }
    handle_content_block_delta(payload)
  when "contentBlockStop"
    handle_content_block_stop(payload)
  when "messageStop"
    # { "stopReason" => "end_turn", "metadata" => {"usage" => {...}} }
    (payload)
  when "metadata"
    # { "usage" => {...} }
    (payload)
  end
  self
end