Skip to content

Solve Claude Code crash issue - #2511

Merged
seefs001 merged 1 commit into
QuantumNous:mainfrom
JerryKwan:issue2499
Dec 24, 2025
Merged

seefs001 merged 1 commit into
QuantumNous:mainfrom
JerryKwan:issue2499

Conversation

@JerryKwan

@JerryKwan JerryKwan commented Dec 24, 2025 •

Copy link
Copy Markdown
Contributor

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

  • Fixed tool call processing to prevent emission of incomplete tool blocks when function names or arguments are missing, improving reliability and reducing unnecessary processing.

✏️ Tip: You can customize this high-level summary in your review settings.

add stricter validation for content_block_start corresponding to
tool call
and fix the crash issue when Claude Code is processing tool call
@coderabbitai

coderabbitai Bot commented Dec 24, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

StreamResponseOpenAI2Claude 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

Cohort / File(s) Summary
Streaming Response Handling
service/convert.go
Added conditional checks in StreamResponseOpenAI2Claude to skip emitting content_block_start when toolCall.Function.Name is empty and to skip emitting content_block_delta when tool arguments length is zero.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐰 A rabbit hops through streaming flows,
With guards that skip where nothing grows—
No empty blocks, just what we need,
Our tool calls sprint at cleaner speed! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning, 1 inconclusive)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
Title check ❓ Inconclusive The title 'Solve Claude Code crash issue' is vague and does not clearly summarize the specific technical change. While it references fixing a crash, it lacks detail about what was actually changed (validation guards for tool calls). Consider a more specific title like 'Add validation guards for tool calls in StreamResponseOpenAI2Claude' to clearly indicate the technical implementation.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
service/convert.go (1)

405-414: Arguments validation looks good.

The guard correctly prevents emitting content_block_delta when 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

📥 Commits

Reviewing files that changed from the base of the PR and between 12555a3 and 31a7962.

📒 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)

Comment thread service/convert.go
Comment on lines +392 to +403
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{}{},
},
})
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

🧩 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.

Comment thread service/convert.go
})
}

info.ClaudeConvertInfo.Index = blockIndex

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 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.go

Repository: 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 -20

Repository: 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.

Comment thread service/convert.go
})
}

if len(toolCall.Function.Arguments) > 0 {

@seefs001 seefs001 Dec 24, 2025 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm guessing if there might be tools that don't have Arguments parameters but are still considered tools.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@seefs001

Copy link
Copy Markdown
Collaborator

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.

@JerryKwan

Copy link
Copy Markdown
Contributor Author

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.
I have done some tests using SGLang + Deepseek / Kimi-K2-Thinking, all of them work as expected. But the pre-modified version caused Claude Code crash.

@seefs001

Copy link
Copy Markdown
Collaborator

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. I have done some tests using SGLang + Deepseek / Kimi-K2-Thinking, all of them work as expected. But the pre-modified version caused Claude Code crash.

Thank you for your thorough research. I will merge it, and it is expected to be included in the next version.

@seefs001
seefs001 merged commit 347f232 into QuantumNous:main Dec 24, 2025
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
salem-2007 added a commit to salem-2007/new-api that referenced this pull request Sep 10, 2026
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 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.

Claude Code crash when processing the response

2 participants