Skip to content

fix: preserve OpenAI image edit reference fields - #4646

Merged
Calcium-Ion merged 2 commits into
QuantumNous:mainfrom
seefs001:fix/openai-image-edit
May 6, 2026
Merged

fix: preserve OpenAI image edit reference fields#4646
Calcium-Ion merged 2 commits into
QuantumNous:mainfrom
seefs001:fix/openai-image-edit

Conversation

@seefs001

@seefs001 seefs001 commented May 6, 2026

Copy link
Copy Markdown
Collaborator

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

(简述:做了什么?为什么这样改能生效?请基于你对代码逻辑的理解来写,避免粘贴未经整理的内容)

补充图片编辑参数,支持application/json传入参数

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

  • Closes # (如有)

✅ 提交前检查项 / Checklist

  • 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • 变更理解: 我已理解这些更改的工作原理及可能影响。
  • 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

(请在此粘贴截图、关键日志或测试报告,以证明变更生效)

Summary by CodeRabbit

  • New Features
    • Added support for JSON-based image editing operations with optimized request handling for improved efficiency
    • Introduced three new image parameters to provide enhanced control and flexibility in image editing workflows
    • Enhanced processing of JSON-formatted image edit requests for better user experience

@coderabbitai

coderabbitai Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

Three files are modified to support JSON-based image edit requests. ImageRequest DTO gains three new fields. ConvertImageRequest adds JSON detection to skip multipart processing for JSON image edits. Helper code replaces json.Marshal with common.Marshal for consistency.

Changes

JSON-Based Image Edit Support

Layer / File(s) Summary
Data Shape
dto/openai_image.go
ImageRequest struct adds Images, Mask, and InputFidelity fields as json.RawMessage to support new image edit parameters.
Core Logic
relay/channel/openai/adaptor.go
New isJSONRequest helper detects JSON requests. ConvertImageRequest returns early for ImagesEdits when JSON is detected, bypassing multipart form construction. DoRequest condition updated to check non-JSON before triggering form request.
Integration
relay/helper/valid_request.go
Image encoding in multipart/form-data branch switches from json.Marshal to common.Marshal for consistency with project's serialization approach.

Sequence Diagram

sequenceDiagram
    participant Client
    participant ConvertImageRequest
    participant isJSONRequest
    participant DoRequest
    participant Helper as valid_request

    Client->>ConvertImageRequest: ImageEdits request (JSON)
    ConvertImageRequest->>isJSONRequest: Check content-type
    isJSONRequest-->>ConvertImageRequest: true
    ConvertImageRequest-->>Client: Return early (skip multipart)
    
    Client->>DoRequest: Forward JSON request
    DoRequest->>DoRequest: Check isJSONRequest + ImagesEdits
    DoRequest-->>Client: Process as JSON (no form)

    alt Multipart Path
        Client->>ConvertImageRequest: ImageEdits request (form)
        ConvertImageRequest->>isJSONRequest: Check content-type
        isJSONRequest-->>ConvertImageRequest: false
        ConvertImageRequest->>Helper: Build multipart payload
        Helper->>Helper: Encode with common.Marshal
        Helper-->>ConvertImageRequest: multipart body
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • QuantumNous/new-api#2321: Modifies image-edit request handling and relay/helper/valid_request.go for multipart vs JSON processing.
  • QuantumNous/new-api#2338: Addresses ImagesEdits multipart handling by preventing forced multipart construction for JSON image-edit requests.
  • QuantumNous/new-api#2090: Modifies image-edit request handling and dto/openai_image.go to support JSON-based image edits.

Suggested reviewers

  • creamlike1024

Poem

🐰 Three little fields hop into the struct,
JSON flows freely without multipart's fuss,
Early returns skip the form we don't trust,
Common marshalling keeps the code robust! 🎨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ 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%. 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 accurately reflects the primary change: adding support for OpenAI image edit reference fields (Images, Mask, InputFidelity) across multiple files.
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.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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.

🧹 Nitpick comments (1)
relay/channel/openai/adaptor.go (1)

603-613: ⚡ Quick win

Consider adding server-side image presence validation for the JSON path

The multipart path guards against an imageless request:

if !foundArrayImages && (len(imageFiles) == 0) {
    return nil, errors.New("image is required")
}

The JSON path has no equivalent check — if neither image nor images is set in the JSON body, the request goes upstream and OpenAI returns a provider-level error rather than a clear local one. A lightweight guard in ConvertImageRequest would match the multipart UX:

✨ Proposed addition in ConvertImageRequest (JSON branch)
 case relayconstant.RelayModeImagesEdits:
     if isJSONRequest(c) {
+        if len(request.Image) == 0 && len(request.Images) == 0 {
+            return nil, errors.New("image or images is required for image edit")
+        }
         return request, nil
     }
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@relay/channel/openai/adaptor.go` around lines 603 - 613, DoRequest routes
image-edit requests to either the multipart form path or the JSON path, but
ConvertImageRequest (the JSON branch) lacks the multipart-equivalent validation
that ensures an image is present; add a lightweight server-side guard in
ConvertImageRequest to detect absence of both "image" and "images" fields (or
empty arrays/strings) and return a clear error (e.g., "image is required")
before proxying upstream so that image-edit JSON requests fail locally like the
multipart branch does. Ensure the check mirrors the multipart logic (used where
foundArrayImages/imageFiles are checked) and returns an appropriate error type
and message from ConvertImageRequest so DoRequest callers receive the local
validation error.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@relay/channel/openai/adaptor.go`:
- Around line 603-613: DoRequest routes image-edit requests to either the
multipart form path or the JSON path, but ConvertImageRequest (the JSON branch)
lacks the multipart-equivalent validation that ensures an image is present; add
a lightweight server-side guard in ConvertImageRequest to detect absence of both
"image" and "images" fields (or empty arrays/strings) and return a clear error
(e.g., "image is required") before proxying upstream so that image-edit JSON
requests fail locally like the multipart branch does. Ensure the check mirrors
the multipart logic (used where foundArrayImages/imageFiles are checked) and
returns an appropriate error type and message from ConvertImageRequest so
DoRequest callers receive the local validation error.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 322709dd-813f-4588-a224-887ad6cf7fa5

📥 Commits

Reviewing files that changed from the base of the PR and between 446a842 and 6513edc.

📒 Files selected for processing (3)
  • dto/openai_image.go
  • relay/channel/openai/adaptor.go
  • relay/helper/valid_request.go

@Calcium-Ion
Calcium-Ion merged commit 38a3314 into QuantumNous:main May 6, 2026
2 checks passed
zhuguoliang1014 pushed a commit to zhuguoliang1014/new-api that referenced this pull request May 8, 2026
- OpenAI image edit: preserve reference image and metadata (QuantumNous#4646)
- UI: migrate Select to Base UI items API (QuantumNous#4655)
- feat: add model performance badges to model lists
- fix: improve billing settings forms
- refactor: move top_up_link from status API to topup info API

Preserved local hupijiao payment integration across all merge conflicts.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Jinxuans referenced this pull request in TokFlux-Org/TokFlux May 9, 2026
* fix: preserve OpenAI image edit reference fields

* feat: support json image edit requests
singcheong pushed a commit to Jwell-ai/jwell-api that referenced this pull request May 14, 2026
* fix: preserve OpenAI image edit reference fields

* feat: support json image edit requests
xyfacai pushed a commit to xyfacai/new-api that referenced this pull request May 30, 2026
* fix: preserve OpenAI image edit reference fields

* feat: support json image edit requests
isboyjc added a commit to isboyjc/amux-api that referenced this pull request May 31, 2026
… edit

- ImageRequest 新增 images/mask/input_fidelity 字段,避免 image edit 引用字段丢失
- 当请求为 application/json 时,image edit 直接透传 JSON 而非强制走 multipart form
  (ConvertImageRequest 早返回 + DoRequest 条件加 !isJSONRequest)
- valid_request.go 不改:该行已是 common.Marshal,encoding/json 仍被 json.RawMessage 类型使用

upstream 38a3314 QuantumNous#4646
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