[GH-ISSUE #3036] [Issue]: Anthropic streaming throws "completed without text" on a valid thinking-only / empty completion, discarding the reply #1607

Closed
opened 2026-10-21 05:40:35 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @kolacheee on GitHub (Jun 29, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3036

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

With extended thinking enabled on an Anthropic connection, a response that spends its whole budget thinking and gets cut off (stop_reason: max_tokens) before emitting any text — or one that hits a stop sequence immediately — is turned into a hard error instead of a (truncated) reply. The streaming generator throws Anthropic stream completed without text (finish reason: ...) whenever no text delta was yielded, even though a thinking-only or empty-but-terminated completion is a legitimate outcome. The captured thinking and token usage are discarded, and the user sees an error rather than a length-truncated turn. The non-streaming branch handles the same empty-text case gracefully (yields "", returns usage), so the two branches disagree.

How to reproduce (try it yourself)

  1. Anthropic connection, a model with extended thinking (e.g. claude-3-7-sonnet or an opus/sonnet-4-x with enableThinking), streaming on.
  2. Set a small visible max_tokens so the budget is consumed by thinking before any text is produced (stop_reason: max_tokens during the thinking block).
  3. Send a prompt.

You'll see: the stream emits message_start, thinking deltas, message_delta(stop_reason=max_tokens), message_stop — but no text delta, so emittedText stays false and the generator throws. The user gets a generation error.

Expected: the (truncated) thinking + token usage + finishReason are returned, like the non-streaming branch, instead of a hard error.

Where it is

packages/server/src/services/llm/providers/anthropic.provider.ts: the message_stop throw at lines 614-617 and the parallel post-loop throw at lines 634-636 (both fire on !emittedText && !options.signal?.aborted). emittedText is set true only on a text delta (lines 609-611); thinking deltas (606-608) never set it. Graceful non-streaming branch: lines 514-537. finishReason capture: lines 599-601. Thinking enablement: lines 463-487.

What's happening

The provider treats "no text was streamed" as a stream failure, but an empty/thinking-only completion is valid (especially when stop_reason indicates length or a stop sequence). The check ignores finishReason and is inconsistent with the non-streaming branch.

Suggested fix

Make the throw finish-reason-AWARE by tracking whether a terminal stop_reason was actually observed, rather than matching finishReason against a value list:

  1. Add a flag next to finishReason (line 557): let sawStopReason = false;
  2. Set it where stop_reason is captured (lines 599-601): inside the message_delta branch, after assigning finishReason, add sawStopReason = true;.
  3. Gate the message_stop throw (615-617) on !emittedText && !sawStopReason && !options.signal?.aborted — a thinking-only/max_tokens/stop_sequence completion then returns usage (with finishReason) gracefully like the non-streaming branch.
  4. Apply the same !sawStopReason guard to the post-loop throw (634-636) — the genuinely-truncated case (no message_stop seen) still throws when no stop_reason ever arrived.

Regression avoided: the literal suggestion — "don't throw when finishReason is length/stop/tool_calls" — would break truncated-stream detection, because finishReason is initialized to "stop" (line 557). A connection that drops mid-response (no message_delta, no message_stop) reaches the post-loop check with finishReason still "stop", so a value-list containing "stop" would suppress that throw and silently turn a failed stream into a clean empty completion. Tracking observation of a stop_reason avoids this. Tool calls never flow through this streaming generator, and the abort and happy-text paths are untouched.


AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.

Originally created by @kolacheee on GitHub (Jun 29, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3036 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** With extended thinking enabled on an Anthropic connection, a response that spends its whole budget thinking and gets cut off (`stop_reason: max_tokens`) before emitting any text — or one that hits a stop sequence immediately — is turned into a hard error instead of a (truncated) reply. The streaming generator throws `Anthropic stream completed without text (finish reason: ...)` whenever no text delta was yielded, even though a thinking-only or empty-but-terminated completion is a legitimate outcome. The captured thinking and token usage are discarded, and the user sees an error rather than a length-truncated turn. The non-streaming branch handles the same empty-text case gracefully (yields `""`, returns usage), so the two branches disagree. **How to reproduce (try it yourself)** 1. Anthropic connection, a model with extended thinking (e.g. claude-3-7-sonnet or an opus/sonnet-4-x with `enableThinking`), streaming on. 2. Set a small visible `max_tokens` so the budget is consumed by thinking before any text is produced (`stop_reason: max_tokens` during the thinking block). 3. Send a prompt. You'll see: the stream emits `message_start`, thinking deltas, `message_delta(stop_reason=max_tokens)`, `message_stop` — but no text delta, so `emittedText` stays false and the generator throws. The user gets a generation error. Expected: the (truncated) thinking + token usage + `finishReason` are returned, like the non-streaming branch, instead of a hard error. **Where it is** `packages/server/src/services/llm/providers/anthropic.provider.ts`: the `message_stop` throw at lines 614-617 and the parallel post-loop throw at lines 634-636 (both fire on `!emittedText && !options.signal?.aborted`). `emittedText` is set true only on a text delta (lines 609-611); thinking deltas (606-608) never set it. Graceful non-streaming branch: lines 514-537. `finishReason` capture: lines 599-601. Thinking enablement: lines 463-487. **What's happening** The provider treats "no text was streamed" as a stream failure, but an empty/thinking-only completion is valid (especially when `stop_reason` indicates length or a stop sequence). The check ignores `finishReason` and is inconsistent with the non-streaming branch. **Suggested fix** Make the throw finish-reason-AWARE by tracking whether a terminal `stop_reason` was actually *observed*, rather than matching `finishReason` against a value list: 1. Add a flag next to `finishReason` (line 557): `let sawStopReason = false;` 2. Set it where `stop_reason` is captured (lines 599-601): inside the `message_delta` branch, after assigning `finishReason`, add `sawStopReason = true;`. 3. Gate the `message_stop` throw (615-617) on `!emittedText && !sawStopReason && !options.signal?.aborted` — a thinking-only/max_tokens/stop_sequence completion then returns usage (with `finishReason`) gracefully like the non-streaming branch. 4. Apply the same `!sawStopReason` guard to the post-loop throw (634-636) — the genuinely-truncated case (no `message_stop` seen) still throws when no `stop_reason` ever arrived. **Regression avoided:** the literal suggestion — "don't throw when `finishReason` is `length`/`stop`/`tool_calls`" — would break truncated-stream detection, because `finishReason` is *initialized* to `"stop"` (line 557). A connection that drops mid-response (no `message_delta`, no `message_stop`) reaches the post-loop check with `finishReason` still `"stop"`, so a value-list containing `"stop"` would suppress that throw and silently turn a failed stream into a clean empty completion. Tracking *observation* of a `stop_reason` avoids this. Tool calls never flow through this streaming generator, and the abort and happy-text paths are untouched. --- *AI-investigated against v2.0.6; cited lines read and confirmed. Please verify before fixing.*
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Claiming this for the current staging sweep in draft PR #3053 (branch codex/issue-sweep-3033-3052).

<!-- gh-comment-id:4834987241 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Claiming this for the current staging sweep in draft PR #3053 (branch `codex/issue-sweep-3033-3052`).
Author
Owner

@SpicyMarinara commented on GitHub (Jun 29, 2026):

Fixed in PR #3053 on branch codex/issue-sweep-3033-3052 (commit bfb1bc76). Closing this issue as addressed; the PR remains open for staging review/merge.

<!-- gh-comment-id:4835086735 --> @SpicyMarinara commented on GitHub (Jun 29, 2026): Fixed in PR #3053 on branch `codex/issue-sweep-3033-3052` (commit bfb1bc76). Closing this issue as addressed; the PR remains open for staging review/merge.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1607
No description provided.