Docs
Cloudflare
CodeRabbit
Railway
SerpAPI
Netlify
OpenRouter
AG Grid
WorkOS
Clerk
Unkey
Sentry
Prisma
Electric
Cloudflare
CodeRabbit
Railway
SerpAPI
Netlify
OpenRouter
AG Grid
WorkOS
Clerk
Unkey
Sentry
Prisma
Electric
Class References
Function References
Interface References
Type Alias References
Variable References

toHttpStream

Function: toHttpStream()

ts
function toHttpStream(stream, abortController?): ReadableStream<Uint8Array<ArrayBufferLike>>;
function toHttpStream(stream, abortController?): ReadableStream<Uint8Array<ArrayBufferLike>>;

Defined in: packages/ai/src/stream-to-response.ts:171

Convert a StreamChunk async iterable to a ReadableStream in HTTP stream format (newline-delimited JSON)

This creates a ReadableStream that emits chunks as newline-delimited JSON:

  • Each chunk is JSON.stringify'd and followed by "\n"
  • No SSE formatting (no "data: " prefix)

This format is compatible with fetchHttpStream connection adapter.

Parameters

stream

AsyncIterable<AGUIEvent>

AsyncIterable of StreamChunks from chat()

abortController?

AbortController

Optional AbortController to abort when stream is cancelled

Returns

ReadableStream<Uint8Array<ArrayBufferLike>>

ReadableStream in HTTP stream format (newline-delimited JSON)

Example

typescript
const stream = chat({ adapter: openaiText(), model: "gpt-4o", messages: [...] });
const readableStream = toHttpStream(stream);
// Use with Response for HTTP streaming (not SSE)
return new Response(readableStream, {
  headers: { 'Content-Type': 'application/x-ndjson' }
});
const stream = chat({ adapter: openaiText(), model: "gpt-4o", messages: [...] });
const readableStream = toHttpStream(stream);
// Use with Response for HTTP streaming (not SSE)
return new Response(readableStream, {
  headers: { 'Content-Type': 'application/x-ndjson' }
});