Skip to main content
Streaming responses arrive as a sequence of text chunks. How you apply those chunks to your UI determines whether the experience feels fast and polished or jittery and broken. This guide covers the three patterns that matter most.
The underlying WebSocket delivers one complete response frame per turn — see WebSocket streaming for the wire protocol. The chunk events consumed by session.stream() below are a client-side reveal effect the SDK builds on top of that single frame, not genuine incremental network delivery. The patterns on this page apply the same way either way — they’re UI concerns, not wire-protocol concerns — but don’t expect a chunk frame to arrive from the network faster than the full response does.

Pattern 1 — Cursor animation

Show a blinking cursor at the end of the in-progress text to signal that more is coming. Remove it immediately when turn_end fires.

Pattern 2 — Avoiding layout shift

If the response container starts empty and expands as text arrives, surrounding elements jump around. Pre-allocate a minimum height, or use CSS to hold space before the first token lands.

Pattern 3 — Abort and retry

When the user wants to interrupt a response or send a corrected message while the AI is still generating, you need to cancel the in-flight stream. The SDK exposes an AbortController-compatible mechanism via session.abort().
session.abort() cancels the current stream iterator without closing the session. The session stays open — the next call to session.stream() or session.sendMessage() will work normally.