Repository navigation
Solve Claude Code crash issue - #2511
Conversation
add stricter validation for content_block_start corresponding to tool call and fix the crash issue when Claude Code is processing tool call
WalkthroughStreamResponseOpenAI2Claude is modified to add conditional guards that prevent emitting tool content blocks when the tool name is absent or arguments are empty, reducing unnecessary empty block emissions in streaming responses. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (1)
service/convert.go (1)
405-414: Arguments validation looks good.The guard correctly prevents emitting
content_block_deltawhen tool call arguments are empty, which addresses the crash issue mentioned in the PR objectives.Note: Line 253 uses
!= ""for the same check. Consider standardizing to one approach for consistency.
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
service/convert.go
🧰 Additional context used
🧬 Code graph analysis (1)
service/convert.go (1)
dto/claude.go (2)
ClaudeResponse(450-464)ClaudeMediaMessage(18-37)
| if toolCall.Function.Name != "" { | ||
| claudeResponses = append(claudeResponses, &dto.ClaudeResponse{ | ||
| Index: &idx, | ||
| Type: "content_block_start", | ||
| ContentBlock: &dto.ClaudeMediaMessage{ | ||
| Id: toolCall.ID, | ||
| Type: "tool_use", | ||
| Name: toolCall.Function.Name, | ||
| Input: map[string]interface{}{}, | ||
| }, | ||
| }) | ||
| } |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
cat -n service/convert.go | sed -n '230,270p'Repository: QuantumNous/new-api
Length of output: 1618
🏁 Script executed:
cat -n service/convert.go | sed -n '385,420p'Repository: QuantumNous/new-api
Length of output: 1355
Add name validation guard to first chunk tool call handling for consistency.
The subsequent chunk handling at lines 392-403 guards against emitting malformed tool blocks by checking toolCall.Function.Name != "". However, the first chunk handling at lines 241-251 unconditionally emits the content_block_start without this validation, creating an inconsistency. If the Name is empty in the first chunk, a malformed response will be appended without protection.
Apply the same name validation guard to line 241-251 to ensure both code paths safely handle empty tool names.
🤖 Prompt for AI Agents
In service/convert.go around lines 241 to 251, the first-chunk handling emits a
content_block_start for tool calls without checking toolCall.Function.Name,
which can produce a malformed tool block when Name is empty; add the same guard
used later (toolCall.Function.Name != "") so the content_block_start and its
ClaudeMediaMessage are only appended when Name is non-empty, mirroring the
validation at lines 392-403 and preventing emission of incomplete tool blocks.
| }) | ||
| } | ||
|
|
||
| info.ClaudeConvertInfo.Index = blockIndex |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# First, let's look at the code context around line 416 in service/convert.go
cd service && cat -n convert.go | sed -n '380,430p'Repository: QuantumNous/new-api
Length of output: 2076
🏁 Script executed:
#!/bin/bash
# Search for ClaudeConvertInfo.Index usage patterns
rg -nP -C5 'ClaudeConvertInfo\.Index' service/convert.goRepository: QuantumNous/new-api
Length of output: 6029
🏁 Script executed:
#!/bin/bash
# Look for blockIndex usage to understand index assignment logic
rg -nP 'blockIndex' service/convert.go | head -20Repository: QuantumNous/new-api
Length of output: 282
Fix index advancement to only occur when tool call blocks are emitted.
The index is updated to blockIndex at line 416 regardless of whether content blocks were actually emitted in lines 392-414. If a tool call has both empty Name and empty Arguments, the index advances without producing any blocks, creating gaps in the response stream.
Move the index assignment inside a conditional block—only update info.ClaudeConvertInfo.Index = blockIndex when at least one block is appended (i.e., within the if toolCall.Function.Name != "" or if len(toolCall.Function.Arguments) > 0 conditions), or add a check to skip empty tool calls entirely.
🤖 Prompt for AI Agents
In service/convert.go around line 416, the code unconditionally sets
info.ClaudeConvertInfo.Index = blockIndex even when the toolCall produced no
content blocks, causing gaps in the response stream; update the assignment so it
only runs when at least one block was appended — either move the index
assignment inside the branches that emit blocks (inside the if
toolCall.Function.Name != "" and/or if len(toolCall.Function.Arguments) > 0
blocks), or add a check to detect empty toolCall (both Name and Arguments empty)
and skip advancing the index entirely; ensure the index is advanced only after
appending one or more blocks.
| }) | ||
| } | ||
|
|
||
| if len(toolCall.Function.Arguments) > 0 { |
There was a problem hiding this comment.
I'm guessing if there might be tools that don't have Arguments parameters but are still considered tools.
There was a problem hiding this comment.
I think tools without parameters do exist such as "get_current_time". Since there are no parameters, there should be no need to send an additional empty input_json_delta event.
|
Is this modification working well in your scenario? I can use GPT normally with both the modified version and the pre-modified version, so I can't verify it. |
Yes, It works in my scenario. |
Thank you for your thorough research. I will merge it, and it is expected to be included in the next version. |
fix #2499
add stricter validation for content_block_start corresponding to tool call
and fix the crash issue when Claude Code is processing tool call
Summary by CodeRabbit
Bug Fixes
✏️ Tip: You can customize this high-level summary in your review settings.