streaming changedbuild-with-claude/streaming
Nearest release: v2.1.286, published an hour after this site recorded the change. Shown because the two are within 24 hours of each other. Nothing here says the release caused the edit.
Recorded here
Lines+13added
Lines−17removed
From line
138
where the diff opens
First seen
14 Aug 2026
this site's first read of the page
Recorded edits7to this page, all time
The whole hunk
from line 138, old and new numbered
/
from line 138
138138
139139## Get the final message without handling events
140140
141If you don't need to process text as it arrives, the SDKs provide a way to use streaming internally while returning the complete `Message` object, identical to what `.create()` returns. This is especially useful for requests with large `max_tokens` values, where the SDKs require streaming to avoid HTTP timeouts.
141If you don't need to process text as it arrives, the SDK provides a way to use streaming internally while returning the complete `Message` object, identical to what `client.messages.create()` (python, typescript, ruby; go: `client.Messages.New()`; java: `client.messages().create()`; csharp: `client.Messages.Create()`; php: `$client->messages->create()`) returns. This is especially useful for requests with large `max_tokens` values, where the SDK requires streaming to avoid HTTP timeouts.
142142
143143<CodeGroup exclude="shell:cURL">
144144 ```bash CLI
from line 195
195195 Messages = [new() { Role = Role.User, Content = "Write a detailed analysis..." }]
196196 };
197197
198 var fullText = "";
199 await foreach (var msg in client.Messages.CreateStreaming(parameters))
200 {
201 fullText += msg;
202 }
203
204 Console.WriteLine(fullText);
198 // To handle events as they arrive as well, pass a MessageContentAggregator to CollectAsync() instead.
199 var message = await client.Messages.CreateStreaming(parameters).Aggregate();
200 Console.WriteLine(message);
205201 ```
206202
207203 ```go Go
from line 250
254250 ```
255251
256252 ```php PHP
253 use Anthropic\Lib\Streaming\MessageAccumulator;
254
257255 $client = new Client();
258256
259257 $stream = $client->messages->createStream(
from line 262
264262 model: 'claude-opus-5-5',
265263 );
266264
267 $fullText = '';
265 $accumulator = MessageAccumulator::forMessages();
268266 foreach ($stream as $event) {
269 if ($event->type === 'content_block_delta' && $event->delta->type === 'text_delta') {
270 $fullText .= $event->delta->text;
271 }
267 $accumulator->accumulate($event);
272268 }
273269
274 echo $fullText;
270 echo array_find($accumulator->message()->content, static fn ($block): bool => $block->type === 'text')->text;
275271 ```
276272
277273 ```ruby Ruby
from line 285
289285 ```
290286</CodeGroup>
291287
292The `.stream()` call keeps the HTTP connection alive with server-sent events, then `.get_final_message()` (Python) or `.finalMessage()` (TypeScript) accumulates all events and returns the complete `Message` object. In Go, you call `message.Accumulate(event)` inside the stream loop to build the same complete `Message`. In Java, use `MessageAccumulator.create()` and call `accumulator.accumulate(event)` on each event. In C#, await the stream's `.Aggregate()` extension method to get the complete `Message`, or pass a `MessageContentAggregator` to `.CollectAsync()` to aggregate while handling events. In Ruby, call `.accumulated_message` on the stream. In the PHP SDK, you iterate over stream events manually to accumulate the response.
288The `.stream()` (java: `.createStreaming()`; csharp: `.CreateStreaming()`; go: `.NewStreaming()`; php: `->createStream()`) call keeps the HTTP connection alive with server-sent events, then the SDK's message-accumulation helper, `stream.get_final_message()` (typescript: `stream.finalMessage()`; ruby: `stream.accumulated_message`; csharp: `.Aggregate()`; go: `message.Accumulate(event)`; java, php: `MessageAccumulator`), collects all events into the complete `Message` object.
293289
294290## Event types
295291
from line 336
340336
341337The deltas for `tool_use` content blocks correspond to updates for the `input` field of the block. To support maximum granularity, the deltas are *partial JSON strings*, whereas the final `tool_use.input` is always an *object*.
342338
343You can accumulate the string deltas and parse the JSON once you receive a `content_block_stop` event, by using a library like [Pydantic](https://docs.pydantic.dev/latest/concepts/json/#partial-json-parsing) to do partial JSON parsing, or by using the [SDKs](https://platform.claude.com/docs/en/cli-sdks-libraries/overview), which provide helpers to access parsed incremental values.
339You can accumulate the string deltas and parse the JSON once you receive a `content_block_stop` event, by using a library like [Pydantic](https://docs.pydantic.dev/latest/concepts/json/#partial-json-parsing) to do partial JSON parsing, or by using the SDK's [streaming helpers](https://platform.claude.com/docs/en/build-with-claude/streaming#streaming-with-sdks). In the Python and TypeScript SDKs, these helpers also give you access to the parsed `input` incrementally, as it streams.
344340
345341A `tool_use` content block delta looks like:
346342
No line in this hunk matches that.