Interface StreamObjectResult<T, E>

The result object returned immediately by streamObject.

Consumers iterate partialObjectStream for incremental partial objects, or elementStream for the completed elements of the array field named by StreamObjectOptions.elements, and await the 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.

interface StreamObjectResult<T, E> {
    partialObjectStream: AsyncIterable<DeepPartial<T>, any, any>;
    elementStream: AsyncIterable<E, any, any>;
    object: Promise<T>;
    text: Promise<string>;
    usage: Promise<TokenUsage>;
    reportedUsage: Promise<undefined | TokenUsage>;
    skipped: Promise<number>;
}

Type Parameters

  • T

    The inferred type from the Zod schema.

  • E = unknown

    The type of one element elementStream yields (StreamObjectElement).

Properties

partialObjectStream: AsyncIterable<DeepPartial<T>, any, any>

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.

Example

for await (const partial of result.partialObjectStream) {
console.log('partial:', partial);
}
elementStream: AsyncIterable<E, any, any>

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.

Example

for await (const sentence of result.elementStream) {
console.log(sentence.text);
}
object: Promise<T>

Resolves to the final Zod-validated object when the stream completes. A rejection that nothing awaits is not reported as an unhandled one.

Throws

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.

text: Promise<string>

Resolves to the raw text when the stream completes.

usage: Promise<TokenUsage>

Resolves to aggregated token usage when the stream completes: zero for whatever the provider never reported.

reportedUsage: Promise<undefined | TokenUsage>

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.

skipped: Promise<number>

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.