fix: avoid empty assistant frame during streaming#168
Conversation
Wrapping the model stream in `createUIMessageStream({ execute → writer.merge(toUIMessageStream(...)) })` emitted the assistant message's `start` chunk before its first content part, so `useChat` rendered an empty assistant message and the loading indicator flashed (with a scroll jump) on every response.
Stream the model directly with `toUIMessageStream(result.stream)` and persist via its `onEnd` callback instead. Without the wrapper there's no `writer` for the transient `data-chat-title`, so the client refreshes the sidebar title on the first `streaming` transition (the title is generated + persisted server-side before streaming starts).
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe chat streaming route now streams through ChangesChat streaming and title update refactor
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant ChatRoute as chats/[id].post.ts
participant streamText
participant UIStream as toUIMessageStream
participant DB
ChatRoute->>streamText: call with model, messages, tools, abortSignal
streamText-->>ChatRoute: result.stream
ChatRoute->>UIStream: convert result.stream
UIStream-->>ChatRoute: responseMessage
ChatRoute->>DB: insert responseMessage on stream end
Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@server/api/chats/`[id].post.ts:
- Around line 135-141: Skip persisting empty assistant responses by guarding the
message insert in the onEnd handler. In server/api/chats/[id].post.ts, update
the onEnd callback around db.insert(schema.messages) so it only writes when
responseMessage.parts has content, preventing empty assistant frames from being
saved and reloaded; keep the existing insert logic for non-empty assistant
responses.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 3635ff55-5a38-4049-a8f0-e888ff2ec207
📒 Files selected for processing (2)
app/pages/chat/[id].vueserver/api/chats/[id].post.ts
On abort (client disconnects before any content) onEnd fires with an empty responseMessage; persisting it reloaded as an empty assistant frame. Guard the insert on responseMessage.parts.
|
This was an issue I did see but did not know how to describe. Thank you!! |
|
Yes it happened after AI SDK v7 upgrade, took a while to figure out and I only found this workaround. It might be an issue in |
|
@dosstx After some investigation, I'm reverting these changes in favor of this fix in |
Problem
On every message, a loading ("Thinking…") indicator flashed at the end of the streamed response, with a scroll jump.
The chat handler wrapped the model stream in
createUIMessageStream({ execute → writer.merge(toUIMessageStream(...)) }). That wrapper's asyncwriter.mergeread-loop + doublehandleUIMessageStreamFinishpass surfaced the assistant message'sstartchunk before its first content part (start-step/reasoning), souseChatcreated an empty assistant message andUChatMessagesshowed its loading indicator until content arrived — reproducible on every response, amplified by extended thinking latency.Fix
Stream the model directly with
toUIMessageStream(result.stream)(the same shape the Nuxt UI playground uses) and persist via its ownonEndcallback:Without the wrapper there's no
writerfor the transientdata-chat-title, so the client now refreshes the sidebar title on the firststreamingtransition instead — the title is generated and persisted server-side before streaming begins, so it's already available.onDatahandler removed accordingly.No
@nuxt/uichanges needed.Validation
pnpm lintandpnpm typecheckpass. Verified manually: no indicator flash / scroll jump, messages persist, and the title still appears as the first response starts streaming.Summary by CodeRabbit