Overview
ThestreamObject() function streams structured objects from chat models in real-time, with progressive updates validated against a Zod schema. This is ideal for displaying partial results as they’re being generated.
Function signature
Parameters
ChatModel
required
The chat model instance to use for streaming.
Message[]
required
Array of messages in the conversation. Must not be empty.
z.ZodType
required
Zod schema that defines the structure of the object to generate.
string
Optional name for the schema.
string
Optional description of the schema.
number
Sampling temperature (0-2).
number
Maximum number of tokens to generate.
number
Nucleus sampling parameter (0-1).
ReasoningConfig
Configuration for extended thinking/reasoning capabilities.
GenerateProviderOptions
Provider-specific options, namespaced by provider name.
AbortSignal
AbortSignal for cancelling the stream.
Return value
Returns aPromise<ObjectStream<TSchema>>. ObjectStream is an async iterable of ObjectStreamEvent objects with two additional properties:
Promise<GenerateObjectResult<TSchema>>
Resolves with the final validated object result when the stream completes. Rejects on abort or upstream failure.
Promise<readonly ObjectStreamEvent<TSchema>[]>
Resolves with all observed events, including abort and failure cases.
The HTTP request starts as soon as you create the stream. You do not need to iterate before the model begins responding.
ObjectStreamEvent types
{ type: 'object-delta'; text: string }
Emitted for each chunk of the JSON object being generated.
{ type: 'object'; object: z.infer<TSchema> }
Emitted once, when the full object payload has been validated.
{ type: 'finish'; finishReason: FinishReason; usage: ChatUsage }
Emitted when streaming completes with final metadata.
Examples
Basic streaming object
Progressive UI updates
Using .result
Using .events
ObjectStream follows the same lifecycle semantics as ChatStream: .result settles independently of iteration, .events keeps the observed history, and iteration is replayable.Cancellation
Important notes
ObjectStream is replayable: iterating after events have already arrived replays the buffered event history.The
object event is emitted once with the complete validated object. Use object-delta events to show progressive updates while the JSON payload is still streaming.Error handling
ThrowsValidationError if:
- Messages array is empty
CoreAIErrorif the stream completes without generating an objectProviderErrorif the provider returns an error during streaming
StructuredOutputValidationErrorStructuredOutputParseError