Skip to content

feat(gemini): 支持 tool_choice 参数转换,优化多个渠道错误处理 - #2742

Merged
Calcium-Ion merged 9 commits into
QuantumNous:mainfrom
seefs001:fix/pr-2540
Jan 26, 2026
Merged

Calcium-Ion merged 9 commits into
QuantumNous:mainfrom
seefs001:fix/pr-2540

Conversation

@seefs001

@seefs001 seefs001 commented Jan 25, 2026 •

Copy link
Copy Markdown
Collaborator

fix #2540

Summary by CodeRabbit

  • New Features

    • Added tool-choice / function-calling support for Gemini requests
    • Admin-only capture of provider reject/refusal reasons for diagnostics
  • Improvements

    • Unified mapping of provider finish/rejection reasons (Gemini, Claude, OpenAI) to OpenAI-style finish reasons
    • Improved handling and error responses when providers return blocked/no-candidate results
    • CSAM/refusal detection propagated to error handling and admin logs
  • UI

    • Admin-facing usage logs show rejection reasons for inspected requests

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

@coderabbitai

coderabbitai Bot commented Jan 25, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@seefs001 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 4 minutes and 4 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Walkthrough

Adds OpenAI tool_choice → Gemini ToolConfig translation, expands Gemini/Claude/OpenAI finish-reason handling (including safety/block reasons), records an admin-only reject reason in request context, and improves empty-candidates/error handling and logging to surface that reject reason for admins.

Changes

Cohort / File(s) Summary
Gemini Tool & Error Handling
relay/channel/gemini/relay-gemini.go
Adds convertToolChoiceToGeminiConfig() and applies ToolConfig from OpenAI tool_choice; extends mapping of Gemini finish reasons (SAFETY, RECITATION, BLOCKLIST, PROHIBITED_CONTENT, SPII, OTHER) to OpenAI content-filter finish reason; improves handling when Gemini returns no candidates by constructing usage and returning appropriate errors.
Gemini Native Handler — admin reason
relay/channel/gemini/relay-gemini-native.go
When Gemini returns no candidates with PromptFeedback.BlockReason, sets admin reject reason in context as gemini_block_reason=<BlockReason>.
OpenAI safety & admin reason
relay/channel/openai/relay-openai.go
Adds CSAM safety detection (maybeMarkXaiCSAMRefusal) that sets admin reject reason and influences retry/error wrapping; marks content-filter finish reasons into context.
Claude safety & admin reason
relay/channel/claude/relay-claude.go, relay/reasonmap/reasonmap.go
Adds maybeMarkClaudeRefusal() and centralizes Claude↔OpenAI finish-reason mappings via reasonmap utilities; maps Claude "refusal" to OpenAI content-filter finish reason.
Context key for admin reason
constant/context_key.go
Adds ContextKeyAdminRejectReason constant ("admin_reject_reason") to carry admin-only rejection reasons in request context.
Logging & handler integration
model/log.go, relay/compatible_handler.go
formatUserLogs() now also removes reject_reason when sanitizing; compatible handler reads ContextKeyAdminRejectReason and injects reject_reason into the other map for logs/consumption when present.
Frontend admin display
web/src/hooks/usage-logs/useUsageLogsData.jsx
Shows an admin-only expansion row labeled "拦截原因" with other.reject_reason when user is admin and field exists.
Service conversion refactor
service/convert.go
Replaces local stop/finish-reason mapping with reasonmap.OpenAIFinishReasonToClaudeStopReason to centralize mappings.

Sequence Diagram

sequenceDiagram
    participant Client
    participant Handler as Channel Handler\n(OpenAI / Gemini / Claude)
    participant Context as Request Context
    participant Compat as Compatible Handler
    participant Logger as Log Formatter
    participant Frontend

    Client->>Handler: Send API request
    Handler->>Handler: Process upstream response\n(map finish_reason / check PromptFeedback / detect CSAM/refusal)
    Handler->>Context: Set ContextKeyAdminRejectReason\n(e.g. gemini_block_reason / openai_finish_reason / claude_stop_reason)
    Handler->>Compat: Return processed response
    Compat->>Context: Read ContextKeyAdminRejectReason
    Compat->>Logger: Add reject_reason to "other" data (if present)
    Logger->>Frontend: Store/display logs (admin view shows 拦截原因)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • creamlike1024

Poem

🐰 I hopped through code and nudged a choice to flow,
Now Gemini hears the tools the callers know,
When safety bars the path, a secret note will say,
Why the reply was stopped — for admins, kept away,
Hooray for clearer relays — nibble, code, and go! 🥕

🚥 Pre-merge checks | ✅ 3 | ❌ 2
❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning Several out-of-scope changes detected: new ContextKeyAdminRejectReason constant, rejection reason logging/tracking across multiple handlers, reasonmap utility refactoring, and frontend UI updates for admin rejection display. The PR scope should focus on relay-gemini.go per issue #2540. Move admin rejection tracking, reasonmap refactoring, and frontend changes to separate PRs or clarify their necessity in the issue.
Docstring Coverage ⚠️ Warning Docstring coverage is 8.70% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed The PR addresses all primary objectives from #2540: tool_choice conversion to Gemini FunctionCallingConfig, expanded FinishReason mappings for safety-related reasons, and improved error handling for empty Candidates responses.
Title check ✅ Passed The title accurately reflects the main changes: adding support for tool_choice parameter conversion in Gemini and optimizing error handling across multiple channels.

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


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: 1

🤖 Fix all issues with AI agents
In `@relay/channel/gemini/relay-gemini.go`:
- Around line 1357-1401: When handling the empty-candidates branch
(len(geminiResponse.Candidates) == 0) you only populate PromptTokens; also set
CompletionTokens and TotalTokens: read
geminiResponse.UsageMetadata.CompletionTokenCount into usage.CompletionTokens if
present, otherwise fall back to usage.CompletionTokenDetails.ReasoningTokens
(which you already set from geminiResponse.UsageMetadata.ThoughtsTokenCount);
then compute usage.TotalTokens = usage.PromptTokens + usage.CompletionTokens
and, as a final fallback, if TotalTokens <= 0 set TotalTokens =
usage.PromptTokens. Update the dto.Usage population in that block (the usage
variable created there) to include these fields so billing/telemetry aren’t
undercounted.

Comment on lines 1357 to +1401
if len(geminiResponse.Candidates) == 0 {
//return nil, types.NewOpenAIError(errors.New("no candidates returned"), types.ErrorCodeBadResponseBody, http.StatusInternalServerError)
//if geminiResponse.PromptFeedback != nil && geminiResponse.PromptFeedback.BlockReason != nil {
// return nil, types.NewOpenAIError(errors.New("request blocked by Gemini API: "+*geminiResponse.PromptFeedback.BlockReason), types.ErrorCodePromptBlocked, http.StatusBadRequest)
//} else {
// return nil, types.NewOpenAIError(errors.New("empty response from Gemini API"), types.ErrorCodeEmptyResponse, http.StatusInternalServerError)
//}
usage := dto.Usage{
PromptTokens: geminiResponse.UsageMetadata.PromptTokenCount,
}
usage.CompletionTokenDetails.ReasoningTokens = geminiResponse.UsageMetadata.ThoughtsTokenCount
for _, detail := range geminiResponse.UsageMetadata.PromptTokensDetails {
if detail.Modality == "AUDIO" {
usage.PromptTokensDetails.AudioTokens = detail.TokenCount
} else if detail.Modality == "TEXT" {
usage.PromptTokensDetails.TextTokens = detail.TokenCount
}
}
if usage.PromptTokens <= 0 {
usage.PromptTokens = info.GetEstimatePromptTokens()
}

var newAPIError *types.NewAPIError
if geminiResponse.PromptFeedback != nil && geminiResponse.PromptFeedback.BlockReason != nil {
newAPIError = types.NewOpenAIError(
errors.New("request blocked by Gemini API: "+*geminiResponse.PromptFeedback.BlockReason),
types.ErrorCodePromptBlocked,
http.StatusBadRequest,
)
} else {
newAPIError = types.NewOpenAIError(
errors.New("empty response from Gemini API"),
types.ErrorCodeEmptyResponse,
http.StatusInternalServerError,
)
}

service.ResetStatusCode(newAPIError, c.GetString("status_code_mapping"))

switch info.RelayFormat {
case types.RelayFormatClaude:
c.JSON(newAPIError.StatusCode, gin.H{
"type": "error",
"error": newAPIError.ToClaudeError(),
})
default:
c.JSON(newAPIError.StatusCode, gin.H{
"error": newAPIError.ToOpenAIError(),
})
}
return &usage, nil

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

Populate TotalTokens/CompletionTokens for empty-candidate responses.

The new usage object only sets PromptTokens; TotalTokens and CompletionTokens stay zero even when Gemini provides usage metadata. This can undercount billing/telemetry. Suggest filling totals and deriving completion tokens from metadata (or at minimum set TotalTokens = PromptTokens when missing).

🔧 Suggested fix
-		usage := dto.Usage{
-			PromptTokens: geminiResponse.UsageMetadata.PromptTokenCount,
-		}
+		usage := dto.Usage{
+			PromptTokens: geminiResponse.UsageMetadata.PromptTokenCount,
+			TotalTokens:  geminiResponse.UsageMetadata.TotalTokenCount,
+		}
+		if usage.TotalTokens == 0 && usage.PromptTokens > 0 {
+			usage.TotalTokens = usage.PromptTokens
+		}
+		usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens
+		if usage.CompletionTokens < 0 {
+			usage.CompletionTokens = 0
+		}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if len(geminiResponse.Candidates) == 0 {
//return nil, types.NewOpenAIError(errors.New("no candidates returned"), types.ErrorCodeBadResponseBody, http.StatusInternalServerError)
//if geminiResponse.PromptFeedback != nil && geminiResponse.PromptFeedback.BlockReason != nil {
// return nil, types.NewOpenAIError(errors.New("request blocked by Gemini API: "+*geminiResponse.PromptFeedback.BlockReason), types.ErrorCodePromptBlocked, http.StatusBadRequest)
//} else {
// return nil, types.NewOpenAIError(errors.New("empty response from Gemini API"), types.ErrorCodeEmptyResponse, http.StatusInternalServerError)
//}
usage := dto.Usage{
PromptTokens: geminiResponse.UsageMetadata.PromptTokenCount,
}
usage.CompletionTokenDetails.ReasoningTokens = geminiResponse.UsageMetadata.ThoughtsTokenCount
for _, detail := range geminiResponse.UsageMetadata.PromptTokensDetails {
if detail.Modality == "AUDIO" {
usage.PromptTokensDetails.AudioTokens = detail.TokenCount
} else if detail.Modality == "TEXT" {
usage.PromptTokensDetails.TextTokens = detail.TokenCount
}
}
if usage.PromptTokens <= 0 {
usage.PromptTokens = info.GetEstimatePromptTokens()
}
var newAPIError *types.NewAPIError
if geminiResponse.PromptFeedback != nil && geminiResponse.PromptFeedback.BlockReason != nil {
newAPIError = types.NewOpenAIError(
errors.New("request blocked by Gemini API: "+*geminiResponse.PromptFeedback.BlockReason),
types.ErrorCodePromptBlocked,
http.StatusBadRequest,
)
} else {
newAPIError = types.NewOpenAIError(
errors.New("empty response from Gemini API"),
types.ErrorCodeEmptyResponse,
http.StatusInternalServerError,
)
}
service.ResetStatusCode(newAPIError, c.GetString("status_code_mapping"))
switch info.RelayFormat {
case types.RelayFormatClaude:
c.JSON(newAPIError.StatusCode, gin.H{
"type": "error",
"error": newAPIError.ToClaudeError(),
})
default:
c.JSON(newAPIError.StatusCode, gin.H{
"error": newAPIError.ToOpenAIError(),
})
}
return &usage, nil
if len(geminiResponse.Candidates) == 0 {
usage := dto.Usage{
PromptTokens: geminiResponse.UsageMetadata.PromptTokenCount,
TotalTokens: geminiResponse.UsageMetadata.TotalTokenCount,
}
if usage.TotalTokens == 0 && usage.PromptTokens > 0 {
usage.TotalTokens = usage.PromptTokens
}
usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens
if usage.CompletionTokens < 0 {
usage.CompletionTokens = 0
}
usage.CompletionTokenDetails.ReasoningTokens = geminiResponse.UsageMetadata.ThoughtsTokenCount
for _, detail := range geminiResponse.UsageMetadata.PromptTokensDetails {
if detail.Modality == "AUDIO" {
usage.PromptTokensDetails.AudioTokens = detail.TokenCount
} else if detail.Modality == "TEXT" {
usage.PromptTokensDetails.TextTokens = detail.TokenCount
}
}
if usage.PromptTokens <= 0 {
usage.PromptTokens = info.GetEstimatePromptTokens()
}
var newAPIError *types.NewAPIError
if geminiResponse.PromptFeedback != nil && geminiResponse.PromptFeedback.BlockReason != nil {
newAPIError = types.NewOpenAIError(
errors.New("request blocked by Gemini API: "+*geminiResponse.PromptFeedback.BlockReason),
types.ErrorCodePromptBlocked,
http.StatusBadRequest,
)
} else {
newAPIError = types.NewOpenAIError(
errors.New("empty response from Gemini API"),
types.ErrorCodeEmptyResponse,
http.StatusInternalServerError,
)
}
service.ResetStatusCode(newAPIError, c.GetString("status_code_mapping"))
switch info.RelayFormat {
case types.RelayFormatClaude:
c.JSON(newAPIError.StatusCode, gin.H{
"type": "error",
"error": newAPIError.ToClaudeError(),
})
default:
c.JSON(newAPIError.StatusCode, gin.H{
"error": newAPIError.ToOpenAIError(),
})
}
return &usage, nil
🤖 Prompt for AI Agents
In `@relay/channel/gemini/relay-gemini.go` around lines 1357 - 1401, When handling
the empty-candidates branch (len(geminiResponse.Candidates) == 0) you only
populate PromptTokens; also set CompletionTokens and TotalTokens: read
geminiResponse.UsageMetadata.CompletionTokenCount into usage.CompletionTokens if
present, otherwise fall back to usage.CompletionTokenDetails.ReasoningTokens
(which you already set from geminiResponse.UsageMetadata.ThoughtsTokenCount);
then compute usage.TotalTokens = usage.PromptTokens + usage.CompletionTokens
and, as a final fallback, if TotalTokens <= 0 set TotalTokens =
usage.PromptTokens. Update the dto.Usage population in that block (the usage
variable created there) to include these fields so billing/telemetry aren’t
undercounted.

@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: 1

🤖 Fix all issues with AI agents
In `@web/src/components/table/usage-logs/UsageLogsColumnDefs.jsx`:
- Around line 581-583: Replace the hard-coded "Block reason" label with the i18n
translation API used in this component: when building content in
UsageLogsColumnDefs.jsx (the block that checks isAdminUser &&
other?.reject_reason), call the translation function (e.g., t('Block reason') or
i18n.t('blockReason') consistent with existing keys) and concatenate the
translated label with other.reject_reason instead of the literal string; add the
translation key to your locale files if it doesn't exist and ensure the
component imports/uses the same translation hook or i18n instance as the rest of
the file.
♻️ Duplicate comments (1)
relay/channel/gemini/relay-gemini.go (1)

1361-1375: Populate TotalTokens and CompletionTokens for complete usage tracking.

The usage object only sets PromptTokens while TotalTokens and CompletionTokens remain at their zero values. For empty candidate responses, completion tokens are typically zero, but TotalTokens should still equal PromptTokens for accurate billing/telemetry.

🔧 Suggested fix
 		usage := dto.Usage{
 			PromptTokens: geminiResponse.UsageMetadata.PromptTokenCount,
+			TotalTokens:  geminiResponse.UsageMetadata.TotalTokenCount,
 		}
 		usage.CompletionTokenDetails.ReasoningTokens = geminiResponse.UsageMetadata.ThoughtsTokenCount
 		for _, detail := range geminiResponse.UsageMetadata.PromptTokensDetails {
 			if detail.Modality == "AUDIO" {
 				usage.PromptTokensDetails.AudioTokens = detail.TokenCount
 			} else if detail.Modality == "TEXT" {
 				usage.PromptTokensDetails.TextTokens = detail.TokenCount
 			}
 		}
 		if usage.PromptTokens <= 0 {
 			usage.PromptTokens = info.GetEstimatePromptTokens()
 		}
+		if usage.TotalTokens <= 0 {
+			usage.TotalTokens = usage.PromptTokens
+		}
+		usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens
+		if usage.CompletionTokens < 0 {
+			usage.CompletionTokens = 0
+		}

Comment on lines +581 to +583
if (isAdminUser && other?.reject_reason) {
content += `\nBlock reason: ${other.reject_reason}`;
}

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 | 🟡 Minor

Use i18n for the "Block reason" label.
This string should be localized like the rest of the column labels.

💡 Suggested change
-        if (isAdminUser && other?.reject_reason) {
-          content += `\nBlock reason: ${other.reject_reason}`;
-        }
+        if (isAdminUser && other?.reject_reason) {
+          content += `\n${t('Block reason')}: ${other.reject_reason}`;
+        }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (isAdminUser && other?.reject_reason) {
content += `\nBlock reason: ${other.reject_reason}`;
}
if (isAdminUser && other?.reject_reason) {
content += `\n${t('Block reason')}: ${other.reject_reason}`;
}
🤖 Prompt for AI Agents
In `@web/src/components/table/usage-logs/UsageLogsColumnDefs.jsx` around lines 581
- 583, Replace the hard-coded "Block reason" label with the i18n translation API
used in this component: when building content in UsageLogsColumnDefs.jsx (the
block that checks isAdminUser && other?.reject_reason), call the translation
function (e.g., t('Block reason') or i18n.t('blockReason') consistent with
existing keys) and concatenate the translated label with other.reject_reason
instead of the literal string; add the translation key to your locale files if
it doesn't exist and ensure the component imports/uses the same translation hook
or i18n instance as the rest of the file.

@seefs001 seefs001 changed the title feat(gemini): 支持 tool_choice 参数转换,优化错误处理 feat(gemini): 支持 tool_choice 参数转换,优化多个渠道错误处理 Jan 25, 2026
@Calcium-Ion
Calcium-Ion merged commit 3722c63 into QuantumNous:main Jan 26, 2026
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
feat(gemini): 支持 tool_choice 参数转换,优化多个渠道错误处理
salem-2007 added a commit to salem-2007/new-api that referenced this pull request Sep 10, 2026
feat(gemini): 支持 tool_choice 参数转换,优化多个渠道错误处理
junjundesk pushed a commit to junjundesk/new-api that referenced this pull request Oct 3, 2026
feat(gemini): 支持 tool_choice 参数转换,优化多个渠道错误处理
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.

3 participants