The inferred type from the Zod schema.
The type of one element elementStream yields
(StreamObjectElement).
Async iterable yielding partial objects as the LLM builds the JSON
response token by token. Each yielded value has the same shape as T
but with all fields optional (DeepPartial). The iteration ends
without throwing when the stream fails, and object then rejects. A
reader that starts after elementStream began the stream receives the
latest partial object first, and a reader that falls behind while
elementStream reads on receives the latest partial object when it reads
again, since each holds the ones before it: at most one partial object
waits for it.
for await (const partial of result.partialObjectStream) {
console.log('partial:', partial);
}
Async iterable yielding each element of the array field named by
StreamObjectOptions.elements, in order, as soon as it is complete:
an element once the next one begins, and the last one once the array's
closing bracket arrives or, at the latest, when the stream ends with text
that parses, so a reply cut off at the token limit never yields its
unfinished last element. Each is the value the array's element schema
parses it to; an element that fails the schema is skipped and counted in
StreamObjectResult.skipped. Without elements it yields nothing.
When the stream fails, the iteration throws the stream's error after the
elements completed before it, so a failed answer never reads as a
finished one. When the stream ends with text that does not parse, such
as a reply cut off at the token limit, the iteration throws the
ObjectGenerationError object rejects with
(Failed to parse streamed JSON: ...) after the elements completed
before the end; an empty reply ends it with nothing yielded, and object
rejects. A reader that starts after every iterable that was read
stopped before the end throws ObjectGenerationError
(Stream interrupted: ...) after the elements completed before the stop.
When abortSignal aborts, the iteration ends.
for await (const sentence of result.elementStream) {
console.log(sentence.text);
}
Resolves to the final Zod-validated object when the stream completes. A rejection that nothing awaits is not reported as an unhandled one.
When the final JSON fails to parse or
validate, and with the message Stream interrupted: <message> when the
stream fails or every reader stops before it ends. When abortSignal
aborts, it rejects with the signal's reason instead.
Resolves to the raw text when the stream completes.
Resolves to aggregated token usage when the stream completes: zero for whatever the provider never reported.
Resolves, when the stream ends, to the usage the provider reported, a
report of zero tokens included, or to undefined when the stream failed,
was aborted or was not read to its end, or ended with no usage reported.
A caller never reads usage it never received as zero, as it would from
StreamObjectResult.usage.
Resolves, when the stream ends, to the number of elements of the
StreamObjectOptions.elements field that failed the element schema
and were skipped; 0 without elements.
The result object returned immediately by streamObject.
Consumers iterate
partialObjectStreamfor incremental partial objects, orelementStreamfor the completed elements of the array field named by StreamObjectOptions.elements, andawaitthe promise properties for the final validated result. Reading either iterable starts the request; the promises settle when the stream ends, or when every iterable that was read stopped before the end.