Skip to content

Revert "Gemini Image系列支持图像编辑" - #2338

Merged
Calcium-Ion merged 1 commit into
mainfrom
revert-2321-pr/gemini-image-edit
Nov 30, 2025
Merged

Revert "Gemini Image系列支持图像编辑"#2338
Calcium-Ion merged 1 commit into
mainfrom
revert-2321-pr/gemini-image-edit

Conversation

@seefs001

@seefs001 seefs001 commented Nov 30, 2025

Copy link
Copy Markdown
Collaborator

Reverts #2321

Summary by CodeRabbit

  • Refactor
    • Simplified Gemini image request handling by streamlining content construction for specific model variants.
    • Removed internal image base64 extraction utilities and streamlined file data access patterns.
    • Cleaned up image edit request processing by removing legacy special-case handling.

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

@coderabbitai

coderabbitai Bot commented Nov 30, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

This PR systematically removes special-case handling for image-related request modes across the relay system. It eliminates dedicated multipart/form-data processing for images-edits, removes public base64 extraction utilities, simplifies Gemini image content handling to a targeted "gemini-3-pro-image" model branch, and refactors internal file access to use accessor methods instead of direct struct fields.

Changes

Cohort / File(s) Summary
API Request Header Setup
relay/channel/api_request.go
Removed special-case branch for RelayModeImagesEdits in SetupApiRequestHeader; images-edits now follows the general header setup path with Content-Type and Accept from the incoming request.
Gemini Image Content Handling
relay/channel/gemini/adaptor.go
Replaced previous Imagine content assembly with a targeted branch for models with UpstreamModelName starting with "gemini-3-pro-image"; constructs GeneralOpenAIRequest with only the prompt as the user message, removing base64 form-data construction.
Gemini Request Conversion
relay/channel/gemini/relay-gemini.go
Removed variadic base64Data parameter from CovertOpenAI2Gemini signature; replaced direct file field access with part.GetFile() accessor calls for FileId and FileData.
Shared Image Utilities
relay/common/relay_utils.go
Removed public functions GetImagesBase64sFromForm, GetImageBase64sFromForm, GetBase64sFromForm and the Base64Data struct type with its String method; eliminates multipart form base64 image extraction from public API.
Image Request Validation
relay/helper/valid_request.go
Removed reading of response_format field from multipart/form-data path in GetAndValidOpenAIImageRequest for RelayModeImagesEdits.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • Verify all call sites of CovertOpenAI2Gemini have been updated to remove the base64Data argument
  • Confirm removed public utilities (GetImagesBase64sFromForm, GetImageBase64sFromForm, etc.) are not referenced elsewhere in the codebase
  • Validate the "gemini-3-pro-image" model-specific logic in adaptor.go produces the correct request structure
  • Review accessor method calls (part.GetFile()) for correct field access patterns

Possibly related PRs

Suggested reviewers

  • creamlike1024

Poem

🐰 Image edits shed their special disguise,
Base64 utilities bid their goodbyes,
Accessors guide the way so clean,
A simpler relay flow is now seen,
No more branches—just one steadfast line! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly indicates this is a revert of a previous change (PR #2321 related to Gemini Image editing support), which is directly supported by the PR description and the systematic removal of image editing functionality across multiple files.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch revert-2321-pr/gemini-image-edit

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.

@Calcium-Ion
Calcium-Ion merged commit f9c8624 into main Nov 30, 2025
1 check was pending

@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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
relay/channel/gemini/adaptor.go (1)

146-175: Address inconsistent JSON error handling in the image generation flow.

The code adds support for gemini-3-pro-image models by converting ImageRequest to a chat-style GeneralOpenAIRequest with Google-specific generation config. One concern:

Error handling: Line 172 silently ignores JSON marshaling errors with extraBody, _ = json.Marshal(extraBody). The codebase consistently handles errors from json.Marshal in other locations. Update to:

-		chatRequest.ExtraBody, _ = json.Marshal(extraBody)
+		extraBodyBytes, err := json.Marshal(extraBody)
+		if err != nil {
+			return nil, fmt.Errorf("failed to marshal extraBody: %w", err)
+		}
+		chatRequest.ExtraBody = extraBodyBytes

The model names ("nano-banana", "gemini-2.5-flash-image") are valid and documented in setting/model_setting/gemini.go. The ImageSize clearing workaround is properly commented and targets the specific models requiring the fix.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8d0827c and d4a2c2a.

📒 Files selected for processing (5)
  • relay/channel/api_request.go (0 hunks)
  • relay/channel/gemini/adaptor.go (1 hunks)
  • relay/channel/gemini/relay-gemini.go (2 hunks)
  • relay/common/relay_utils.go (0 hunks)
  • relay/helper/valid_request.go (0 hunks)
💤 Files with no reviewable changes (3)
  • relay/helper/valid_request.go
  • relay/channel/api_request.go
  • relay/common/relay_utils.go
🧰 Additional context used
🧠 Learnings (4)
📓 Common learnings
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.
📚 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:

  • relay/channel/gemini/relay-gemini.go
  • relay/channel/gemini/adaptor.go
📚 Learning: 2025-08-05T17:14:17.246Z
Learnt from: neotf
Repo: QuantumNous/new-api PR: 1511
File: setting/ratio_setting/model_ratio.go:118-123
Timestamp: 2025-08-05T17:14:17.246Z
Learning: Claude models handle "-thinking" variants differently from Gemini models. For Claude models, only the base model (without "-thinking") gets an entry in defaultModelRatio map. The "-thinking" variants rely on the Claude relay handler stripping the suffix using strings.TrimSuffix(textRequest.Model, "-thinking") before looking up the ratio, so they automatically use the base model's ratio.

Applied to files:

  • relay/channel/gemini/adaptor.go
📚 Learning: 2025-08-04T14:36:34.528Z
Learnt from: antecanis8
Repo: QuantumNous/new-api PR: 1500
File: relay/channel/gemini/adaptor.go:0-0
Timestamp: 2025-08-04T14:36:34.528Z
Learning: In the Gemini API batchEmbedContents endpoint, each request item in the "requests" array should include its own "model" field, as shown in the official documentation at https://ai.google.dev/api/embeddings?hl=zh-cn#method:-models.batchembedcontents. The model field is not a top-level parameter.

Applied to files:

  • relay/channel/gemini/adaptor.go
🧬 Code graph analysis (2)
relay/channel/gemini/relay-gemini.go (3)
dto/openai_request.go (1)
  • GeneralOpenAIRequest (26-98)
dto/gemini.go (1)
  • GeminiChatRequest (14-23)
service/image.go (1)
  • DecodeBase64FileData (43-66)
relay/channel/gemini/adaptor.go (2)
dto/openai_request.go (2)
  • GeneralOpenAIRequest (26-98)
  • Message (282-293)
model/model_meta.go (1)
  • Model (23-44)
🔇 Additional comments (1)
relay/channel/gemini/relay-gemini.go (1)

185-185: LGTM: Simplified function signature removes image editing complexity.

The removal of the variadic base64Data parameter simplifies the function interface and aligns with this PR's goal of removing special-case image editing handling. This change makes the conversion flow more straightforward.

Comment on lines +467 to +470
if part.GetFile().FileId != "" {
return nil, fmt.Errorf("only base64 file is supported in gemini")
}
format, base64String, err := service.DecodeBase64FileData(file.FileData)
format, base64String, err := service.DecodeBase64FileData(part.GetFile().FileData)

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

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Find GetFile() method definition
rg -n "func.*GetFile\(\)" --type=go -A5 | head -30

Repository: QuantumNous/new-api

Length of output: 414


🏁 Script executed:

#!/bin/bash
# Check GetFile usage patterns and any nil checks
rg -n -B2 -A2 "GetFile\(\)" --type=go | head -60

Repository: QuantumNous/new-api

Length of output: 1250


🏁 Script executed:

#!/bin/bash
# Look for proto definitions related to File or Content
fd -e proto -e pb.go | xargs grep -l "GetFile" 2>/dev/null | head -10

Repository: QuantumNous/new-api

Length of output: 45


🏁 Script executed:

#!/bin/bash
# Get the full GetFile() implementation
sed -n '339,360p' dto/openai_request.go

Repository: QuantumNous/new-api

Length of output: 678


Add nil check before calling GetFile() accessor methods.

The GetFile() method (defined in dto/openai_request.go:339) explicitly returns nil if m.File is nil or fails type assertions. At lines 467 and 470, the code directly calls GetFile().FileId and GetFile().FileData without null safety checks, creating a panic risk. Ensure the result is assigned to a variable and checked for nil before dereferencing, following the pattern used elsewhere (e.g., dto/openai_request.go:163).

🤖 Prompt for AI Agents
In relay/channel/gemini/relay-gemini.go around lines 467 to 470, the code calls
part.GetFile().FileId and part.GetFile().FileData directly which can panic
because GetFile() may return nil; assign the result of part.GetFile() to a local
variable, check it for nil before accessing FileId/FileData, and return an
appropriate error (or handle the nil case) if the file is nil; follow the
existing pattern used elsewhere (e.g., dto/openai_request.go:163) to safe-guard
the dereferences.

@nickhelion

Copy link
Copy Markdown

这个 feature 还是需要的,请问有无计划加回来

@nickhelion

Copy link
Copy Markdown

这个 feature 还是需要的,请问有无计划加回来
可以通过谷歌形式的路径调用
/v1beta/models/gemini-2.5-flash-image:generateContent

@Calcium-Ion
Calcium-Ion deleted the revert-2321-pr/gemini-image-edit branch January 29, 2026 17:10
ennnnny pushed a commit to ennnnny/new-api that referenced this pull request Mar 17, 2026
…emini-image-edit

Revert "Gemini Image系列支持图像编辑"
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