class Anthropic::Helpers::Streaming::MessageStream

def accumulate_event(event:, current_snapshot:)

Returns:
  • (Anthropic::Models::Message) - updated message snapshot with event applied

Parameters:
  • current_snapshot (Anthropic::Models::Message, nil) -- current accumulated message state
  • event (Anthropic::Models::RawMessageStreamEvent) -- the raw streaming event to process

Other tags:
    Api: - private
def accumulate_event(event:, current_snapshot:)
 event in Anthropic::Models::RawMessageStreamEvent
age = "Expected event to be a variant of RawMessageStreamEvent, got #{event.class}"
e ArgumentError.new(message)
rent_snapshot.nil?
rn event.message if event.type == :message_start
age = "Unexpected event order, got \"#{event.type}\" before \":message_start\""
e RuntimeError.new(message)
vent
hropic::Models::RawMessageStartEvent
e the converter to create a new, isolated copy of the message object.
is ensures proper type validation and prevents shared object references
at could lead to unintended mutations during streaming accumulation.
tches the Python SDK's approach of explicitly constructing Message objects.
rn Anthropic::Internal::Type::Converter.coerce(Anthropic::Models::Message, event.message)
hropic::Models::RawContentBlockStartEvent
ent_snapshot.content = (current_snapshot.content || []) + [event.content_block]
hropic::Models::RawContentBlockDeltaEvent
ent = current_snapshot.content[event.index]
 (delta = event.delta)
nthropic::Models::TextDelta if content.type == :text
ntent.text += delta.text
nthropic::Models::InputJSONDelta if content.type == :tool_use
on_buf = content.json_buf.to_s
on_buf += delta.partial_json
ntent.input = json_buf
ntent.json_buf = json_buf
nthropic::Models::CitationsDelta if content.type == :text
ntent.citations ||= []
ntent.citations << delta.citation
nthropic::Models::ThinkingDelta if content.type == :thinking
ntent.thinking += delta.thinking
nthropic::Models::SignatureDelta if content.type == :thinking
ntent.signature = delta.signature

hropic::Models::RawMessageDeltaEvent
ent_snapshot.stop_reason = event.delta.stop_reason
ent_snapshot.stop_sequence = event.delta.stop_sequence
ent_snapshot.usage.output_tokens = event.usage.output_tokens
t_snapshot