createDataStreamResponse
The createDataStreamResponse function creates a Response object that streams data to the client (see Streaming Data).
Import
import { createDataStreamResponse } from "ai"Example
const response = createDataStreamResponse({ status: 200, statusText: 'OK', headers: { 'Custom-Header': 'value', }, async execute(dataStream) { // Write data dataStream.writeData({ value: 'Hello' });
// Write annotation dataStream.writeMessageAnnotation({ type: 'status', value: 'processing' });
// Merge another stream const otherStream = getAnotherStream(); dataStream.merge(otherStream); }, onError: error => `Custom error: ${error.message}`,});API Signature
Parameters
status:
number
statusText:
string
headers:
Headers | Record<string, string>
execute:
(dataStream: DataStreamWriter) => Promise<void> | void
DataStreamWriter
write:
(data: DataStreamString) => void
writeData:
(value: JSONValue) => void
writeMessageAnnotation:
(value: JSONValue) => void
writeSource:
(source: Source) => void
merge:
(stream: ReadableStream<DataStreamString>) => void
onError:
((error: unknown) => string) | undefined
onError:
(error: unknown) => string
Returns
Response
A Response object that streams formatted data stream parts with the specified status, headers, and content.