Skip to content
Source
Sign inSign up
TanStack/ai

fix(ai): preserve message roles during assistant reasoning #2262

Mergedjperasmus(GitHub profile, opens in a new tab)merged 2 commits intomain fromrefs/pull/2262/headmerged by AlemTuzlak View on GitHub

jperasmus(GitHub profile, opens in a new tab)commented

A user message that arrives during streamed reasoning can appear as part of the assistant reply. Only assistant-role text starts now claim the assistant reasoning placeholder.

🎯 Changes

  • Keep streamed user and system messages under their own IDs and roles.
  • Test the event sequence in StreamProcessor and through a watching useChat client.
  • Document how message IDs and roles work during streamed reasoning.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr, or these tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.
  • Docs: I updated docs/ for this change, or this change is not user-facing.
  • Changeset: I added a changeset (pnpm changeset), or this PR does not change a published package.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Root cause

Issue. A user or system message appears as assistant text if its start event arrives before the assistant starts its reply.

Cause. handleTextMessageStartEvent can assign the incoming ID to the assistant message created by reasoning. That message retains its assistant role.

Fix. Check the normalized role before assigning the incoming ID. The assistant reply can still take over the reasoning placeholder.

Possible alternatives

  • Insert each complete user message through the client API. Every client that streams follow-ups would need to implement that handling.

Testing

Commands

  • pnpm --filter @tanstack/ai exec vitest run tests/stream-processor.test.ts
  • pnpm --filter @tanstack/ai-e2e test:e2e tests/chat-client-watcher-status.spec.ts

Before the fix (92e8799) — both role assertions received assistant. The browser test could not find the user row.

StreamProcessor: 2 failed
Chromium: expected "Follow-up." in the user row; element not found

With the fix

StreamProcessor: 283 passed
Chromium watcher spec: 2 passed

The core build, type check, lint, and formatting checks also passed. The full local pnpm test:pr run passed on Linux with Node 24.22.1. Nx reported 494 successful tasks, and the declaration-file scan passed. The earlier macOS run timed out in ai-skills and ai-sandbox-local-process. Both suites passed on Linux.

Reproduce

  1. Send RUN_STARTED and REASONING_MESSAGE_CONTENT.
  2. Send a user message with its own ID using TEXT_MESSAGE_START, CONTENT, and END.
  3. Send the assistant reply under a different ID.

Before the fix, the user text is part of an assistant message. With the fix, the user text has its own row. The reasoning and reply share the assistant row. The browser test runs this sequence without a model or credentials.

Risk / rollback

The guard changes which message starts can claim an assistant placeholder. Revert the commit to restore the previous behavior.

Summary by CodeRabbit

  • Bug Fixes

    • Streamed user and system messages now remain separate from assistant reasoning and replies, preserving their roles and text in the transcript.
  • Documentation

    • Clarified how message IDs and roles are represented in streamed chat events.
  • Tests

    • Added coverage for streamed user and system messages, including follow-ups received while the assistant is reasoning.
    1. Pushed22d0ae8

    2. nx-cloud[bot](GitHub profile, opens in a new tab)commented

      View your CI Pipeline Execution ↗ for commit 22d0ae8b53c54806a1a572b1dd56859910dcabfd

      CommandStatusDurationResult
      nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded11m 25sView ↗
      nx run-many --targets=test:coverage --projects=...✅ Succeeded3sView ↗
      nx run-many --targets=build --exclude=examples/...✅ Succeeded1m 51sView ↗

      💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


      ☁️ Nx Cloud last updated this comment at 2026-11-30 09:36:37 UTC

    3. AlemTuzlak approved these changes

    4. AlemTuzlak merged thisf6a2e66

    5. AlemTuzlak closed this

Sign in to join this conversation.