[GH-ISSUE #3034] [Issue]: Non-streaming Anthropic chat() drops stop_reason, so max_tokens truncation is reported as a clean "stop" #1602

Closed
opened 2026-10-21 05:40:31 +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/3034

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

On the non-streaming Anthropic path, a response truncated at the output-token limit is indistinguishable from a naturally completed one. The non-streaming branch of chat() parses content and usage but never reads json.stop_reason, so the returned usage has no finishReason. The base chatComplete() wrapper then falls back to reporting "stop" even when Anthropic actually returned stop_reason: "max_tokens" (or "refusal"/"pause_turn"). Downstream length-aware logic — schedule parsing, agent execution, Professor Mari's "hit the output limit" warning — cannot tell the response was cut off.

How to reproduce (try it yourself)

  1. Configure an Anthropic connection.
  2. Trigger any non-streaming, non-tool chatComplete call — e.g. weekly schedule generation (schedule.service.ts:153 calls provider.chatComplete([...], { model, temperature, maxTokens }) with no stream, no onToken). Base chatComplete computes useStream = options.stream ?? !!options.onToken = false, so it calls this.chat({ ...options, stream:false }), hitting the buggy branch.
  3. Use a small maxTokens with a long generation so the model hits max_tokens.

You'll see: result.finishReason === "stop" despite the content being truncated mid-output; schedule JSON parsing fails with no truncation signal.

Expected: result.finishReason === "length", matching the streaming branch and the tool path.

Where it is

packages/server/src/services/llm/providers/anthropic.provider.ts — the non-streaming branch of chat() (lines 514-536): the inline JSON type omits stop_reason, and the returned usage object (lines 530-534) has no finishReason. Correct handling already exists at line 367 (tool-path chatComplete) and lines 599-600 (streaming branch). Fallback that masks it: base-provider.ts:658. Reachable caller: packages/server/src/services/conversation/schedule.service.ts:153.

What's happening

The non-streaming branch builds usage only from json.usage.input_tokens/output_tokens and never inspects json.stop_reason, so no finishReason is propagated. Every other Anthropic path in the file normalizes stop_reason, making this branch an oversight.

Suggested fix

In the non-streaming branch of chat():

  1. Widen the inline response type to include stop_reason?: string.
  2. Add finishReason to the returned usage object:
if (json.usage) {
  return {
    promptTokens: json.usage.input_tokens,
    completionTokens: json.usage.output_tokens,
    totalTokens: json.usage.input_tokens + json.usage.output_tokens,
    finishReason: normalizeAnthropicFinishReason(json.stop_reason),
  };
}
return;

This matches the in-file precedent (line 367 tool path, line 600 streaming) and resolves the truncation-as-clean-stop bug. Regression avoided: do NOT fabricate a finishReason-only usage object on the json.usage-absent path. Anthropic's Messages API always returns usage on a successful response, so that path is effectively unreachable; returning a zeroed-token LLMUsage there would feed fake 0/0 token counts into agent token accounting (agent-executor.ts:448) and telemetry. Keep the existing bare return; there. normalizeAnthropicFinishReason already accepts string|null|undefined, so an absent stop_reason yields "stop" — identical to today's fallback.

Related: one of a family of provider-level "finish/stop reason dropped → truncation reported as a clean stop" issues filed together (Gemini non-streaming, Anthropic non-streaming, OpenAI streaming, ChatGPT-subscription streaming). Same root-cause class, separate per-provider fix.


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/3034 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** On the non-streaming Anthropic path, a response truncated at the output-token limit is indistinguishable from a naturally completed one. The non-streaming branch of `chat()` parses content and usage but never reads `json.stop_reason`, so the returned usage has no `finishReason`. The base `chatComplete()` wrapper then falls back to reporting `"stop"` even when Anthropic actually returned `stop_reason: "max_tokens"` (or `"refusal"`/`"pause_turn"`). Downstream length-aware logic — schedule parsing, agent execution, Professor Mari's "hit the output limit" warning — cannot tell the response was cut off. **How to reproduce (try it yourself)** 1. Configure an Anthropic connection. 2. Trigger any non-streaming, non-tool `chatComplete` call — e.g. weekly schedule generation (`schedule.service.ts:153` calls `provider.chatComplete([...], { model, temperature, maxTokens })` with no `stream`, no `onToken`). Base `chatComplete` computes `useStream = options.stream ?? !!options.onToken = false`, so it calls `this.chat({ ...options, stream:false })`, hitting the buggy branch. 3. Use a small `maxTokens` with a long generation so the model hits `max_tokens`. You'll see: `result.finishReason === "stop"` despite the content being truncated mid-output; schedule JSON parsing fails with no truncation signal. Expected: `result.finishReason === "length"`, matching the streaming branch and the tool path. **Where it is** `packages/server/src/services/llm/providers/anthropic.provider.ts` — the non-streaming branch of `chat()` (lines 514-536): the inline JSON type omits `stop_reason`, and the returned usage object (lines 530-534) has no `finishReason`. Correct handling already exists at line 367 (tool-path `chatComplete`) and lines 599-600 (streaming branch). Fallback that masks it: `base-provider.ts:658`. Reachable caller: `packages/server/src/services/conversation/schedule.service.ts:153`. **What's happening** The non-streaming branch builds usage only from `json.usage.input_tokens/output_tokens` and never inspects `json.stop_reason`, so no `finishReason` is propagated. Every other Anthropic path in the file normalizes `stop_reason`, making this branch an oversight. **Suggested fix** In the non-streaming branch of `chat()`: 1. Widen the inline response type to include `stop_reason?: string`. 2. Add `finishReason` to the returned usage object: ```ts if (json.usage) { return { promptTokens: json.usage.input_tokens, completionTokens: json.usage.output_tokens, totalTokens: json.usage.input_tokens + json.usage.output_tokens, finishReason: normalizeAnthropicFinishReason(json.stop_reason), }; } return; ``` This matches the in-file precedent (line 367 tool path, line 600 streaming) and resolves the truncation-as-clean-stop bug. **Regression avoided:** do NOT fabricate a `finishReason`-only usage object on the `json.usage`-absent path. Anthropic's Messages API always returns `usage` on a successful response, so that path is effectively unreachable; returning a zeroed-token `LLMUsage` there would feed fake 0/0 token counts into agent token accounting (`agent-executor.ts:448`) and telemetry. Keep the existing bare `return;` there. `normalizeAnthropicFinishReason` already accepts `string|null|undefined`, so an absent `stop_reason` yields `"stop"` — identical to today's fallback. *Related: one of a family of provider-level "finish/stop reason dropped → truncation reported as a clean stop" issues filed together (Gemini non-streaming, Anthropic non-streaming, OpenAI streaming, ChatGPT-subscription streaming). Same root-cause class, separate per-provider fix.* --- *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:4834986800 --> @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:4835086081 --> @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#1602
No description provided.