Skip to main content

Stream Processing Guide

Handle real-time AI streaming responses efficiently.

Overview

Streaming allows you to display AI responses as they’re generated, improving perceived performance and user experience.

Basic Streaming

TypeScript/JavaScript

Python

React Streaming

With useEffect

Custom Hook

Next.js Streaming

App Router

Error Handling

Retry on Interruption

Timeout Handling

Best Practices

1. Buffer for UI Updates

2. Handle Partial Tokens

3. Cancel Streams

Performance Tips

  1. Update UI efficiently - Don’t update DOM on every chunk
  2. Use RequestAnimationFrame for smooth rendering
  3. Buffer updates - Batch UI updates every 50-100ms
  4. Cancel appropriately - Clean up aborted streams
  5. Handle errors gracefully - Show error states clearly

Related: Multi-turn Conversations | Best Practices