Skip to content

fix: gemini request -> openai tool call - #2558

Merged
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
seefs001:fix/gemini-tool-call
Jan 3, 2026
Merged

fix: gemini request -> openai tool call#2558
Calcium-Ion merged 1 commit into
QuantumNous:mainfrom
seefs001:fix/gemini-tool-call

Conversation

@seefs001

@seefs001 seefs001 commented Dec 31, 2025

Copy link
Copy Markdown
Collaborator

fix #2479

Summary by CodeRabbit

  • Bug Fixes
    • Fixed tool format detection to correctly parse both array and single object configurations.
    • Improved error handling during tool conversion to prevent service interruptions.

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

@coderabbitai

coderabbitai Bot commented Dec 31, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

Fixes two issues in Gemini tool call handling: corrects string detection logic for tool arrays (checks prefix instead of suffix with "[") in GetTools parsing, and adds error handling for FunctionDeclarations type conversion using a safer utility function instead of direct type assertion.

Changes

Cohort / File(s) Summary
Tool array detection fix
dto/gemini.go
Changed GetTools parsing from HasSuffix("[") to HasPrefix("[") to correctly detect tool arrays; adds fallback handling for single-tool objects starting with "{"
Type conversion error handling
service/convert.go
Replaced direct type assertion of FunctionDeclarations with common.Any2Type[[]dto.FunctionRequest] utility function; adds error logging and skips processing on conversion failure instead of panicking

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A parser's flaw, now set ablaze,
Tools hiding at the string's beginning phase,
With brackets checked and types converted right,
Function calls now work with all their might! ✨

Pre-merge checks and finishing touches

❌ 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%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ 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 accurately summarizes the main objective of fixing Gemini-to-OpenAI tool call conversion issues identified in linked issue #2479.
Linked Issues check ✅ Passed Both changes directly address the two problems identified in issue #2479: the GetTools parsing logic fix corrects the list-of-tool-call detection [#2479], and the FunctionDeclarations conversion fix replaces type assertion with safe conversion utility [#2479].
Out of Scope Changes check ✅ Passed All code changes are directly related to fixing the two Gemini-to-OpenAI tool call conversion issues described in issue #2479; no out-of-scope changes detected.
✨ Finishing touches
  • 📝 Generate docstrings

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

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b808b96 and ddb40b1.

📒 Files selected for processing (2)
  • dto/gemini.go
  • service/convert.go
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-08-08T17:12:43.157Z
Learnt from: RedwindA
Repo: QuantumNous/new-api PR: 1537
File: relay/gemini_handler.go:330-342
Timestamp: 2025-08-08T17:12:43.157Z
Learning: In the new-api repository, the `GeminiEmbeddingHandler` function in `relay/gemini_handler.go` is designed specifically for native Gemini embedding requests and therefore does not require the `ConvertGeminiRequest` step that is used in the chat handler. The embedding requests are already in the native Gemini format and don't need conversion.

Applied to files:

  • service/convert.go
🧬 Code graph analysis (1)
service/convert.go (3)
common/utils.go (1)
  • Any2Type (291-303)
dto/openai_request.go (2)
  • FunctionRequest (249-254)
  • ToolCallRequest (242-247)
common/sys_log.go (1)
  • SysError (16-19)
🔇 Additional comments (1)
service/convert.go (1)

677-692: Safer type conversion with proper error handling.

Replacing the direct type assertion with common.Any2Type prevents panics when the incoming Gemini FunctionDeclarations structure doesn't match []dto.FunctionRequest exactly. The error logging includes both the error and the actual type (%T), which aids debugging. Continuing on error allows other tools to be processed successfully.

Consider verifying that the conversion handles actual Gemini function declarations correctly, as limited test coverage exists for this conversion path.

Comment thread dto/gemini.go
func (r *GeminiChatRequest) GetTools() []GeminiChatTool {
var tools []GeminiChatTool
if strings.HasSuffix(string(r.Tools), "[") {
if strings.HasPrefix(string(r.Tools), "[") {

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

Critical fix: Correct array detection logic.

The change from HasSuffix to HasPrefix correctly identifies JSON arrays that start with "[". The previous suffix check would never match valid JSON arrays, causing tool call detection to fail entirely.

🤖 Prompt for AI Agents
In dto/gemini.go around line 129, the JSON array detection currently uses
strings.HasSuffix which cannot match arrays that start with "["; replace the
HasSuffix check with strings.HasPrefix(string(r.Tools), "[") so the code
correctly detects JSON arrays beginning with "[" and triggers tool call
handling; update any related comments/tests if they assume the old behavior.

@seefs001
seefs001 marked this pull request as draft January 1, 2026 07:30
@seefs001
seefs001 marked this pull request as ready for review January 3, 2026 04:34
@Calcium-Ion
Calcium-Ion merged commit e8aaed4 into QuantumNous:main Jan 3, 2026
1 check passed
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 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.

gemini模型,使用tool call功能,代码中解析list of tool call是不是判断错了

2 participants