Skip to content

fix(routing/proxy): emit finish_reason: tool_calls when tool call pre… - #2773

Open
crashf wants to merge 2 commits into
mnfst:mainfrom
crashf:fix/chatgpt-adapter-tool-call-finish-reason
Open

fix(routing/proxy): emit finish_reason: tool_calls when tool call pre…#2773
crashf wants to merge 2 commits into
mnfst:mainfrom
crashf:fix/chatgpt-adapter-tool-call-finish-reason

Conversation

@crashf

@crashf crashf commented Aug 27, 2026

Copy link
Copy Markdown

…cedes empty completion

GPT/Codex streaming can emit response.output_item.added with a function_call before response.completed, whose output array is empty. Previously the adapter only checked output for function_call items, producing finish_reason: stop and causing the client to end the turn prematurely.

  • Track sawToolCall in ReasoningStreamState
  • Set sawToolCall = true on response.output_item.added function_call
  • In handleCompletedEvent, use hasFunctionCalls || sawToolCall to decide finish_reason
  • Add regression tests for empty-completion + tool-call, text-only completion, and normal function-call completion

Fixes the "GPT no-respond / stops after one tool call" symptom in streaming mode.


Summary by cubic

Fixes the ChatGPT/Codex streaming adapter emitting finish_reason: stop when a tool call precedes an empty response.completed output, which made clients end the turn before running the tool. The adapter now emits finish_reason: tool_calls when any function_call was streamed, even if the completed event's output is empty.

  • Tracks tool-call presence in the stream transformer state on response.output_item.added events.
  • Uses the tracked state, alongside existing output inspection, to decide finish_reason.
  • Adds regression tests for empty-completion tool calls, text-only completions, and normal function-call completions.

Written for commit f2a798f. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

crashf added 2 commits August 27, 2026 15:47
…cedes empty completion

GPT/Codex streaming can emit `response.output_item.added` with a
`function_call` before `response.completed`, whose `output` array
is empty. Previously the adapter only checked `output` for
function_call items, producing `finish_reason: stop` and causing
the client to end the turn prematurely.

- Track `sawToolCall` in `ReasoningStreamState`
- Set `sawToolCall = true` on `response.output_item.added` function_call
- In `handleCompletedEvent`, use `hasFunctionCalls || sawToolCall`
  to decide `finish_reason`
- Add regression tests for empty-completion + tool-call,
  text-only completion, and normal function-call completion

Fixes the "GPT no-respond / stops after one tool call" symptom
in streaming mode.
@crashf
crashf force-pushed the fix/chatgpt-adapter-tool-call-finish-reason branch from f2a798f to 10436f1 Compare August 27, 2026 15:47
@codecov

codecov Bot commented Aug 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.50%. Comparing base (1431f74) to head (10436f1).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2773   +/-   ##
=======================================
  Coverage   96.50%   96.50%           
=======================================
  Files         604      604           
  Lines       36048    36050    +2     
  Branches    10081    10083    +2     
=======================================
+ Hits        34788    34790    +2     
  Misses        702      702           
  Partials      558      558           
Flag Coverage Δ
backend 95.69% <100.00%> (+<0.01%) ⬆️
frontend 97.36% <ø> (ø)
shared 99.45% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant