Skip to content

Add ai-streaming-ndjson example#1488

Open
guuszz wants to merge 1 commit into
vercel:mainfrom
guuszz:feat/ai-streaming-ndjson-example
Open

Add ai-streaming-ndjson example#1488
guuszz wants to merge 1 commit into
vercel:mainfrom
guuszz:feat/ai-streaming-ndjson-example

Conversation

@guuszz
Copy link
Copy Markdown

@guuszz guuszz commented May 28, 2026

What is this?

A new example under solutions/ai-streaming-ndjson that demonstrates progressive token-by-token streaming from a Next.js Route Handler to the browser using NDJSON (newline-delimited JSON) over a ReadableStream.

It fills a gap: most streaming examples use Server-Sent Events, but EventSource is GET-only and locked to text/event-stream. For many real-world cases (AI completions with long prompts, long-running ops with progress, multi-event responses) you want a POST with a body — and NDJSON delivers the same UX with less ceremony.

What's included

  • app/api/stream/route.tsReadableStream writing one JSON event per line with typed events (meta, chunk, done, error). Edge-compatible.
  • app/page.tsx — client component that reads via getReader() + TextDecoder, parses NDJSON line-by-line, and renders chunks progressively with a live progress bar and typing cursor.
  • README.md — explains the why (EventSource limitations), the protocol, server/client snippets, when to use vs WebSockets/SSE, and the X-Accel-Buffering header.

How to test

  1. pnpm install
  2. pnpm dev and open http://localhost:3000
  3. Hit "Start streaming" — words appear one at a time
  4. Or test the endpoint directly:

This example demonstrates progressive token-by-token streaming from a
Next.js Route Handler to the browser using NDJSON (newline-delimited JSON)
over a ReadableStream — a simpler alternative to Server-Sent Events when
you need POST with a body.

What's included:
- app/api/stream/route.ts: ReadableStream writing one JSON event per line
  with custom types (meta, chunk, done, error). Edge-compatible.
- app/page.tsx: client component that reads the stream via getReader() +
  TextDecoder, parses NDJSON line-by-line, and renders chunks progressively
  with a live progress bar and typing cursor effect.
- README.md: explains the why (EventSource doesn't accept POST + body),
  the protocol, server/client snippets, when to use vs WebSockets/SSE,
  and X-Accel-Buffering header notes.

The endpoint accepts { prompt } and echoes it back word-by-word with a
40ms delay to simulate token generation — in a real app, replace the loop
with your LLM SDK's stream iterator (OpenAI/Anthropic/Gemini all have this).

Why this matters: many AI integrations need a POST with a long prompt or
context document, but EventSource is GET-only. Most tutorials show SSE
demos that conveniently fit in a query string. This example fills the
gap with a production-tested pattern.
@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 28, 2026

@guuszz is attempting to deploy a commit to the Vercel Examples Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 28, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
mintlify-docs-rewrite Error Error May 28, 2026 1:48am

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented May 28, 2026

Deployment failed with the following error:

The `vercel.ts` schema validation failed with the following message: `rewrites[0]` missing required property `destination`

Learn More: https://vercel.com/docs/concepts/projects/project-configuration

const tokens = prompt
.split(/(\s+)/)
.filter((t) => t.length > 0)
.map((t, i, arr) => (i < arr.length - 1 && !arr[i + 1].match(/^\s/) ? t + ' ' : t))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.map((t, i, arr) => (i < arr.length - 1 && !arr[i + 1].match(/^\s/) ? t + ' ' : t))

Tokenization logic corrupts whitespace by doubling all spaces in the streamed output.

Fix on Vercel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant