Skip to content

Claude streaming output sometimes truncated at end #6382

Closed

Description

@CasualDeveloper

Summary

Intermittently, Claude-based models stream responses that are missing the last few characters/words. The stream ends cleanly (no visible error), but the final text delta never arrives.

This appears isolated to Claude providers; OpenAI / Gemini models do not seem to be affected.

Environment

  • OpenCode: v1.0.207 (release commit 76880dce0)
  • Runtime: Bun 1.3.5
  • Claude via AI SDK: @ai-sdk/[email protected]

Reproduction

  1. Configure any Claude model/provider.
  2. Run a prompt with a strict sentinel at the end (repeat ~10 times):
Write 200 lines numbered 001–200.
Then write a final line exactly:
SENTINEL:'`’()[]{}<>✓Ω漢END
Do not output anything after END.

Expected

The final sentinel line always appears intact.

Actual

Occasionally the output ends early (mid-word / missing last characters). Examples include the final line being cut, e.g. SENTINEL:'\’`.

Investigation / suspected root cause

OpenCode consumes streamText().fullStream and appends each text-delta into the message; the TUI batches updates but flushes after stream end. This suggests OpenCode is not dropping the final chunk in its own aggregation/rendering.

OpenCode v1.0.207 pins @ai-sdk/anthropic to 2.0.50. In that version, the provider's doStream() uses ReadableStream.tee() and an un-awaited async IIFE to pull/cancel a "first chunk" branch while returning a delayed stream result. In some runtimes, canceling one tee branch while the other is still being consumed can prematurely end/disturb the underlying source, which can drop tail chunks. That would present exactly as "end-of-response truncation" with no explicit error.

@ai-sdk/[email protected] rewrites the first-chunk pulling logic to be synchronous/awaited and returns the consumer stream directly (no async IIFE / no DelayedPromise), which should remove this race.

Proposed fix (minimal)

Bump deps (staying within v2):

  • @ai-sdk/anthropic: 2.0.50 → 2.0.56
  • @ai-sdk/provider-utils: 3.0.18 → 3.0.19 (required by 2.0.56)

Patch:

diff --git a/packages/opencode/package.json b/packages/opencode/package.json
@@
-    "@ai-sdk/anthropic": "2.0.50",
+    "@ai-sdk/anthropic": "2.0.56",
@@
-    "@ai-sdk/provider-utils": "3.0.18",
+    "@ai-sdk/provider-utils": "3.0.19",

(and regenerate bun.lock)

I have this change ready on a branch and can open a PR if that’s the preferred workflow.

Activity

  1. github-actions commented on Dec 29, 2025

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

    Your detailed investigation into the @ai-sdk/anthropic version and the ReadableStream.tee() race condition is valuable and provides a concrete fix path that may also address these other reported streaming truncation issues.

    Feel free to ignore if your specific case involves different root causes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions