
# `streamToResponse`

<Note type="warning">
  `streamToResponse` has been removed in AI SDK 4.0. Use
  `pipeDataStreamToResponse` from
  [streamText](/docs/reference/ai-sdk-core/stream-text) instead.
</Note>

`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

<Snippet text={`import { streamToResponse } from "ai"`} prompt={false} />

## Example

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

```ts
import { StreamData, streamText, streamToResponse } from 'ai';
__PROVIDER_IMPORT__;
import { createServer } from 'http';

createServer(async (req, res) => {
  const result = streamText({
    model: __MODEL__,
    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

<PropertiesTable
  content={[
    {
      name: 'stream',
      type: 'ReadableStream',
      description:
        'The Web Stream to pipe to the response. It can be the return value of OpenAIStream, HuggingFaceStream, AnthropicStream, or an AIStream instance.',
    },
    {
      name: 'response',
      type: 'ServerResponse',
      description:
        'The Node.js ServerResponse object to pipe the stream to. This is usually the second argument of a Node.js HTTP request handler.',
    },
    {
      name: 'options',
      type: 'Options',
      description: 'Configure the response',
      properties: [
        {
          type: 'Options',
          parameters: [
            {
              name: 'status',
              type: 'number',
              description:
                'The status code to set on the response. Defaults to `200`.',
            },
            {
              name: 'headers',
              type: 'Record<string, string>',
              description:
                "Additional headers to set on the response. Defaults to `{ 'Content-Type': 'text/plain; charset=utf-8' }`.",
            },
          ],
        },
      ],
    },
    {
      name: 'data',
      type: 'StreamData',
      description:
        'StreamData object for forwarding additional data to the client.',
    },
  ]}
/>


## Navigation

- [AIStream](/v5/docs/reference/stream-helpers/ai-stream)
- [StreamingTextResponse](/v5/docs/reference/stream-helpers/streaming-text-response)
- [streamToResponse](/v5/docs/reference/stream-helpers/stream-to-response)
- [OpenAIStream](/v5/docs/reference/stream-helpers/openai-stream)
- [AnthropicStream](/v5/docs/reference/stream-helpers/anthropic-stream)
- [AWSBedrockStream](/v5/docs/reference/stream-helpers/aws-bedrock-stream)
- [AWSBedrockAnthropicStream](/v5/docs/reference/stream-helpers/aws-bedrock-anthropic-stream)
- [AWSBedrockAnthropicMessagesStream](/v5/docs/reference/stream-helpers/aws-bedrock-messages-stream)
- [AWSBedrockCohereStream](/v5/docs/reference/stream-helpers/aws-bedrock-cohere-stream)
- [AWSBedrockLlama2Stream](/v5/docs/reference/stream-helpers/aws-bedrock-llama-2-stream)
- [CohereStream](/v5/docs/reference/stream-helpers/cohere-stream)
- [GoogleGenerativeAIStream](/v5/docs/reference/stream-helpers/google-generative-ai-stream)
- [HuggingFaceStream](/v5/docs/reference/stream-helpers/hugging-face-stream)
- [@ai-sdk/langchain Adapter](/v5/docs/reference/stream-helpers/langchain-adapter)
- [@ai-sdk/llamaindex Adapter](/v5/docs/reference/stream-helpers/llamaindex-adapter)
- [MistralStream](/v5/docs/reference/stream-helpers/mistral-stream)
- [ReplicateStream](/v5/docs/reference/stream-helpers/replicate-stream)
- [InkeepStream](/v5/docs/reference/stream-helpers/inkeep-stream)


[Full Sitemap](/sitemap.md)
