class

Anthropic::MessageStream

Inherits Enumerable < Reference < Object

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
final_parsed_output_as(type : T.class) : T | Nil forall T

Parse the final message's structured output into T.

Returns nil when no final message exists yet, the message has no text content, or the text does not parse as JSON.

Source
final_parsed_output_as!(type : T.class) : T forall T

Parse the final message's structured output into T.

Raises StructuredOutputParseError when no final message exists, the message has no text content, or the text does not parse as T.

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