Skip to content

perf: avoid repeated Claude tool-name lookups - #7447

Merged
Calcium-Ion merged 2 commits into
QuantumNous:mainfrom
Cylion-bot:local/claude-tool-lookup
Sep 18, 2026
Merged

Calcium-Ion merged 2 commits into
QuantumNous:mainfrom
Cylion-bot:local/claude-tool-lookup

Conversation

@Cylion-bot

@Cylion-bot Cylion-bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Related Issue

None. This is a focused performance improvement, not a new feature or a bug fix requiring a linked issue.

Type of change

  • Performance / Refactor

Description

When converting a /v1/messages request to /v1/chat/completions, each
tool_result without a name previously triggered
SearchToolNameByToolCallId. Each lookup scanned the message list and parsed
message content again, causing repeated serialization and deserialization as
the number of tool results grew. On requests with long message histories and
many tool results, this can cause substantial CPU usage.

The converter now records tool names while parsing the input messages once
and fills in unnamed tool results after traversal. This preserves explicit
names, first-match behavior for duplicate IDs, and results that reference a
tool call later in the message list.

The code and PR description were prepared with AI assistance and reviewed by
the submitter.

Proof of Work

  • Added regression coverage for forward references, duplicate IDs, explicit
    names, and unknown IDs.
  • GOWORK=off go test ./relayconvert -run TestConvertRequestClaudeToChatResolvesToolResultNames -count=1 — passed.
  • GOWORK=off go build ./... — passed.

Checklist

  • Human review: I have reviewed the code and description and take responsibility for their accuracy and completeness.
  • Not a duplicate: I have searched existing Issues and PRs.
  • Feature issue: Not applicable; this PR does not add a new feature.
  • Prior discussion: Not applicable; this is a focused performance change, not a large or directional change.
  • Scope: This PR is outside the excluded categories listed in the template.
  • Focused change: This PR contains only the converter change and its regression test.
  • Local verification: I have run and reviewed the validation listed above.
  • Security: This change contains no secrets and follows the project's coding guidelines.

Summary by CodeRabbit

  • Bug Fixes

    • Improved conversion of Claude tool results so matching tool names are correctly preserved in OpenAI chat messages.
    • Explicit tool names continue to take precedence, while unmatched tool results remain unnamed.
    • Tool names are now resolved consistently even when the corresponding tool call appears later in the request.
  • Tests

    • Added coverage for matching tool calls, missing tool definitions, and explicit tool-name overrides.

@coderabbitai

coderabbitai Bot commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: a2143d46-6650-406e-8559-062eda2aeb14

📥 Commits

Reviewing files that changed from the base of the PR and between 2237bf0 and 88186aa.

📒 Files selected for processing (2)
  • relaykit/dto/claude.go
  • relaykit/relayconvert/internal/claude_messages/to_oai_chat_req.go
💤 Files with no reviewable changes (1)
  • relaykit/dto/claude.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • relaykit/relayconvert/internal/claude_messages/to_oai_chat_req.go

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


Walkthrough

The Claude-to-OpenAI converter now defers unnamed tool_result name resolution until all content blocks are processed. The previous lookup method was removed. Tests cover resolved, missing, and explicitly provided tool names.

Changes

Tool name resolution

Layer / File(s) Summary
Deferred tool result name resolution
relaykit/relayconvert/internal/claude_messages/to_oai_chat_req.go, relaykit/dto/claude.go, relaykit/relayconvert/request_registry_test.go
The converter records content-block names by ID, backfills unnamed tool results after processing, removes the previous lookup method, and tests resolved, missing, and explicit names.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Merge Risk: ⚪ Minimal · up to 88186

The conversion preserves the documented tool-result naming behavior while avoiding repeated lookups. No actionable merge risk remains.

🚥 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%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: improving performance by avoiding repeated Claude tool-name lookups.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

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

A rabbit reads each line,
The patch grows clear beneath the moon,
Small changes hop in place,
Tests guard the garden path,
Reviews bloom before the dawn.

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

…okup

Replace the repeated anonymous struct in the Claude-to-chat converter
with a local named type, and remove SearchToolNameByToolCallId now that
the converter resolves tool names in a single pass and nothing else
calls it.
@Calcium-Ion
Calcium-Ion merged commit 946c730 into QuantumNous:main Sep 18, 2026
1 check passed
Javabcd pushed a commit to Javabcd/new-api that referenced this pull request Sep 22, 2026
* perf: avoid repeated Claude tool-name lookups

* refactor(relaykit): name tool-result backfill type and drop unused lookup

Replace the repeated anonymous struct in the Claude-to-chat converter
with a local named type, and remove SearchToolNameByToolCallId now that
the converter resolves tool names in a single pass and nothing else
calls it.
GuDong2003 pushed a commit to GuDong2003/new-api that referenced this pull request Sep 25, 2026
* perf: avoid repeated Claude tool-name lookups

* refactor(relaykit): name tool-result backfill type and drop unused lookup

Replace the repeated anonymous struct in the Claude-to-chat converter
with a local named type, and remove SearchToolNameByToolCallId now that
the converter resolves tool names in a single pass and nothing else
calls it.
TomJKS pushed a commit to TomJKS/new-api that referenced this pull request Sep 28, 2026
* perf: avoid repeated Claude tool-name lookups

* refactor(relaykit): name tool-result backfill type and drop unused lookup

Replace the repeated anonymous struct in the Claude-to-chat converter
with a local named type, and remove SearchToolNameByToolCallId now that
the converter resolves tool names in a single pass and nothing else
calls it.
timwenx pushed a commit to timwenx/new-api that referenced this pull request Nov 18, 2026
* perf: avoid repeated Claude tool-name lookups

* refactor(relaykit): name tool-result backfill type and drop unused lookup

Replace the repeated anonymous struct in the Claude-to-chat converter
with a local named type, and remove SearchToolNameByToolCallId now that
the converter resolves tool names in a single pass and nothing else
calls it.
xiaolanawaqaq pushed a commit to xiaolanawaqaq/new-apiaaa that referenced this pull request Nov 26, 2026
* perf: avoid repeated Claude tool-name lookups

* refactor(relaykit): name tool-result backfill type and drop unused lookup

Replace the repeated anonymous struct in the Claude-to-chat converter
with a local named type, and remove SearchToolNameByToolCallId now that
the converter resolves tool names in a single pass and nothing else
calls it.
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.

2 participants