Skip to content

fix(qwenwork): split tool calls that share an upstream index - #595

Merged
caigee-cmd merged 2 commits into
caigee-cmd:mainfrom
orshih6:contribot/issue-594
Nov 26, 2026
Merged

caigee-cmd merged 2 commits into
caigee-cmd:mainfrom
orshih6:contribot/issue-594

Conversation

@orshih6

@orshih6 orshih6 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Qwen4-Max on QwenWork CN sends every tool call of a turn at index: 0 and tells them apart only by id. The adapter passed the deltas through unchanged, so OpenAI-style consumers merged them by index. The Responses accumulator, and the adapter's own non-stream merge, ended up with one call named cli2api_diag_ambercli2api_diag_violet and arguments {"key": "amber"}{"key": "violet"}. The Responses stream then failed with an invalid-JSON error and never sent response.completed.

This adds a small per-stream toolCallIndexer in internal/providers/qwenwork/sse.go. It rewrites the index of each tool_calls delta before the chunk is relayed (writeOpenAIChunks) or merged (collectUpstream):

  • A delta with an id it hasn't seen starts a new call. That call keeps its upstream index if the index is still free; otherwise it gets the next unused one.
  • A delta without an id continues the latest call at that upstream index.
  • Upstreams that already send distinct indexes keep their indexes as they are.

Tests: two new tests in client_test.go replay the reported pattern (two ids sharing index 0, with arguments split across chunks). One runs through ChatNonStream, the other through ChatStream and folds the relayed deltas by index the way clients do. Both expect two separate calls, at index 0 and 1, each with its own complete arguments. Both fail on main with the merged output from the report and pass with this change. go test passes for ./internal/providers/qwenwork/ and ./internal/gateway/, and go vet reports nothing for the qwenwork package.

Not included: the report also says a forced tool_choice and parallel_tool_calls are not forwarded to QwenWork. I left that out because I can't check which fields the upstream accepts. I also didn't change the gateway's shared Responses accumulator to start a new item when the id changes, because that would affect every provider.

🤖 Generated with Claude Code

Fixes #594


This change was prepared with an AI coding agent (Claude) and opened automatically after an automated review, so no person has read it yet. I'm happy to make any changes you'd like.

Signed-off-by: Бат-Орших <34618353+orshih6@users.noreply.github.com>
@caigee-cmd

Copy link
Copy Markdown
Owner

Reviewed against current main.

The change is scoped to the QwenWork SSE adapter. toolCallIndexer only rewrites an index when a new id arrives on an index that is already taken; upstreams that already send distinct indexes keep them, and a delta with no id continues the latest call at that upstream index. The two new tests replay the Qwen4-Max pattern (two ids sharing index 0, arguments split across chunks) through both the non-stream merge and the streamed relay. No conflict with main; the only thing missing was the unrelated Codex identity bump, which is now included.

Approving and merging.

@caigee-cmd caigee-cmd left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Scoped QwenWork fix. Index rewrite only fires when a new tool-call id lands on an already-used index, so distinct-index upstreams are unchanged. Tests cover the reported Qwen4-Max case on both the stream and non-stream paths.

@caigee-cmd
caigee-cmd merged commit ccd4744 into caigee-cmd:main Nov 26, 2026
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.

QwenWork 的 Qwen4-Max 同轮多工具调用复用 index,导致 Responses 流式报错

2 participants