streamToResponse

streamToResponse has been removed in AI SDK 4.0. Use pipeDataStreamToResponse from streamText instead.

streamToResponse pipes a data stream to a Node.js ServerResponse object and sets the status code and headers.

This is useful to create data stream responses in environments that use ServerResponse objects, such as Node.js HTTP servers.

The status code and headers can be configured using the options parameter. By default, the status code is set to 200 and the Content-Type header is set to text/plain; charset=utf-8.

Import

import { streamToResponse } from "ai"

Example

You can e.g. use streamToResponse to pipe a data stream to a Node.js HTTP server response:

import { StreamData, streamText, streamToResponse } from 'ai';
import { createServer } from 'http';
createServer(async (req, res) => {
const result = streamText({
model: "anthropic/claude-sonnet-4.5",
prompt: 'What is the weather in San Francisco?',
});
// use stream data
const data = new StreamData();
data.append('initialized call');
streamToResponse(
result.toAIStream({
onFinal() {
data.append('call completed');
data.close();
},
}),
res,
{},
data,
);
}).listen(8080);

API Signature

Parameters

stream:

ReadableStream

response:

ServerResponse

options:

Options
Options

status:

number

headers:

Record<string, string>

data:

StreamData