Skip to content
+

Chat - Streaming lifecycle

Observe the full runtime lifecycle for send, stream, stop, error, retry, and callbacks.

The demo below exposes success, failure, and long-running stream modes so you can observe every lifecycle phase:

  • sendMessage()—send a user turn and start streaming.
  • stopStreaming()—cancel an active stream.
  • retry()—retry a failed or cancelled message.
  • onData—fires when a data-* chunk arrives.
  • onFinish—fires when the stream ends (success, abort, or error).
  • onError—fires on any runtime error.

Lifecycle phases

Sending and streaming a message

When sendMessage() is called:

  1. The user message is added optimistically with status: 'sending'.
  2. The adapter's sendMessage() returns a ReadableStream.
  3. Chunks update the assistant message in real time.
  4. A finish chunk completes the turn—the user message moves to status: 'sent'.

Stopping a stream

Call stopStreaming() to abort the active stream. The user message moves to status: 'cancelled' and the onFinish callback fires with isAbort: true.

Retrying a failed message

When a stream fails or is cancelled, call retry(messageId) with the user message ID. The runtime removes the failed assistant messages and resends the user turn.

Runtime callbacks

Register callbacks on ChatProvider to observe lifecycle events:

<ChatProvider
  adapter={adapter}
  onData={(part) => console.log('data chunk', part.type)}
  onFinish={(payload) => console.log('finished', payload.finishReason)}
  onError={(error) => console.log('error', error.message)}
>
  <MyChat />
</ChatProvider>

The demo below exercises every lifecycle callback alongside sendMessage(), stopStreaming(), and retry():

Streaming lifecycle
Callback events will appear here.

No messages yet.

Lifecycle summary

  • The runtime manages the full send-stream-finish lifecycle automatically.
  • stopStreaming() aborts the stream and triggers onFinish with isAbort: true.
  • retry() removes failed assistant messages and resends the original user turn.
  • Callbacks (onData, onFinish, onError) expose lifecycle events without touching the store.

See also

  • See Streaming for details on the full chunk protocol reference.
  • See State and store for details on the error model and callback signatures.
  • See Message parts for details on rendering multi-part responses.
  • See Tool call events for details on tool-specific lifecycle callbacks.

API