Stream HelpersstreamToResponse

streamToResponse

This method will pipe a ReadableStream to a Node.js ServerResponse object. It can be helpful to combine this with other AI stream utilities, such as OpenAIStream, in Node.js environments. Similar to StreamingTextResponse, it automatically sets the status code to 200 and the Content-Type header to 'text/plain; charset=utf-8'

Import

React

import { streamToResponse } from "ai"

Parameters

stream:

ReadableStream
The Web Stream to pipe to the response. It can be the return value of OpenAIStream, HuggingFaceStream, AnthropicStream, or an AIStream instance.

response:

ServerResponse
The Node.js ServerResponse object to pipe the stream to. This is usually the second argument of a Node.js HTTP request handler.

options:

Options
Configure the response
Options

status:

number
The status code to set on the response. Defaults to `200`.

headers:

Record<string, string>
Additional headers to set on the response. Defaults to `{ 'Content-Type': 'text/plain; charset=utf-8' }`.