[GH-ISSUE #3035] [Issue]: OpenAI streaming chat() drops finishReason — length/content_filter completions recorded as a clean stop #1604

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

Originally assigned to: @SpicyMarinara on GitHub.

What's wrong

For a normal streaming OpenAI/OpenRouter/custom-endpoint chat turn (the default path when chat tools are off), the streaming chat() generator never reads the provider's finish_reason and never attaches finishReason to the usage it returns. So a response truncated by max_tokens (finish_reason: "length"), blocked by content_filter, or an incomplete Responses stream is persisted with finishReason: null — indistinguishable from a clean stop. Any consumer that relies on finishReason (truncation UI indicators, isLengthFinishReason in the Mari workspace agent) gets no signal that the message was cut off or filtered. The tool path and the peer Google/Anthropic streaming generators all handle this correctly; the OpenAI streaming generator is the outlier.

How to reproduce (try it yourself)

  1. Configure an OpenAI-compatible connection (openai/openrouter/custom) and start a normal chat without chat tools enabled (so generate.routes.ts takes the else branch at line 6541 → provider.chat()).
  2. Set Max Output Tokens low enough that the model is truncated mid-sentence (the provider emits SSE chunks, then a chunk with "finish_reason":"length", then a usage chunk, then [DONE]).
  3. Send the prompt.

You'll see: the streamed text returned and the assistant message saved with extra.finishReason = null (generate.routes.ts:7007); the debug log at line 6654 prints finish: N/A. Same for a content_filter stop or a Responses response.incomplete.

Expected: a real finishReason (length) is recorded — as it already is on a Gemini connection in the same flow.

Where it is

packages/server/src/services/llm/providers/openai.provider.ts: non-stream chat() (lines 976-1000, returns extractChatCompletionsUsage with no finish reason); streaming chat() SSE loop (lines 1018-1087, never reads parsed.choices[0].finish_reason, returns bare streamUsage); chatResponses() streaming (lines 1845-1887, response.completed/response.incomplete set no finish reason); and the non-stream chatResponses() return (line 1764). Contrast: google.provider.ts:875, the tool path at lines 1344-1345 / 2114. Consumed at generate.routes.ts:6541-6605 (usage stored at 6595, finishReason never assigned) and persisted at line 7007.

What's happening

The streaming chat() and chatResponses() generators were written to yield text and return only token usage; unlike the tool-path methods and the Google/Anthropic generators, they never thread finish_reason/Responses status into the returned LLMUsage.finishReason. The route's finishReason variable is therefore left undefined on the non-tool streaming path, and base-provider.ts:658 masks a missing value as "stop".

Suggested fix

Thread the provider finish reason into every chat()/chatResponses() return, then have the consumer read it:

  1. Streaming chat() loop (~1018-1087): add let finishReason: string | undefined; next to streamUsage; inside the chunk loop capture const choice0 = parsed.choices[0] as { finish_reason?: string } | undefined; if (choice0?.finish_reason) finishReason = choice0.finish_reason;. Change both if (streamUsage) return streamUsage; sites to return finishReason ? { ...streamUsage, finishReason } : streamUsage;.
  2. Non-stream chat() (line 1000): capture choices[0].finish_reason and attach it to the usage if present.
  3. chatResponses() response.completed (1845-1862): after streamUsage = this.extractResponsesUsage(resp);, add if (streamUsage && resp.status === "incomplete") streamUsage = { ...streamUsage, finishReason: "length" }; (mirrors the tool path at 2113-2114).
  4. Non-stream chatResponses() return (line 1764): map status === "incomplete" → finishReason: "length" (also covers GPT-5/o-series non-streaming truncations, which the literal suggested fix omitted).
  5. Consumer (generate.routes.ts:6594-6595): after if (result.value) usage = result.value; add finishReason = usage?.finishReason ?? finishReason; — additive, matching the tool branch (6400, 6538), and it also makes Google/Anthropic streaming turns report correctly.

Regressions avoided: only attach finishReason when the provider actually reports one (never force ?? "stop" on the streaming path — that would re-mask filtered/truncated turns). The spreads are safe because extractChatCompletionsUsage/extractResponsesUsage build fresh objects with no finishReason field, so token fields can't be clobbered; the consumer reads token fields by name. Edits 2 and 4 complete the fix beyond the literal suggestion (the non-stream Responses return had the identical gap). The minimal, peer-consistent shape attaches finishReason only when usage is present.

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/3035 Originally assigned to: @SpicyMarinara on GitHub. **What's wrong** For a normal streaming OpenAI/OpenRouter/custom-endpoint chat turn (the default path when chat tools are off), the streaming `chat()` generator never reads the provider's `finish_reason` and never attaches `finishReason` to the usage it returns. So a response truncated by `max_tokens` (`finish_reason: "length"`), blocked by `content_filter`, or an incomplete Responses stream is persisted with `finishReason: null` — indistinguishable from a clean `stop`. Any consumer that relies on `finishReason` (truncation UI indicators, `isLengthFinishReason` in the Mari workspace agent) gets no signal that the message was cut off or filtered. The tool path and the peer Google/Anthropic streaming generators all handle this correctly; the OpenAI streaming generator is the outlier. **How to reproduce (try it yourself)** 1. Configure an OpenAI-compatible connection (openai/openrouter/custom) and start a normal chat **without** chat tools enabled (so `generate.routes.ts` takes the `else` branch at line 6541 → `provider.chat()`). 2. Set Max Output Tokens low enough that the model is truncated mid-sentence (the provider emits SSE chunks, then a chunk with `"finish_reason":"length"`, then a usage chunk, then `[DONE]`). 3. Send the prompt. You'll see: the streamed text returned and the assistant message saved with `extra.finishReason = null` (`generate.routes.ts:7007`); the debug log at line 6654 prints `finish: N/A`. Same for a `content_filter` stop or a Responses `response.incomplete`. Expected: a real `finishReason` (`length`) is recorded — as it already is on a Gemini connection in the same flow. **Where it is** `packages/server/src/services/llm/providers/openai.provider.ts`: non-stream `chat()` (lines 976-1000, returns `extractChatCompletionsUsage` with no finish reason); streaming `chat()` SSE loop (lines 1018-1087, never reads `parsed.choices[0].finish_reason`, returns bare `streamUsage`); `chatResponses()` streaming (lines 1845-1887, `response.completed`/`response.incomplete` set no finish reason); and the non-stream `chatResponses()` return (line 1764). Contrast: `google.provider.ts:875`, the tool path at lines 1344-1345 / 2114. Consumed at `generate.routes.ts:6541-6605` (usage stored at 6595, `finishReason` never assigned) and persisted at line 7007. **What's happening** The streaming `chat()` and `chatResponses()` generators were written to yield text and return only token usage; unlike the tool-path methods and the Google/Anthropic generators, they never thread `finish_reason`/Responses `status` into the returned `LLMUsage.finishReason`. The route's `finishReason` variable is therefore left undefined on the non-tool streaming path, and `base-provider.ts:658` masks a missing value as `"stop"`. **Suggested fix** Thread the provider finish reason into every `chat()`/`chatResponses()` return, then have the consumer read it: 1. **Streaming `chat()` loop (~1018-1087):** add `let finishReason: string | undefined;` next to `streamUsage`; inside the chunk loop capture `const choice0 = parsed.choices[0] as { finish_reason?: string } | undefined; if (choice0?.finish_reason) finishReason = choice0.finish_reason;`. Change both `if (streamUsage) return streamUsage;` sites to `return finishReason ? { ...streamUsage, finishReason } : streamUsage;`. 2. **Non-stream `chat()` (line 1000):** capture `choices[0].finish_reason` and attach it to the usage if present. 3. **`chatResponses()` `response.completed` (1845-1862):** after `streamUsage = this.extractResponsesUsage(resp);`, add `if (streamUsage && resp.status === "incomplete") streamUsage = { ...streamUsage, finishReason: "length" };` (mirrors the tool path at 2113-2114). 4. **Non-stream `chatResponses()` return (line 1764):** map `status === "incomplete"` → `finishReason: "length"` (also covers GPT-5/o-series non-streaming truncations, which the literal suggested fix omitted). 5. **Consumer (`generate.routes.ts:6594-6595`):** after `if (result.value) usage = result.value;` add `finishReason = usage?.finishReason ?? finishReason;` — additive, matching the tool branch (6400, 6538), and it also makes Google/Anthropic streaming turns report correctly. **Regressions avoided:** only attach `finishReason` when the provider actually reports one (never force `?? "stop"` on the streaming path — that would re-mask filtered/truncated turns). The spreads are safe because `extractChatCompletionsUsage`/`extractResponsesUsage` build fresh objects with no `finishReason` field, so token fields can't be clobbered; the consumer reads token fields by name. Edits 2 and 4 complete the fix beyond the literal suggestion (the non-stream Responses return had the identical gap). The minimal, peer-consistent shape attaches `finishReason` only when usage is present. *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:4834987028 --> @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:4835086401 --> @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#1604
No description provided.