Interface: StreamObjectResult<T, E>
Defined in: packages/agentos/src/api/streamObject.ts:232
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.
Type Parameters
T
T
The inferred type from the Zod schema.
E
E = unknown
The type of one element elementStream yields
(StreamObjectElement).
Properties
elementStream
elementStream:
AsyncIterable<E>
Defined in: packages/agentos/src/api/streamObject.ts:282
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
object:
Promise<T>
Defined in: packages/agentos/src/api/streamObject.ts:293
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.
partialObjectStream
partialObjectStream:
AsyncIterable<DeepPartial<T>>
Defined in: packages/agentos/src/api/streamObject.ts:251
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);
}
reportedUsage
reportedUsage:
Promise<TokenUsage|undefined>
Defined in: packages/agentos/src/api/streamObject.ts:311
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
skipped:
Promise<number>
Defined in: packages/agentos/src/api/streamObject.ts:318
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.
text
text:
Promise<string>
Defined in: packages/agentos/src/api/streamObject.ts:296
Resolves to the raw text when the stream completes.
usage
usage:
Promise<TokenUsage>
Defined in: packages/agentos/src/api/streamObject.ts:302
Resolves to aggregated token usage when the stream completes: zero for whatever the provider never reported.