class

Anthropic::MessageStream

Inherits Enumerable < Reference < Object

MessageStream class for handling Server-Sent Events (SSE) from streaming API

Events are buffered on first iteration so that helper methods like text, collect_text, final_message, thinking, and citations can be called in any order and multiple times.

Constructors

Instance methods

citations

Iterate through citations

Source
collect_text

Collect all text into a single string

Source
each

Iterate through all events in the stream.

On the first call this reads from the response IO and buffers all events. Subsequent calls replay from the buffer so that helpers like text, collect_text, and final_message can be used in any order and multiple times.

When the stream contains an SSE error event, this raises an appropriately-typed Anthropic::APIError subclass immediately after yielding the preceding events, mirroring the Ruby SDK's behavior. The raw ErrorEvent is also yielded so callers can still observe it.

Source
final_message

Get final message after consuming the entire stream.

Safe to call after text, collect_text, or any other helper -- events are buffered and replayed.

Source
text

Convenience iterator for text only

Source
thinking

Get accumulated thinking content

Source
tool_use_deltas

Iterate only tool use deltas

Source

Nested types