Skip to main content
Streaming lets you receive partial updates as the LLM generates its response, rather than waiting for the complete result. This is useful for improving perceived latency in user interfaces — you can render data progressively as it arrives.

Basic Streaming

Call stream() instead of get() to receive a StructuredOutputStream. The partials() method yields parsed partial objects as the response is built.
Instructor is smart about updates. It calculates and compares hashes of the previous and newly deserialized version of the model, so your callback only fires when a property actually changes — not on every token received. Partial updates are deserialized but not validated. Only the final result returned by finalValue() is fully validated, making it safe to persist or process further.

Explicit Streaming Control

You can also enable streaming with withStreaming() and then call get(), which internally drains the stream and returns the final value.

StructuredOutputStream Methods

The StructuredOutputStream class provides several ways to consume the stream.

Iteration Methods

Result Access Methods

Utility Methods

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 a Sequence response model, you can stream completed items individually rather than waiting for the entire list.

Streaming with Output Formats

Streaming works with all output formats. During streaming, partials are always objects regardless of your chosen output format. The final value respects the format you specified. See Output Formats for details.