Docs
CodeRabbit
Cloudflare
AG Grid
SerpAPI
Netlify
OpenRouter
Neon
WorkOS
Clerk
Electric
PowerSync
Sentry
Railway
Prisma
Strapi
Unkey
CodeRabbit
Cloudflare
AG Grid
SerpAPI
Netlify
OpenRouter
Neon
WorkOS
Clerk
Electric
PowerSync
Sentry
Railway
Prisma
Strapi
Unkey
Persistence (Advanced)
Embeddings
Reranking
Evaluate
Sandboxes (Advanced)
Class References
Function References
Interface References
Type Alias References
Variable References
Function References

toServerSentEventsResponse

ts
function toServerSentEventsResponse<TOffset>(stream, init?): Response;
function toServerSentEventsResponse<TOffset>(stream, init?): Response;

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

Convert a StreamChunk async iterable to a Response in Server-Sent Events format

This creates a Response that emits chunks in SSE format:

  • Each chunk is prefixed with "data: "
  • Each chunk is followed by "\n\n"
  • Stream ends when the underlying iterable is exhausted (RUN_FINISHED is the terminal event)

Pass a durability sink (memoryStream(request) / durableStream(request)) to make the stream resumable: fresh runs are appended to the log and each SSE event is tagged with an id: offset; a reconnect (native Last-Event-ID) or a ?offset join replays from the log without re-running the producer. batch controls how many chunks are buffered per append (default 32), and batchWaitMs how long a buffered chunk waits for more (default 50).

Type Parameters

TOffset

TOffset extends string = string

Parameters

stream

AsyncIterable<AGUIEvent>

AsyncIterable of StreamChunks from chat()

init?

ResponseInit & object

Optional Response initialization options (including abortController, durability with its optional batch and batchWaitMs, and debug)

Returns

Response

Response in Server-Sent Events format

Example

typescript
export async function POST(request: Request) {
  const stream = chat({ adapter: openaiText('gpt-5.5'), messages: [...] });
  return toServerSentEventsResponse(stream, { durability: { adapter: memoryStream(request) } });
}
export async function POST(request: Request) {
  const stream = chat({ adapter: openaiText('gpt-5.5'), messages: [...] });
  return toServerSentEventsResponse(stream, { durability: { adapter: memoryStream(request) } });
}