Skip to content

Conversation

@Andrew-Chen-Wang
Copy link

Added a new endpoint for streaming text responses using Hono primitives.

Background

If you wanted to customize with toUIMessageStream and perform other actions after the stream, you would need to use Hono's streamSSE

Summary

Added a new endpoint demonstrating streamSSE

Manual Verification

This is from my own code

Checklist

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • Formatting issues have been fixed (run pnpm prettier-fix in the project root)
  • I have reviewed this pull request (self-review)

Future Work

Related Issues

Added a new endpoint for streaming text responses using Hono primitives.
Comment on lines +45 to +48
return streamSSE(c, async (stream) => {
for await (const part of result.toUIMessageStream({})) {
await stream.writeSSE({ data: JSON.stringify(part) })
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return streamSSE(c, async (stream) => {
for await (const part of result.toUIMessageStream({})) {
await stream.writeSSE({ data: JSON.stringify(part) })
}
c.header('x-vercel-ai-ui-message-stream', 'v1');
return streamSSE(c, async (stream) => {
for await (const part of result.toUIMessageStream({})) {
await stream.writeSSE({ data: JSON.stringify(part) })
}
await stream.writeSSE({ data: '[DONE]' })

The custom SSE streaming implementation is missing the required data: [DONE]\n\n termination message that signals the end of the stream according to the AI SDK's UI Message Stream protocol.

View Details

Analysis

Missing [DONE] termination and required header in Hono streaming example

What fails: The /text-stream endpoint in examples/hono/src/hono-streaming.ts (lines 45-49) implements manual SSE streaming but omits the required data: [DONE]\n\n termination message and x-vercel-ai-ui-message-stream: v1 header, violating the AI SDK UI Message Stream protocol.

How to reproduce:

# Start the Hono server and inspect the /text-stream endpoint response
cd examples/hono && pnpm dev:streaming
# In another terminal:
curl -X POST http://localhost:3001/text-stream
# Stream ends without the required 'data: [DONE]\n\n' marker

Result: The stream sends UI message chunks but never sends the data: [DONE]\n\n termination marker. The response also lacks the x-vercel-ai-ui-message-stream: v1 header. This prevents clients using AI SDK frontend hooks from properly detecting stream completion.

Expected: According to the AI SDK UI Message Stream protocol specification, streams must:

  1. Include the x-vercel-ai-ui-message-stream: v1 header
  2. Terminate with data: [DONE]\n\n after all message chunks

The /chat endpoint correctly uses result.toUIMessageStreamResponse() which handles both requirements via createUIMessageStreamResponse() and JsonToSseTransformStream (see packages/ai/src/ui-message-stream/json-to-sse-transform-stream.ts flush method).

@gr2m gr2m added the documentation Improvements or additions to documentation label Nov 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants