Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/elysiajs/documentation/llms.txt

Use this file to discover all available pages before exploring further.

Elysia has native support for streaming responses, making it straightforward to integrate with the Vercel AI SDK. You can return ReadableStream, Response, or Server-Sent Event streams directly from your route handlers — Elysia handles the streaming automatically.

Response streaming

Return a ReadableStream from the AI SDK directly from your handler:
import { Elysia } from 'elysia'
import { streamText } from 'ai'
import { openai } from '@ai-sdk/openai'

new Elysia().get('/', () => {
  const stream = streamText({
    model: openai('gpt-4o'),
    system: 'You are a helpful assistant.',
    prompt: 'Hi! How are you doing?'
  })

  // Return a plain ReadableStream
  return stream.textStream

  // UI Message Stream is also supported
  // return stream.toUIMessageStream()
})

Server-Sent Events

Wrap a ReadableStream with the sse function to send each chunk as a Server-Sent Event:
import { Elysia, sse } from 'elysia'
import { streamText } from 'ai'
import { openai } from '@ai-sdk/openai'

new Elysia().get('/', () => {
  const stream = streamText({
    model: openai('gpt-4o'),
    system: 'You are a helpful assistant.',
    prompt: 'Hi! How are you doing?'
  })

  // Each chunk is sent as a Server-Sent Event
  return sse(stream.textStream)

  // UI Message Stream is also supported
  // return sse(stream.toUIMessageStream())
})

Returning a Response directly

If you do not need Eden type safety on the stream, return the response directly from the AI SDK:
import { Elysia } from 'elysia'
import { streamText } from 'ai'
import { openai } from '@ai-sdk/openai'

new Elysia().get('/', () => {
  const stream = streamText({
    model: openai('gpt-4o'),
    system: 'You are a helpful assistant.',
    prompt: 'Hi! How are you doing?'
  })

  return stream.toTextStreamResponse()

  // UI Message Stream Response uses SSE
  // return stream.toUIMessageStreamResponse()
})

Manual streaming with generators

Use an async generator to yield chunks manually for full control over the stream:
import { Elysia, sse } from 'elysia'
import { streamText } from 'ai'
import { openai } from '@ai-sdk/openai'

new Elysia().get('/', async function* () {
  const stream = streamText({
    model: openai('gpt-4o'),
    system: 'You are a helpful assistant.',
    prompt: 'Hi! How are you doing?'
  })

  for await (const data of stream.textStream)
    yield sse({
      data,
      event: 'message'
    })

  yield sse({ event: 'done' })
})

Streaming via fetch

If the AI SDK does not support the model you need, use Elysia’s fetch to proxy a streaming HTTP response directly:
import { Elysia, fetch } from 'elysia'

new Elysia().get('/', () => {
  return fetch('https://api.openai.com/v1/chat/completions', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${process.env.OPENAI_API_KEY}`
    },
    body: JSON.stringify({
      model: 'gpt-4o',
      stream: true,
      messages: [
        { role: 'system', content: 'You are a helpful assistant.' },
        { role: 'user', content: 'Hi! How are you doing?' }
      ]
    })
  })
})
Elysia automatically proxies the fetch response with streaming support.
For additional information, refer to the AI SDK documentation.

Build docs developers (and LLMs) love