fix: gemini request -> openai tool call - #2558
Conversation
WalkthroughFixes 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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
dto/gemini.goservice/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.Any2Typeprevents panics when the incoming GeminiFunctionDeclarationsstructure doesn't match[]dto.FunctionRequestexactly. 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.
| func (r *GeminiChatRequest) GetTools() []GeminiChatTool { | ||
| var tools []GeminiChatTool | ||
| if strings.HasSuffix(string(r.Tools), "[") { | ||
| if strings.HasPrefix(string(r.Tools), "[") { |
There was a problem hiding this comment.
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.
fix: gemini request -> openai tool call
fix #2479
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.