generateStream returns an AsyncIterable<GenerationEvent>. It is not a promise: you get the
iterable back synchronously and consume it with for await.
Signature
The events
In order:
Exactly one terminal event arrives,
completed or failed. The SDK never fabricates a
successful completed.
Where errors surface
Preflight failures throw from the call: no model available, bad options. A failure during generation arrives as afailed event rather than a thrown error, so a UI can render a retry
without a try/catch around the loop.
Cancelling
Break out of the loop:AbortController:
Keeping the page responsive
Inference runs in a worker, so the main thread stays free. What can still stall a page is updating the DOM on every token. Batch withrequestAnimationFrame for long outputs: