Skip to content

fix: preserve Claude tool block indexes - #5077

Closed
lxil520 wants to merge 1 commit into
QuantumNous:mainfrom
lxil520:fix/5018-claude-tool-index
Closed

fix: preserve Claude tool block indexes#5077
lxil520 wants to merge 1 commit into
QuantumNous:mainfrom
lxil520:fix/5018-claude-tool-index

Conversation

@lxil520

@lxil520 lxil520 commented May 24, 2026

Copy link
Copy Markdown

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

Anthropic content block indexes identify which stream block a tool_use or input_json_delta belongs to. The OpenAI-compatible stream response previously subtracted one and clamped to zero, so Claude tool blocks with indexes 0 and 1 could both be emitted as OpenAI tool call index 0. This keeps Claude's original block index when emitting tool calls and adds a regression test for two sequential tool blocks.

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

GOCACHE=/private/tmp/new-api-go-cache GOMODCACHE=/private/tmp/new-api-go-mod go test ./relay/channel/claude -run 'TestStreamResponseClaude2OpenAIUsesClaudeBlockIndexForToolCalls' -count=1

Result:

ok   github.com/QuantumNous/new-api/relay/channel/claude   0.820s

@coderabbitai

coderabbitai Bot commented May 24, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

The PR fixes an off-by-one index calculation bug in Claude-to-OpenAI streaming response translation. The fcIdx value now uses Claude's content-block index directly instead of subtracting 1 and clamping to 0, eliminating index collisions that corrupted concurrent tool-call arguments.

Changes

Claude tool call indexing fix

Layer / File(s) Summary
Tool index calculation and validation
relay/channel/claude/relay-claude.go, relay/channel/claude/relay_claude_test.go
The fcIdx calculation in StreamResponseClaude2OpenAI now uses claudeResponse.Index directly, removing the previous -1 offset. A new test validates that tool-call indices are preserved from Claude's content_block_start index values without collisions.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • QuantumNous/new-api#2854: Both PRs adjust Claude→OpenAI streaming tool-call Index mapping to derive tool call ordering directly from Claude content-block indices (including new base/max index tracking and corrected transition/index handling), so the main PR's fcIdx change is part of the same tool-call indexing behavior.
  • QuantumNous/new-api#3080: Both PRs modify StreamResponseClaude2OpenAI's tool-call Index mapping logic in relay/channel/claude/relay-claude.go to derive the OpenAI-style tool call ordering from Claude content_block_start indices (including the exact off-by-one/index selection).

Suggested reviewers

  • seefs001
  • Calcium-Ion

Poem

🐰 A bunny hops through tool calls true,
No more collisions blocking through!
Index by index, clear and bright,
Concurrent tools now work just right! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: preserve Claude tool block indexes' directly relates to the main change—removing the -1 offset to preserve Claude's block index as fcIdx.
Linked Issues check ✅ Passed The code changes directly address issue #5018: removing the -1 offset and clamping logic from fcIdx computation, and adding a test verifying the new behavior.
Out of Scope Changes check ✅ Passed All changes are scoped to the reported issue: fcIdx fix in relay-claude.go and corresponding test in relay_claude_test.go.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@seefs001

Copy link
Copy Markdown
Collaborator

issue创建者提交的PR已经合并 #5095

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.

Bug: StreamResponseClaude2OpenAI fcIdx 撞键导致并发工具调用丢失

2 participants