Skip to main content
AI operations take time. Streaming shows real-time progress as the agent works.

Event Types

Every streaming method returns the same SSEEvent union, so the patterns below work for all of them.

ProgressEvent

A human-readable progress update. message is always set; the agent-loop fields are present on events emitted from inside an agent.

CompleteEvent

The final event on a successful stream. data is the response type of the method you called — for example, a generateGraphStream() stream completes with a GenerateGraphResponse.

ErrorEvent

PreviewEvent

An incremental preview of the agent’s work-in-progress result. The payload is spread at the top level, and its shape depends on the agent — Mutation previews carry config.

ReasoningEvent

A reasoning message emitted by an agent as it works — useful for surfacing the agent’s thinking in a UI.

Async Iterator Pattern

generateGraphStream() returns an async iterator:

Preview Events

Some agents emit preview events carrying partial results before the stream completes. Render them to show work-in-progress.
The fields on a preview event depend on the agent — see each agent’s page for what it streams. Events you do not handle can be safely ignored.

Cancellation

Pass an AbortSignal to cancel mid-operation:

Progress Callback Alternative

If you want progress updates without the streaming API, use the onProgress callback with generateGraph():
This collects the stream internally and returns the final result.

React Pattern

Store the abort controller in a ref for proper cleanup:
Key patterns:
  1. Cancel previous requests — Abort any in-flight request before starting a new one
  2. Store in ref — The abort controller persists across renders
  3. Cleanup on unmount — Cancel pending requests when the component unmounts
  4. Handle AbortError — Don’t treat user cancellation as an error
ProgressEvent carries a message, not a numeric percentage — show the latest message as a status line alongside an indeterminate progress indicator.

Type Guards

The SDK exports isProgressEvent(), isCompleteEvent(), isErrorEvent(), isPreviewEvent(), and isReasoningEvent() for narrowing SSE events, and isGraphyApiError() for narrowing caught errors. All examples on this page use these type guards. See the Type Reference for full signatures.

Error Handling in Streams

Errors can come from two sources:
  1. Error events — The API returns an error during processing
  2. Exceptions — Network failure, timeout, or abort
See Error Handling for details on error codes and retry behavior.