Repository navigation
fix(relaykit): keep tool calls when merging adjacent assistant messages for Claude - #7921
Conversation
Adjacent assistant messages with string content were merged keeping only the second message's tool calls, so tool results for the first message's calls had no matching tool_use and Claude rejected the request. - concatenate both messages' tool calls in input order - skip the "..." placeholder of a tool-call message in the merged text
|
Note Currently processing new changes in this PR. This may take a few minutes, please wait... ⚙️ Run configuration
📒 Files selected for processing: 2
WalkthroughThe Claude Messages converter now preserves tool calls when it merges adjacent assistant messages with string content. It joins non-placeholder text with spaces in that case and retains the existing text-merging behavior when neither message contains tool calls. Tests cover both cases. ChangesAssistant message merging
Priority: ➖ Normal Estimated code review effort: 2 (Simple) | ~10 minutes Change: Bug fix · Severity of issue fixed: Medium Suggested reviewers: Merge Risk: ⚪ Minimal · up to
|
There was a problem hiding this comment.
🔇 Additional comments: 2
relaykit/relayconvert/internal/oai_chat/to_claude_messages_req_test.go: 1
82-177: LGTM!relaykit/relayconvert/internal/oai_chat/to_claude_messages_req.go: 1
171-171: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value
⚠️ Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.Apply De Morgan's law to the placeholder check.
staticcheckreports QF1001 on this line. The negated compound condition is hard to read. Move the placeholder test into a named variable.♻️ Proposed refactor
- if previousText := lastMessage.StringContent(); previousText != "" && !(previousText == "..." && len(previousToolCalls) > 0) { - texts = append(texts, previousText) - } + previousText := lastMessage.StringContent() + isPlaceholder := previousText == "..." && len(previousToolCalls) > 0 + if previousText != "" && !isPlaceholder { + texts = append(texts, previousText) + }Source: Linters/SAST tools
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Organization UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
fb993c8b-3b00-4915-8d2a-4c4b92802097
📒 Files selected for processing: 2
relaykit/relayconvert/internal/oai_chat/to_claude_messages_req.gorelaykit/relayconvert/internal/oai_chat/to_claude_messages_req_test.go
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
…es for Claude (QuantumNous#7921) * fix(relaykit): keep tool calls when merging adjacent assistant messages Adjacent assistant messages with string content were merged keeping only the second message's tool calls, so tool results for the first message's calls had no matching tool_use and Claude rejected the request. - concatenate both messages' tool calls in input order - skip the "..." placeholder of a tool-call message in the merged text * fix(relaykit): preserve literal ellipses in assistant message merges --------- Co-authored-by: seefs001 <[email protected]> (cherry picked from commit 7837356)
…es for Claude (QuantumNous#7921) * fix(relaykit): keep tool calls when merging adjacent assistant messages Adjacent assistant messages with string content were merged keeping only the second message's tool calls, so tool results for the first message's calls had no matching tool_use and Claude rejected the request. - concatenate both messages' tool calls in input order - skip the "..." placeholder of a tool-call message in the merged text * fix(relaykit): preserve literal ellipses in assistant message merges --------- Co-authored-by: seefs001 <[email protected]> (cherry picked from commit 7837356)
…es for Claude (QuantumNous#7921) * fix(relaykit): keep tool calls when merging adjacent assistant messages Adjacent assistant messages with string content were merged keeping only the second message's tool calls, so tool results for the first message's calls had no matching tool_use and Claude rejected the request. - concatenate both messages' tool calls in input order - skip the "..." placeholder of a tool-call message in the merged text * fix(relaykit): preserve literal ellipses in assistant message merges --------- Co-authored-by: seefs001 <[email protected]>
…es for Claude (QuantumNous#7921) * fix(relaykit): keep tool calls when merging adjacent assistant messages Adjacent assistant messages with string content were merged keeping only the second message's tool calls, so tool results for the first message's calls had no matching tool_use and Claude rejected the request. - concatenate both messages' tool calls in input order - skip the "..." placeholder of a tool-call message in the merged text * fix(relaykit): preserve literal ellipses in assistant message merges --------- Co-authored-by: seefs001 <[email protected]> (cherry picked from commit 7837356)
…es for Claude (QuantumNous#7921) (#128) * fix(relaykit): keep tool calls when merging adjacent assistant messages Adjacent assistant messages with string content were merged keeping only the second message's tool calls, so tool results for the first message's calls had no matching tool_use and Claude rejected the request. - concatenate both messages' tool calls in input order - skip the "..." placeholder of a tool-call message in the merged text * fix(relaykit): preserve literal ellipses in assistant message merges --------- Co-authored-by: Roger Chien <[email protected]> Co-authored-by: seefs001 <[email protected]>
…es for Claude (QuantumNous#7921) * fix(relaykit): keep tool calls when merging adjacent assistant messages Adjacent assistant messages with string content were merged keeping only the second message's tool calls, so tool results for the first message's calls had no matching tool_use and Claude rejected the request. - concatenate both messages' tool calls in input order - skip the "..." placeholder of a tool-call message in the merged text * fix(relaykit): preserve literal ellipses in assistant message merges --------- Co-authored-by: seefs001 <[email protected]>
…es for Claude (QuantumNous#7921) * fix(relaykit): keep tool calls when merging adjacent assistant messages Adjacent assistant messages with string content were merged keeping only the second message's tool calls, so tool results for the first message's calls had no matching tool_use and Claude rejected the request. - concatenate both messages' tool calls in input order - skip the "..." placeholder of a tool-call message in the merged text * fix(relaykit): preserve literal ellipses in assistant message merges --------- Co-authored-by: seefs001 <[email protected]>
Agent
Roger Chien <[email protected]>is not among the historical core developers ingit log, so this is stated here per AGENTS.md.Links
User request
Out of scope — refuse
Open gate — do not open unless all are satisfied
go buildor tests passed: yesKind
Issue facts
tool_calls. The first message'stool_useblocks are dropped while theirtool_resultblocks remain.tool_resultwithout a matchingtool_usein the preceding assistant message (HTTP 400), so the conversation cannot continue.""). How often clients send this shape was not measured.OpenAIChatRequestToClaudeMessageson the request in relaykit: Chat → Claude conversion drops tool_calls when merging adjacent assistant messages #7920 producestool_resultforcall_awith notool_useforcall_a; the same conversation as one assistant message with bothtool_callsconverts correctly.POST /v1/chat/completions,RelayFormatOpenAI→RelayFormatClaude, Claude / AWS / Vertex Claude channels per the code, pass-through off; reproduced at the converter level, not against a server. Billing / Frontend / Deployment: not applicable.Change
In
OpenAIChatRequestToClaudeMessages, when two adjacent same-role messages with string content are merged and either has tool calls:ToolCallsof the merged message = previous message's tool calls followed by the current message's (input order)."..."when that message has tool calls (that"..."is the placeholder the converter puts on an empty tool-call message, not client text). If the result is empty, the existing placeholder logic sets"..."as before.Merging two messages without tool calls keeps the existing
fmt.Sprintf("%s %s", ...)branch, so plain-text output is unchanged.Research
Duplicate / prior art
gh search issues/gh search prs(--repo QuantumNous/new-api):tool_use_id,unexpected tool_use_id,consecutive assistant,adjacent assistant,tool_calls claude merge,tool_result claude,连续 assistant,tool_calls 丢失.toolmessages into one user message; that merge works on main and is not touched here.Docs and code
/en/docs/apiand/en/docs/api/ai-model/chat/openai/createchatcompletionhave nothing on Chat → Claude conversion and no setting for it.relaykit/relayconvert/internal/oai_chat/to_claude_messages_req.go:157-165(main) —fmtMessagecarries only the currentToolCalls; the merge removes the previous message without carrying itsToolCalls.:166-168— empty / null content becomes"...", so a tool-call-only assistant message always looks like string content to the next merge, and its"..."would otherwise end up in the merged text.:246onward — a message withToolCallsis emitted as text +tool_useblocks, so carrying the merged list inToolCallsis enough for both tool calls to reach Claude.tool_use_idis theidof the tool use request the result answers.Alternatives considered
null, but whether every Claude provider (Anthropic, Bedrock, Vertex) accepts it was not verified, and it changes output for requests that work today.Files
relaykit/relayconvert/internal/oai_chat/to_claude_messages_req.go"..."placeholder of a tool-call message in the merged textrelaykit/relayconvert/internal/oai_chat/to_claude_messages_req_test.goTestOpenAIChatRequestToClaudeMessagesMergesAdjacentAssistantToolCalls(3 table cases) to the existing test fileBehavior
call_a/call_b, content""):{"role":"assistant","content":[{"type":"text","text":"... "},{"type":"tool_use","id":"call_b",...}]}{"role":"assistant","content":[{"type":"text","text":"..."},{"type":"tool_use","id":"call_a",...},{"type":"tool_use","id":"call_b",...}]}, identical to the output for one assistant message with bothtool_calls.call_awith contentnull, then plain text"Checking a."): assistant text"... Checking a.", notool_use. After: text"Checking a."plustool_usecall_a."First. Second."before and after.nullsecond content, are still not merged (they are sent as consecutive assistant messages and no tool calls are lost). Not changed here.Verification
cd relaykit && GOWORK=off go test ./relayconvert/internal/oai_chat/ -run TestOpenAIChatRequestToClaudeMessagesMergesAdjacentAssistantToolCalls -count=1 -v: on upstream main603a5ceb5with only the test added, FAIL in 2 of 3 cases.both_assistants_have_tool_calls: tool uses[call_b](expected[call_a call_b]), text"... ".tool_calls_followed_by_plain_text: tool usesnil(expected[call_a]), text"... Checking a.".both_assistants_are_plain_text: PASS. On this branch: all 3 PASS.cd relaykit && GOWORK=off go build ./...succeeds;GOWORK=off go vet ./relayconvert/internal/oai_chat/clean;GOWORK=off go test ./dto/... ./relayconvert/... -count=1all ok.go test ./relay/channel/claude/ ./relay/channel/aws/ ./relay/channel/vertex/ -count=1all ok.gofmt -l relaykit/relayconvert/prints nothing.json.Marshal(result.Messages). Upstream main: assistant has onlytool_usecall_b. This branch:tool_usecall_a,call_b, byte-for-byte the same messages as converting one assistant message with bothtool_calls. With anullsecond content, output is identical on main and this branch (two consecutive assistant messages).to_claude_messages_req_test.go; no new test file.Risks
tool_useblocks, and its text no longer contains the"..."placeholder of the tool-call message. Merges without tool calls take the unchanged branch.Scope check
Summary by CodeRabbit
Bug Fixes
Tests