[GH-ISSUE #3033] [Issue]: Non-streaming Gemini path drops finishReason, so MAX_TOKENS truncation is reported as a clean "stop" #1603

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/3033

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

With a Gemini connection that has thinking/reasoning enabled (the mainstream config for gemini-2.5/3), a response truncated by the output-token limit is reported to the rest of the app as a clean completion. The non-streaming branch of GoogleProvider.chat() returns token usage but never includes the finishReason, even though Gemini's MAX_TOKENS value is sitting right there in the candidate. As a result, two genuinely helpful user messages never fire: the game-setup "World generation response was cut off — increase max output tokens" message, and the Professor Mari "hit the model output limit, ask her to continue" warning. The user just sees a truncated or garbled response, or a generic JSON parse error.

How to reproduce (try it yourself)

  1. Configure a Google (Gemini) connection with a gemini-2.5/3 model and reasoningEffort/enableThinking set. (Thinking forces the non-streaming generateContent path via useStreaming = options.stream && !thinkingConfig at line 633. Alternatively call any non-tool generation with stream:false.)
  2. Set Max Output Tokens low and send a prompt that produces more output than that, so Gemini returns finishReason: "MAX_TOKENS".
  3. Run it through game world generation (or Professor Mari).

You'll see: the partial text returned, recorded as a normal completion (finishReason persisted as null), and the explicit truncation message never shown.

Expected: truncation surfaces as finishReason: "length", and the "response was cut off / hit the output limit" message fires — matching a streaming Gemini turn, which already does this.

Where it is

packages/server/src/services/llm/providers/google.provider.ts — non-streaming branch of chat(), the if (json.usageMetadata) return block at lines 752-758 (the candidate with its finishReason is in scope at line 735 but never read). Contrast: streaming return at line 875 (return { ...streamUsage, finishReason: normalizeGeminiFinishReason(lastFinishReason) }) and tools path at line 583. Helper normalizeGeminiFinishReason (lines 445-450, maps MAX_TOKENS→"length"). Fallback that masks the missing reason: base-provider.ts:658 (usage?.finishReason ?? "stop"). Consumers: game.routes.ts:1638-1643 + 4158; professor-mari/workspace-agent.service.ts:579-581 + 1317.

What's happening

The non-streaming branch was written to return only token counts and never threads candidate.finishReason into the returned LLMUsage, even though it has the candidate in scope. assertGeminiUsableResponse also does not flag MAX_TOKENS when there is visible text (geminiFinishReasonError returns null for that case), so nothing else catches it. BaseLLMProvider.chatComplete then computes finishReason: usage?.finishReason ?? "stop", surfacing the truncation as a clean stop.

Suggested fix

In the non-streaming return block (lines 752-758), add the finish reason, mirroring the streaming path:

if (json.usageMetadata) {
  return {
    promptTokens: json.usageMetadata.promptTokenCount,
    completionTokens: json.usageMetadata.candidatesTokenCount,
    totalTokens: json.usageMetadata.totalTokenCount,
    completionReasoningTokens: json.usageMetadata.thoughtsTokenCount,
    finishReason: normalizeGeminiFinishReason(candidate?.finishReason),
  };
}
return;

candidate is already in scope (line 735), normalizeGeminiFinishReason already exists (line 445), and LLMUsage.finishReason is a valid optional field. Regression avoided: do NOT also fabricate a zeroed usage object in the no-usage return; case (line 760). The live Gemini API always returns usageMetadata on a MAX_TOKENS truncation, so that path is effectively unreachable; returning {promptTokens:0,...} there would persist fabricated 0-token counts (generate.routes.ts:6998-7005 stores usage?.promptTokens ?? null) for a response that actually produced text, and would make this path the lone outlier versus the streaming/OpenAI convention of returning undefined when no usage exists. Touch only the if (json.usageMetadata) block.

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/3033 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** With a Gemini connection that has thinking/reasoning enabled (the mainstream config for gemini-2.5/3), a response truncated by the output-token limit is reported to the rest of the app as a clean completion. The non-streaming branch of `GoogleProvider.chat()` returns token usage but never includes the `finishReason`, even though Gemini's `MAX_TOKENS` value is sitting right there in the candidate. As a result, two genuinely helpful user messages never fire: the game-setup "World generation response was cut off — increase max output tokens" message, and the Professor Mari "hit the model output limit, ask her to continue" warning. The user just sees a truncated or garbled response, or a generic JSON parse error. **How to reproduce (try it yourself)** 1. Configure a Google (Gemini) connection with a gemini-2.5/3 model and reasoningEffort/enableThinking set. (Thinking forces the non-streaming generateContent path via `useStreaming = options.stream && !thinkingConfig` at line 633. Alternatively call any non-tool generation with `stream:false`.) 2. Set Max Output Tokens low and send a prompt that produces more output than that, so Gemini returns `finishReason: "MAX_TOKENS"`. 3. Run it through game world generation (or Professor Mari). You'll see: the partial text returned, recorded as a normal completion (`finishReason` persisted as null), and the explicit truncation message never shown. Expected: truncation surfaces as `finishReason: "length"`, and the "response was cut off / hit the output limit" message fires — matching a streaming Gemini turn, which already does this. **Where it is** `packages/server/src/services/llm/providers/google.provider.ts` — non-streaming branch of `chat()`, the `if (json.usageMetadata)` return block at lines 752-758 (the `candidate` with its `finishReason` is in scope at line 735 but never read). Contrast: streaming return at line 875 (`return { ...streamUsage, finishReason: normalizeGeminiFinishReason(lastFinishReason) }`) and tools path at line 583. Helper `normalizeGeminiFinishReason` (lines 445-450, maps `MAX_TOKENS`→`"length"`). Fallback that masks the missing reason: `base-provider.ts:658` (`usage?.finishReason ?? "stop"`). Consumers: `game.routes.ts:1638-1643` + `4158`; `professor-mari/workspace-agent.service.ts:579-581` + `1317`. **What's happening** The non-streaming branch was written to return only token counts and never threads `candidate.finishReason` into the returned `LLMUsage`, even though it has the candidate in scope. `assertGeminiUsableResponse` also does not flag `MAX_TOKENS` when there is visible text (`geminiFinishReasonError` returns null for that case), so nothing else catches it. `BaseLLMProvider.chatComplete` then computes `finishReason: usage?.finishReason ?? "stop"`, surfacing the truncation as a clean stop. **Suggested fix** In the non-streaming return block (lines 752-758), add the finish reason, mirroring the streaming path: ```ts if (json.usageMetadata) { return { promptTokens: json.usageMetadata.promptTokenCount, completionTokens: json.usageMetadata.candidatesTokenCount, totalTokens: json.usageMetadata.totalTokenCount, completionReasoningTokens: json.usageMetadata.thoughtsTokenCount, finishReason: normalizeGeminiFinishReason(candidate?.finishReason), }; } return; ``` `candidate` is already in scope (line 735), `normalizeGeminiFinishReason` already exists (line 445), and `LLMUsage.finishReason` is a valid optional field. **Regression avoided:** do NOT also fabricate a zeroed usage object in the no-usage `return;` case (line 760). The live Gemini API always returns `usageMetadata` on a `MAX_TOKENS` truncation, so that path is effectively unreachable; returning `{promptTokens:0,...}` there would persist fabricated 0-token counts (`generate.routes.ts:6998-7005` stores `usage?.promptTokens ?? null`) for a response that actually produced text, and would make this path the lone outlier versus the streaming/OpenAI convention of returning undefined when no usage exists. Touch only the `if (json.usageMetadata)` block. *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:4834986562 --> @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:4835085789 --> @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#1603
No description provided.