Skip to content
Source
Sign inSign up
TanStack/ai

fix(ai): omit subagent text from non-streaming chat results #2300

Openpcbeingused33(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/2300/headopened View on GitHub

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

Fixes #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):

  • streamToText drops a delta tagged with subagentRunId and keeps the chunk.
  • chat({ stream: false }) with a streaming child returns Delegating. All done. The child delta Report. stays in chunks.
  • The same call with a child that returns a string leaves Report for task 1. out of text.

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.
    • streamToText follows 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. tanstack-source added labelsbug

    2. 🦋 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
      NameType
      @tanstack/aiPatch
      @tanstack/ai-optimize-evalsPatch

      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

    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Explain Change →

      No actionable comments were generated in the recent review.

      Walkthrough

      streamToText now excludes child text marked with a nonempty subagentRunId from the parent’s text. Child chunks remain in chunks. 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.md
      streamToText excludes child text chunks with a nonempty subagentRunId from text and retains them in chunks. 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 to d7110

      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: Passed 4

      Check nameStatusExplanation
      Title checkPassedThe 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 checkPassedThe 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 checkPassed[#2297] The issue requires that chat({ stream: false, subagents }) leave child text out of the parent result.text while child chunks stay available. The diff at head changes streamToText in pack…
      Out of Scope Changes checkPassedAll changes relate to #2297. The streamToText filter 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 that chat({ stream: false, subagents }) leave child text out of the parent result.text while child chunks stay available. The diff at head changes streamToText in packages/ai/src/stream-to-response.ts. It skips a TEXT_MESSAGE_CONTENT chunk when subagentRunId is a non-empty string. The chunk is still pushed to chunks before the check. runNonStreamingText uses streamToText, 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 direct streamToText call, a streaming child (Delegating. All done., with Report. kept in chunks), and a string child (Report for task 1. excluded from text). This matches the issue's suggested fix. The issue also asks whether text should include tool-calling preamble text and whether ChatResult should expose the final answer separately. The issue calls these a question and a suggestion, not coding requirements, and the PR description states that parent text is 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. The streamToText filter 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, the streamToText and ChatResult.text reference pages) describe the changed text behavior, 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.

      Share

      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.md
      • docs/chat/subagents-advanced.md
      • docs/reference/functions/streamToText.md
      • docs/reference/interfaces/ChatResult.md
      • packages/ai/src/stream-to-response.ts
      • packages/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.

    4. 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)
      Automated triage — a human review follows.
    5. github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer

Sign in to join this conversation.