mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #3035] [Issue]: OpenAI streaming chat() drops finishReason — length/content_filter completions recorded as a clean stop #1604
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#1604
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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'sfinish_reasonand never attachesfinishReasonto the usage it returns. So a response truncated bymax_tokens(finish_reason: "length"), blocked bycontent_filter, or an incomplete Responses stream is persisted withfinishReason: null— indistinguishable from a cleanstop. Any consumer that relies onfinishReason(truncation UI indicators,isLengthFinishReasonin 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)
generate.routes.tstakes theelsebranch at line 6541 →provider.chat())."finish_reason":"length", then a usage chunk, then[DONE]).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 printsfinish: N/A. Same for acontent_filterstop or a Responsesresponse.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-streamchat()(lines 976-1000, returnsextractChatCompletionsUsagewith no finish reason); streamingchat()SSE loop (lines 1018-1087, never readsparsed.choices[0].finish_reason, returns barestreamUsage);chatResponses()streaming (lines 1845-1887,response.completed/response.incompleteset no finish reason); and the non-streamchatResponses()return (line 1764). Contrast:google.provider.ts:875, the tool path at lines 1344-1345 / 2114. Consumed atgenerate.routes.ts:6541-6605(usage stored at 6595,finishReasonnever assigned) and persisted at line 7007.What's happening
The streaming
chat()andchatResponses()generators were written to yield text and return only token usage; unlike the tool-path methods and the Google/Anthropic generators, they never threadfinish_reason/Responsesstatusinto the returnedLLMUsage.finishReason. The route'sfinishReasonvariable is therefore left undefined on the non-tool streaming path, andbase-provider.ts:658masks a missing value as"stop".Suggested fix
Thread the provider finish reason into every
chat()/chatResponses()return, then have the consumer read it:chat()loop (~1018-1087): addlet finishReason: string | undefined;next tostreamUsage; inside the chunk loop captureconst choice0 = parsed.choices[0] as { finish_reason?: string } | undefined; if (choice0?.finish_reason) finishReason = choice0.finish_reason;. Change bothif (streamUsage) return streamUsage;sites toreturn finishReason ? { ...streamUsage, finishReason } : streamUsage;.chat()(line 1000): capturechoices[0].finish_reasonand attach it to the usage if present.chatResponses()response.completed(1845-1862): afterstreamUsage = this.extractResponsesUsage(resp);, addif (streamUsage && resp.status === "incomplete") streamUsage = { ...streamUsage, finishReason: "length" };(mirrors the tool path at 2113-2114).chatResponses()return (line 1764): mapstatus === "incomplete"→finishReason: "length"(also covers GPT-5/o-series non-streaming truncations, which the literal suggested fix omitted).generate.routes.ts:6594-6595): afterif (result.value) usage = result.value;addfinishReason = usage?.finishReason ?? finishReason;— additive, matching the tool branch (6400, 6538), and it also makes Google/Anthropic streaming turns report correctly.Regressions avoided: only attach
finishReasonwhen the provider actually reports one (never force?? "stop"on the streaming path — that would re-mask filtered/truncated turns). The spreads are safe becauseextractChatCompletionsUsage/extractResponsesUsagebuild fresh objects with nofinishReasonfield, 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 attachesfinishReasononly 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.
@SpicyMarinara commented on GitHub (Jun 29, 2026):
Claiming this for the current staging sweep in draft PR #3053 (branch
codex/issue-sweep-3033-3052).@SpicyMarinara commented on GitHub (Jun 29, 2026):
Fixed in PR #3053 on branch
codex/issue-sweep-3033-3052(commitbfb1bc76). Closing this issue as addressed; the PR remains open for staging review/merge.bsakaguchi referenced this issue2026-10-24 04:10:39 +07:00