Streaming makes AI responses feel instantaneous instead of making users stare at a spinner. This tutorial wires up a full streaming pipeline from the OpenAI SDK to a live-typing React UI.
the api route
We create a Next.js Route Handler that calls openai.chat.completions.create with stream: true and pipes the result into a ReadableStream...
reading the stream on the client
A React hook reads the stream chunk-by-chunk, appending to state on each iteration to produce the typing effect...
abort on unmount
We attach an AbortController to cancel in-flight requests when the component unmounts or the user navigates away...
error handling
Network errors mid-stream need special handling since the response has already started with a 200 status...
openai · streaming · next.js