Basic Streaming
Callstream() instead of get() to receive a StructuredOutputStream. The partials() method yields parsed partial objects as the response is built.
finalValue() is fully validated, making it safe to persist or process further.
Explicit Streaming Control
You can also enable streaming withwithStreaming() and then call get(), which internally drains the stream and returns the final value.
StructuredOutputStream Methods
TheStructuredOutputStream class provides several ways to consume the stream.
Iteration Methods
| Method | Description |
|---|---|
partials() | Yields parsed partial values. Only the final update is validated; earlier partials are only deserialized. |
sequence() | For Sequence response models — yields only completed items. See Sequences. |
responses() | Yields StructuredOutputResponse snapshots as they arrive. |
Result Access Methods
| Method | Description |
|---|---|
finalValue() | Drains the stream and returns the final parsed, validated result. |
finalResponse() | Drains the stream and returns the final StructuredOutputResponse. |
lastUpdate() | Returns the most recently received parsed value. |
lastResponse() | Returns the most recently received StructuredOutputResponse. |
Utility Methods
| Method | Description |
|---|---|
usage() | Returns the latest token usage data from the stream. |
Example: Streaming with Final Retrieval
A common pattern is to stream partials for UI updates, then use the final validated value for persistence.Example: Streaming Sequence Items
When using aSequence response model, you can stream completed items individually rather than waiting for the entire list.