Repository navigation
fix(core): array textStream/elementStream no longer keep stale partial elements - #25278
Conversation
…ementStream Token-level providers can complete an array element in place across chunks. The streams emitted each element as soon as its index appeared, so they kept stale partial elements that diverged from the final object. Emit an element only once a later element or stream end proves it complete. Fixes #25208 Co-Authored-By: mastra-platform[bot] <284800079+mastra-platform[bot]@users.noreply.github.com>
PR triageLinked issue check skipped for excluded profile @mastra-platform[bot]. Changed test gateChanged Test Gate is pending. The |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
1 Skipped Deployment
|
🦋 Changeset detectedLatest commit: d284118 The changes in this PR will be included in the next version bump. This PR includes changesets to release 26 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 |
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository: mastra-ai/mastra/.coderabbit.yaml Review profile: ASSERTIVE Plan: Essentials Run ID: 📒 Files selected for processing (5)
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 2 remain after this review. WalkthroughArray structured output streams now publish array elements after later chunks complete them. The text stream emits valid JSON that includes completed elements, and the element stream publishes those elements at completion. ChangesArray structured output streams
Priority: ➖ Normal Estimated code review effort: 3 (Moderate) | ~20 minutes Change: Bug fix · Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to Array elements are held until complete, and no actionable merge-blocking risk remains after normal checks. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning Some tools did not complete. Review the errors below. 🔧 ESLint
packages/core/src/stream/base/output-format-handlers.test.tsESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox. packages/core/src/stream/base/output-format-handlers.tsESLint skipped: the matched ESLint configuration already failed (missing-dependency). packages/core/src/stream/base/output.test.tsESLint skipped: the matched ESLint configuration already failed (missing-dependency).
Comment |
|
Verdict: approve Reviewed head: Findings
Approach: this matches the design I wrote before reading the diff: hold back the trailing element until a later element or the end of the stream. It adds no flags or config. Issue and intent: closes #25208 (bug, Verification
Existing review disposition: CodeRabbit completed with no inline findings, and there are no human reviews. The bot comments (triage, changeset, Vercel) are informational. Adversarial check: the strongest case against is that Assumptions: a delay of one element is an acceptable latency cost for correctness. Open questions: none. Review runtime: anthropic/claude-opus-5-5, reasoning setting: low. |
Summary
When an agent streams structured output with an array schema and the provider streams token by token, the partial JSON parser can yield the last element before it's complete (e.g.
{a:1}). A later chunk then completes that same element ({a:1,b:2}).textStreamandelementStreamonly wrote out elements at new indices, so they kept the incomplete version and didn't match the finalobject.Both streams now hold back the last element of each chunk until a following element or the end of the stream shows it's complete. Elements are always taken from the latest chunk. The earlier #18758 behaviour stays the same: a single chunk is still written as one closed JSON string, empty arrays come out as
[], and the text is always valid JSON.The trade-off is that each element arrives one element later than before.
Fixes #25208 (the fix follows the approach in #25138).
Changes
packages/core/src/stream/base/output-format-handlers.ts:createJsonTextStreamTransformerholds back the last element and writes it on flush.packages/core/src/stream/base/output.ts:elementStreamholds back the last element and publishes it on flush.Test plan
MastraModelOutputtests (textStream and elementStream). All 4 fail without the fix and pass with it.vitest run src/stream: 346 tests pass.pnpm --filter ./packages/core check(tsc) passes.ELI5
When an array item arrives in pieces, the stream waits before sharing the newest item. This prevents consumers from receiving an unfinished item when later chunks add its missing fields.
Changes
textStreamto track the latest array and hold back its trailing element until a later element or stream end.elementStreamto hold back its trailing element and publish remaining elements at stream end.@mastra/core.This delays publication of each trailing element. Test execution results were not provided.