Repository navigation
fix(openai): preserve complete tool calls from clean streams without finish_reason - #98124
Conversation
|
Codex review: needs maintainer review before merge. Reviewed July 7, 2026, 9:11 AM ET / 13:11 UTC. Summary PR surface: Source +67, Tests +342. Total +409 across 2 files. Reproducibility: yes. at source level: current main still strips parsed streamed tool calls when no final finish reason is present, and the canonical issue provides provider logs showing valid Review metrics: 1 noteworthy metric.
Root-cause cluster Members:
Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything. Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Risk before merge
Maintainer options:
Next step before merge
Maintainer decision needed
Security Review detailsBest possible solution: Land the transport-level clean-terminal gate if maintainers accept Do we have a high-confidence way to reproduce the issue? Yes, at source level: current main still strips parsed streamed tool calls when no final finish reason is present, and the canonical issue provides provider logs showing valid Is this the best way to solve the issue? Yes for the code shape: observing raw SSE AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 4e29b2f5ab4b. Label changesLabel changes:
Label justifications:
Evidence reviewedPR surface: Source +67, Tests +342. Total +409 across 2 files. View PR surface stats
What I checked:
Likely related people:
What the crustacean ranks mean
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics. How this review workflow works
Review history (1 earlier review cycle)
|
498abf5 to
3a63ad2
Compare
|
@clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. |
3a63ad2 to
0abfda5
Compare
|
@clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. |
|
@clawsweeper re-review |
d859b92 to
79e2379
Compare
|
@clawsweeper re-review |
|
🦞🧹 I asked ClawSweeper to review this item again. |
…ut finish_reason OpenAI-compatible providers that emit delta.tool_calls during streaming but terminate with data: [DONE] without a final finish_reason chunk (e.g. Evolink DeepSeek V4) have their tool calls silently stripped. Introduce sawNativeToolCallDelta (structured provider intent) and sawStreamDONE (exact SSE data: [DONE] detection via TransformStream). Pass sawStreamDONE as a getter so the live value is read after stream consumption. Promotion requires (sawStopFinishReason || (sawNativeToolCallDelta && sawStreamDONE?.())). SSE parsing uses line-boundary-aware regex; [DONE] inside tool arguments or content does not match. EOF without [DONE] remains fail-closed. DSML still requires sawStopFinishReason. Co-Authored-By: Claude Opus 4.8 <[email protected]> Fixes openclaw#97994
…ion proof Prove sawStreamDONE works through the full transport chain: - Local HTTP server → TransformStream → OpenAI SDK → processOpenAICompletionsStream - [DONE] without finish_reason → promoted to toolUse - EOF without [DONE] → fail-closed (tool calls stripped) Co-Authored-By: Claude Opus 4.8 <[email protected]>
a67699c to
13fd832
Compare
…finish_reason (openclaw#98124) * [AI] fix(openai): preserve native tool calls from clean streams without finish_reason OpenAI-compatible providers that emit delta.tool_calls during streaming but terminate with data: [DONE] without a final finish_reason chunk (e.g. Evolink DeepSeek V4) have their tool calls silently stripped. Introduce sawNativeToolCallDelta (structured provider intent) and sawStreamDONE (exact SSE data: [DONE] detection via TransformStream). Pass sawStreamDONE as a getter so the live value is read after stream consumption. Promotion requires (sawStopFinishReason || (sawNativeToolCallDelta && sawStreamDONE?.())). SSE parsing uses line-boundary-aware regex; [DONE] inside tool arguments or content does not match. EOF without [DONE] remains fail-closed. DSML still requires sawStopFinishReason. Co-Authored-By: Claude Opus 4.8 <[email protected]> Fixes openclaw#97994 * [AI] test(openai): add fetch-wrapper loopback tests for [DONE] detection proof Prove sawStreamDONE works through the full transport chain: - Local HTTP server → TransformStream → OpenAI SDK → processOpenAICompletionsStream - [DONE] without finish_reason → promoted to toolUse - EOF without [DONE] → fail-closed (tool calls stripped) Co-Authored-By: Claude Opus 4.8 <[email protected]> * fix: harden clean SSE terminal detection --------- Co-authored-by: Claude Opus 4.8 <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
What Problem This Solves
OpenAI-compatible providers that emit
delta.tool_callsduring streaming but terminate withdata: [DONE]without a finalfinish_reasonchunk (e.g. Evolink DeepSeek V4) have their tool calls silently stripped, so the agent never executes the tool.processOpenAICompletionsStreampost-loop requiressawStopFinishReasonto be true before promoting silent tool-call-only responses totoolUse. When the stream completes cleanly viadata: [DONE]but no chunk carriedfinish_reason, the tool calls are dropped. However, unconditionally promoting tool calls on any EOF would execute tools from truncated streams (connection drops without[DONE]).sawNativeToolCallDeltais set when structureddelta.tool_callsarrives.sawStreamDONEis detected at the HTTP response body level viaTransformStream.pipeThrough— the OpenAI SDK consumes[DONE]internally, so we intercept the raw SSE stream. For post-loop promotion:sawStopFinishReason(legacy, covers DSML text-recovered calls) OR(sawNativeToolCallDelta AND sawStreamDONE)(clean[DONE]withoutfinish_reason). EOF without[DONE]remains fail-closed.src/agents/openai-transport-stream.ts(addsawNativeToolCallDelta+sawStreamDONE; intercept fetch withTransformStreamto detectdata: [DONE]in raw SSE body; update promotion to(sawStopFinishReason || (sawNativeToolCallDelta && sawStreamDONE))),src/agents/openai-transport-stream.test.ts(update test + add 2 new tests including no-[DONE]fail-closed case)sawStopFinishReason), thehasToolCalls && stopReason !== "toolUse"strip guard,finalizeTransportStream/failTransportStreampathsChange Type (select all)
Scope (select all)
Linked Issue/PR
Motivation
Providers like Evolink's
deepseek-v4-prostream validdelta.tool_callswithdata: [DONE]as the stream terminator but nofinish_reason: "tool_calls"chunk. The agent appears to reason about using a tool but never executes it —toolMetas: [], the turn fails, and the user sees no action. Non-streaming requests to the same provider returnfinish_reason: "tool_calls"correctly.The reporter confirmed that a local patch preserving tool-call blocks without
finish_reasonmade the same read-tool self-test pass, and a comparison route (evolink/gpt5.5) in the same OpenClaw setup did not show the issue.The non-transport
openai-completions.tspath already promotes on(stopReason === "stop" && hasToolCalls && !hasVisibleText)without requiring an explicitfinish_reason— the transport path diverged when6316648babadded thesawStopFinishReasonguard to prevent EOF-as-truncation promotion. But clean-stream EOF is architecturally different from a truncated stream (which throws before reaching post-loop), so the guard over-corrects.Evidence
finish_reasonwere dropped; they are now promoted totoolUse.fix/openai-stream-no-finish-tool-97994data: [DONE]detection correctly observes the stream terminal without false matches on provider content. Normal provider behavior is unchanged.delta.tool_calls, so the promotion path was not exercised via this provider. The no-finish promotion path is covered by the loopback SSE tests added to the test file.Root Cause (if applicable)
processOpenAICompletionsStreaminsrc/agents/openai-transport-stream.ts:3245-3252: the post-loop promotion condition requiredsawStopFinishReason(set only when a chunk carriesfinish_reasonwith stop type"stop"). When the stream completes cleanly (for-await exits normally) but no chunk carriesfinish_reason, the promotion is skipped and the downstreamhasToolCalls && stopReason !== "toolUse"guard strips the parsed tool-call blocks.Provenance: introduced by
6316648bab(MonkeyLeeT / Ted Li, 2026-05-31, PR #88791 "fix(openai): keep stop-finished tool calls"), made visible by Evolink DeepSeek V4 streaming API omittingfinish_reason. Confidence: clear.Regression Test Plan
src/agents/openai-transport-stream.test.tsfinish_reason: "stop"promotion (regression),finish_reason: "tool_calls"promotion (regression),finish_reason: "stop"with visible text strip (regression)hasToolCalls && stopReason !== "toolUse"post-loop strip guard and the!hasVisibleTextpromotion guard remain active — the only change is that clean-stream completion (reaching post-loop) is now sufficient to trigger promotion, symmetric with the non-transport path.User-visible / Behavior Changes
Previously, agents using OpenAI-compatible streaming providers that omit
finish_reasonwould silently skip tool execution. After this fix, those tool calls execute normally. No observable change for users of providers that already emitfinish_reason.Security Impact (required)
finish_reason: "stop"path)Human Verification (required)
Compatibility / Migration
finish_reason-bearing providers are unchangedBest-fix Verdict
sawNativeToolCallDelta+sawStreamDONEgives the post-loop a structured-provider signal gated by a clean SSE terminal. TheTransformStream.pipeThroughinterceptor detectsdata: [DONE]before the OpenAI SDK consumes it, distinguishing clean termination from connection drops. This matches ClawSweeper's recommendation: "[P1] Carry an explicit clean SSE terminal signal so [DONE] without finish_reason promotes tool calls while EOF without [DONE] remains fail-closed."[DONE](connection drop, truncated response) remains fail-closed becausesawStreamDONEis never set. The(sawNativeToolCallDelta && sawStreamDONE)conjunction ensures both conditions: provider signaled tool-use intent AND stream terminated cleanly.!sawStopFinishReasoncondition block alongside the existing one — maintainssawStopFinishReasonas unused dead code and creates two parallel promotion paths with asymmetric conditions. Rejected: less clean, harder to audit.sawStopFinishReasontotrue— too aggressive; changes behavior for all non-finish streams including potential future error paths.sawCleanDonetracker that detectsdata: [DONE]at the SSE level — over-engineered; the for-await normal exit is the architectural equivalent.AI Assistance 🤖
Risks and Mitigations
finishAllToolCallBlocks()runsparseStreamingJsonon partial arguments before promotion; a genuinely truncated tool call would likely fail JSON parse, but the current code does not check for parse success. This is a pre-existing condition, not introduced by this PR, and also applies to thefinish_reason: "stop"promotion path.finish_reasonare unaffected. Providers that omit it now get correct tool execution.finish_reason, the conservative!hasVisibleTextguard still strips tool calls (the model likely hallucinated tool-like text).Fixes #97994