fix(ai): omit subagent text from non-streaming chat results #2300
main fromrefs/pull/2300/head·opened View on GitHubFixes #2297
🎯 Changes#
streamToText joined every TEXT_MESSAGE_CONTENT delta into text. A child chunk is tagged with subagentRunId before the parent stream re-emits it (attributeChunk). chat({ stream: false }) reads that stream through runNonStreamingText → streamToText, so the parent text includes the child text. Concurrent children interleave. A child that returns a string is appended whole.
streamToText now skips a TEXT_MESSAGE_CONTENT chunk whose subagentRunId is a non-empty string. The chunk stays in chunks. The child runner already counts only that child's own deltas the same way.
Filtering only inside runNonStreamingText would leave a direct streamToText call on the same stream wrong. The join is the one place both paths share.
No new API. A patch changeset is included. docs/chat/subagents-advanced.md states that parent text is the parent reply. The reference pages for ChatResult.text and streamToText match the updated comments.
Test plan#
packages/ai/tests/stream-false-subagent-text.test.ts (3 tests):
streamToTextdrops a delta tagged withsubagentRunIdand keeps the chunk.chat({ stream: false })with a streaming child returnsDelegating. All done.The child deltaReport.stays inchunks.- The same call with a child that returns a string leaves
Report for task 1.out oftext.
On current main those three fail (Delegating. Report.All done. and Delegating. Report for task 1.All done.). With this change they pass. packages/ai/tests/stream-to-response.test.ts also passes (56 tests across the two files).
✅ 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).
This change was prepared with the assistance of an AI coding agent (Claude).
Summary by CodeRabbit#
- Bug Fixes
- Parent chat text now includes only the parent’s reply, excluding text generated by subagents. Subagent text remains available in the chat’s chunks and tool results.
streamToTextfollows the same behavior while retaining the full set of chunks.
- Documentation
- Clarified how parent text, subagent text, and chunks are represented in chat results.
- 1
pcbeingused33(GitHub profile, opens in a new tab) linked this to fix #2297
tanstack-source added labelsbug
No actionable comments were generated in the recent review.
Walkthrough
-
streamToTextnow excludes child text marked with a nonemptysubagentRunIdfrom the parent’stext. Child chunks remain inchunks. Tests cover streaming and string-returning subagents, and documentation describes the distinction.Changes: Parent text aggregation
Layer / File(s) Summary Filter child text and validate result behavior
packages/ai/src/stream-to-response.ts,packages/ai/tests/stream-false-subagent-text.test.ts,docs/chat/subagents-advanced.md,docs/reference/functions/streamToText.md,docs/reference/interfaces/ChatResult.md,.changeset/omit-subagent-text-from-parent.mdstreamToTextexcludes child text chunks with a nonemptysubagentRunIdfromtextand retains them inchunks. Tests cover streaming and string-returning subagents. Documentation and the changeset describe this behavior.Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers:
alemtuzlak,jherr,tombeckenham
Merge Risk:
Minimal · up tod7110-
Non-streaming chat results will now contain only the parent's reply, with subagent text kept in the chunks. The change is narrow, covered by targeted tests, and easy to revert, so merge risk is minimal.
Pre-merge checks:
4-
Check name Status Explanation Title check The title names the main change: non-streaming chat results (chat with stream: false) will omit subagent text. It uses the fix(ai) convention and matches the changeset, the streamToText filter, and th… Description check The description follows the template. It has the Changes section with the cause, the fix, and the reason for the location of the fix. It includes a test plan with three named tests and reported result… Linked Issues check [ #2297] The issue requires thatchat({ stream: false, subagents })leave child text out of the parentresult.textwhile child chunks stay available. The diff at head changesstreamToTextin pack…Out of Scope Changes check All changes relate to #2297. ThestreamToTextfilter and the matching comment updates in packages/ai/src/stream-to-response.ts implement the requirement. The new test file packages/ai/tests/stream-f…Full details: Title check
-
Explanation
The title names the main change: non-streaming chat results (chat with stream: false) will omit subagent text. It uses the fix(ai) convention and matches the changeset, the streamToText filter, and the linked issue
#2297. The title is concise and specific.
Full details: Description check
-
Explanation
The description follows the template. It has the Changes section with the cause, the fix, and the reason for the location of the fix. It includes a test plan with three named tests and reported results. Checklist items are marked, including docs and changeset. One checklist item is unchecked: the author did not run pnpm run test:pr and left the box empty. The template allows this only when the tests do not apply, and they do apply here. The author should run that command or explain why it was not run.
Full details: Linked Issues check
-
Explanation
[
#2297] The issue requires thatchat({ stream: false, subagents })leave child text out of the parentresult.textwhile child chunks stay available. The diff at head changesstreamToTextin packages/ai/src/stream-to-response.ts. It skips aTEXT_MESSAGE_CONTENTchunk whensubagentRunIdis a non-empty string. The chunk is still pushed tochunksbefore the check.runNonStreamingTextusesstreamToText, so the filter covers the streaming-child path and the string-child path, which both reach the parent stream as tagged chunks. The new tests cover a directstreamToTextcall, a streaming child (Delegating. All done., withReport.kept inchunks), and a string child (Report for task 1.excluded fromtext). This matches the issue's suggested fix. The issue also asks whethertextshould include tool-calling preamble text and whetherChatResultshould expose the final answer separately. The issue calls these a question and a suggestion, not coding requirements, and the PR description states that parenttextis the parent reply. These points do not fail this assessment. Assessment is based on source diff and test file content; I did not execute the test suite.
Full details: Out of Scope Changes check
-
Explanation
All changes relate to
#2297. ThestreamToTextfilter and the matching comment updates in packages/ai/src/stream-to-response.ts implement the requirement. The new test file packages/ai/tests/stream-false-subagent-text.test.ts supports that requirement. The changeset and the docs updates (docs/chat/subagents-advanced.md, thestreamToTextandChatResult.textreference pages) describe the changedtextbehavior, which supports the change and is not unrelated. The diff shows no unrelated code changes. The reference page edits are documentation of the same behavior.
-
Finishing Touches
-
Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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.
Recent review info-
Run configuration-
- Source mode: Auto
- Repository: TanStack/ai
- Branch: main
- File: .coderabbit.yaml
- Commit: 840ea56
- Layers: Repository YAML
- Review profile: CHILL
- Plan: Advanced
- Run ID:
e505c0e3-6c17-43a4-980c-cee2f1ae2ee8
Commits-
Reviewing files that changed from the base of the PR and between 840ea56621f4c32de4359849a0beaa343db60fe3 and d711001bd91a1edd4832c209272eed58564455da.
Files selected for processing: 6-
.changeset/omit-subagent-text-from-parent.mddocs/chat/subagents-advanced.mddocs/reference/functions/streamToText.mddocs/reference/interfaces/ChatResult.mdpackages/ai/src/stream-to-response.tspackages/ai/tests/stream-false-subagent-text.test.ts
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
-
-
github-actions[bot](GitHub profile, opens in a new tab) assignedtombeckenham
Thanks for the PR, @pcbeingused33! 🙌 @tombeckenham will take a look.
Automated pre-review checks
- ✅ CI passing
- ✅ No merge conflicts
- ✅ Changeset present
- ⚠️ No E2E test changes detected — behavior changes need coverage under
testing/e2e/(see CONTRIBUTING)
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: d711001bd91a1edd4832c209272eed58564455da
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 2 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR