Stream HelpersStreamingReactResponse

StreamingReactResponse

The StreamingReactResponse class is designed to facilitate streaming React responses in a server action environment. It can handle and stream both raw content and data payloads, including special UI payloads, through nested promises.

Import

React

import { StreamingReactResponse } from "ai"

Parameters

stream:

ReadableStream

options:

Options
Options

ui?:

(Message) => UINode | Promise<UINode>
A function that receives a message object with content and optional data fields. This function should return a React component (as UINode) for each chunk in the stream. The data attribute in the message is available when the data option is configured to include stream data.
Message

content:

string
The text content of the message.

data?:

JSONValue[] | undefined
The data payload of the message.

data?:

experimental_StreamData
An instance of experimental_StreamData used to process and stream data along with the response.

Returns

A Promise<ReactResponseRow>, which resolves to the next row of the React response. Each row contains a payload with UI components (ui), raw content (content), and a next property pointing to the subsequent row or null if it's the last row. This setup allows for continuous streaming and rendering of data in a React-based UI.