📄 ai-sdk/docs/reference/stream-helpers/stream-to-response

File: stream-to-response.md | Updated: 11/15/2025

Source: https://ai-sdk.dev/docs/reference/stream-helpers/stream-to-response

AI SDK

Menu

v5 (Latest)

AI SDK 5.x

AI SDK by Vercel

AI SDK 6 Beta

Foundations

Overview

Providers and Models

Prompts

Tools

Streaming

Getting Started

Navigating the Library

Next.js App Router

Next.js Pages Router

Svelte

Vue.js (Nuxt)

Node.js

Expo

Agents

Agents

Building Agents

Workflow Patterns

Loop Control

AI SDK Core

Overview

Generating Text

Generating Structured Data

Tool Calling

Model Context Protocol (MCP) Tools

Prompt Engineering

Settings

Embeddings

Image Generation

Transcription

Speech

Language Model Middleware

Provider & Model Management

Error Handling

Testing

Telemetry

AI SDK UI

Overview

Chatbot

Chatbot Message Persistence

Chatbot Resume Streams

Chatbot Tool Usage

Generative User Interfaces

Completion

Object Generation

Streaming Custom Data

Error Handling

Transport

Reading UIMessage Streams

Message Metadata

Stream Protocols

AI SDK RSC

Advanced

Reference

AI SDK Core

AI SDK UI

AI SDK RSC

Stream Helpers

AIStream

StreamingTextResponse

streamToResponse

OpenAIStream

AnthropicStream

AWSBedrockStream

AWSBedrockAnthropicStream

AWSBedrockAnthropicMessagesStream

AWSBedrockCohereStream

AWSBedrockLlama2Stream

CohereStream

GoogleGenerativeAIStream

HuggingFaceStream

@ai-sdk/langchain Adapter

@ai-sdk/llamaindex Adapter

MistralStream

ReplicateStream

InkeepStream

AI SDK Errors

Migration Guides

Troubleshooting

Copy markdown

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 { openai } from '@ai-sdk/openai';import { StreamData, streamText, streamToResponse } from 'ai';import { createServer } from 'http';
createServer(async (req, res) => {  const result = streamText({    model: openai('gpt-4.1'),    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

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' }`.

data:

StreamData

StreamData object for forwarding additional data to the client.

On this page

streamToResponse

Import

Example

API Signature

Parameters

Deploy and Scale AI Apps with Vercel.

Vercel delivers the infrastructure and developer experience you need to ship reliable AI-powered applications at scale.

Trusted by industry leaders:

  • OpenAI
  • Photoroom
  • leonardo-ai Logoleonardo-ai Logo
  • zapier Logozapier Logo

Talk to an expert