Skip to content

fix: pass quality parameter to openai-image provider#54

Merged
ShellMonster merged 1 commit intomainfrom
fix/openai-image-quality-param
Apr 22, 2026
Merged

fix: pass quality parameter to openai-image provider#54
ShellMonster merged 1 commit intomainfrom
fix/openai-image-quality-param

Conversation

@ShellMonster
Copy link
Copy Markdown
Owner

@ShellMonster ShellMonster commented Apr 22, 2026

User description

变更类型

  • Bug修复 (fix)

变更说明

前端在选择 openai-image provider 时会发送 quality 参数(auto/low/medium/high),但后端 openai_image.gobuildImagesGenerationRequestBody 从未读取该参数,导致 API 请求中 quality 始终为空。

本次修复:

  • buildImagesGenerationRequestBody:从 params["quality"] 读取并设置到请求体
  • ValidateParams:增加 quality 值的白名单校验(auto/low/medium/high)

验证

  • go vet ./internal/provider/... 通过
  • 直接调用 yunwu.ai /v1/images/generationsquality: "low" 返回 200 正常

检查清单

  • 代码遵循项目规范
  • 本地编译通过

CodeAnt-AI Description

OpenAI image quality is now validated and sent in image requests

What Changed

  • OpenAI image requests now include the selected quality value instead of dropping it
  • quality is checked before sending, and only auto, low, medium, or high are accepted
  • Invalid quality values now fail early with a clear error

Impact

✅ Correct image quality settings in generated images
✅ Fewer failed image requests from invalid quality values
✅ Clearer image generation error messages

🔄 Retrigger CodeAnt AI Review

Details

💡 Usage Guide

Checking Your Pull Request

Every time you make a pull request, our system automatically looks through it. We check for security issues, mistakes in how you're setting up your infrastructure, and common code problems. We do this to make sure your changes are solid and won't cause any trouble later.

Talking to CodeAnt AI

Got a question or need a hand with something in your pull request? You can easily get in touch with CodeAnt AI right here. Just type the following in a comment on your pull request, and replace "Your question here" with whatever you want to ask:

@codeant-ai ask: Your question here

This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.

Example

@codeant-ai ask: Can you suggest a safer alternative to storing this secret?

Preserve Org Learnings with CodeAnt

You can record team preferences so CodeAnt AI applies them in future reviews. Reply directly to the specific CodeAnt AI suggestion (in the same thread) and replace "Your feedback here" with your input:

@codeant-ai: Your feedback here

This helps CodeAnt AI learn and adapt to your team's coding style and standards.

Example

@codeant-ai: Do not flag unused imports.

Retrigger review

Ask CodeAnt AI to review the PR again, by typing:

@codeant-ai: review

Check Your Repository Health

To analyze the health of your code repository, visit our dashboard at https://app.codeant.ai. This tool helps you identify potential issues and areas for improvement in your codebase, ensuring your repository maintains high standards of code health.

The frontend sends quality (auto/low/medium/high) but the backend
openai_image.go never read it from params, always leaving it empty.
Now quality is properly forwarded to /v1/images/generations.
@codeant-ai
Copy link
Copy Markdown

codeant-ai Bot commented Apr 22, 2026

CodeAnt AI is reviewing your PR.


Thanks for using CodeAnt! 🎉

We're free for open-source projects. if you're enjoying it, help us grow by sharing.

Share on X ·
Reddit ·
LinkedIn

@codeant-ai codeant-ai Bot added the size:S This PR changes 10-29 lines, ignoring generated files label Apr 22, 2026
@codacy-production
Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes. Give us feedback

Comment on lines +64 to +68
quality, _ := params["quality"].(string)
switch strings.TrimSpace(strings.ToLower(quality)) {
case "", "auto", "low", "medium", "high":
default:
return fmt.Errorf("quality 仅支持 auto、low、medium、high")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: The new quality validation silently accepts non-string values because params["quality"].(string) ignores failed type assertions and falls back to "", which passes the whitelist. This means inputs like numbers/objects bypass validation and are then dropped from the request body, causing incorrect behavior instead of a clear 400 error. Validate the type explicitly when the key is present, and return an error if quality is not a string. [type error]

Severity Level: Major ⚠️
- ❌ /api/v1/tasks/generate ignores non-string quality silently.
- ⚠️ Image quality may differ from caller's configuration.
Steps of Reproduction ✅
1. Start the backend server (`backend/cmd/server/main.go:260-33`) so that the HTTP route
`POST /api/v1/tasks/generate` is registered with `GenerateHandler` in
`backend/internal/api/handlers.go:399-420`.

2. Send an HTTP request to `POST /api/v1/tasks/generate` with JSON body bound to
`GenerateRequest` (`handlers.go:16-21`), for example:

   `{"provider":"openai-image","model_id":"gpt-image","params":{"prompt":"test
   image","quality":1}}`; because `Params` is `map[string]interface{}`, the `quality`
   value `1` is decoded as a non-string type (`float64`).

3. Inside `GenerateHandler`, `p := provider.GetProvider(req.Provider)` resolves to an
`OpenAIImageProvider` instance for provider `"openai-image"` via
`backend/internal/provider/provider.go:14-21`, and `p.ValidateParams(req.Params)` is
called at `handlers.go:60-63`.

4. In `OpenAIImageProvider.ValidateParams` (`openai_image.go:40-71`), `quality, _ :=
params["quality"].(string)` at line 64 fails the type assertion (value is `float64`), so
`quality` becomes the zero value `""`; the `switch` at lines 65-68 treats `""` as allowed
and returns `nil` instead of an error, and later `buildImagesGenerationRequestBody` at
`openai_image.go:134-155` again asserts `params["quality"].(string)`, sees a non-string,
and omits `body.Quality`, causing the OpenAI Images request to ignore the caller-supplied
`quality` value without returning the expected 400-type validation error.

Fix in Cursor | Fix in VSCode Claude

(Use Cmd/Ctrl + Click for best experience)

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** backend/internal/provider/openai_image.go
**Line:** 64:68
**Comment:**
	*Type Error: The new quality validation silently accepts non-string values because `params["quality"].(string)` ignores failed type assertions and falls back to `""`, which passes the whitelist. This means inputs like numbers/objects bypass validation and are then dropped from the request body, causing incorrect behavior instead of a clear 400 error. Validate the type explicitly when the key is present, and return an error if `quality` is not a string.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

本次变更修复了 openai-image provider 在处理图像生成请求时忽略 quality 参数的问题。通过在请求构建逻辑中添加参数映射以及在校验逻辑中增加白名单限制,确保了 API 请求的完整性与参数的合法性。

Highlights

  • 参数传递修复: 在 buildImagesGenerationRequestBody 中增加了对 quality 参数的读取,确保该参数能正确传递给 OpenAI API。
  • 参数校验增强: 在 ValidateParams 中引入了 quality 参数的白名单校验,仅允许 auto、low、medium 和 high 四种值。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@codeant-ai
Copy link
Copy Markdown

codeant-ai Bot commented Apr 22, 2026

CodeAnt AI finished reviewing your PR.

@ShellMonster ShellMonster merged commit 7d5a6ce into main Apr 22, 2026
5 checks passed
@ShellMonster ShellMonster deleted the fix/openai-image-quality-param branch April 22, 2026 03:04
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a quality parameter to the OpenAI image provider, adding validation logic and integrating it into the image generation request body. The review feedback suggests expanding the validation whitelist to include standard OpenAI values like standard and hd to ensure better compatibility with the official API. Additionally, there is a recommendation to optimize the code by removing redundant string trimming operations during the request body construction.

Comment on lines +64 to +69
quality, _ := params["quality"].(string)
switch strings.TrimSpace(strings.ToLower(quality)) {
case "", "auto", "low", "medium", "high":
default:
return fmt.Errorf("quality 仅支持 auto、low、medium、high")
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

建议在 quality 的校验白名单中增加 OpenAI 官方支持的 standardhd。虽然目前项目可能主要配合特定的代理(如 yunwu.ai)使用,但保留对官方标准值的支持可以增强代码的兼容性,防止在切换回官方 API 时发生验证错误。

Suggested change
quality, _ := params["quality"].(string)
switch strings.TrimSpace(strings.ToLower(quality)) {
case "", "auto", "low", "medium", "high":
default:
return fmt.Errorf("quality 仅支持 auto、low、medium、high")
}
quality, _ := params["quality"].(string)
switch strings.TrimSpace(strings.ToLower(quality)) {
case "", "auto", "low", "medium", "high", "standard", "hd":
default:
return fmt.Errorf("quality 仅支持 auto、low、medium、high、standard、hd")
}
References
  1. 所有用户输入必须进行验证和清理。 (link)

Comment on lines +150 to +152
if quality, _ := params["quality"].(string); strings.TrimSpace(quality) != "" {
body.Quality = strings.TrimSpace(strings.ToLower(quality))
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

此处对 quality 进行了重复的 strings.TrimSpace 调用。建议优化逻辑以减少不必要的计算,这符合项目性能优化规范中关于避免冗余计算的要求。

Suggested change
if quality, _ := params["quality"].(string); strings.TrimSpace(quality) != "" {
body.Quality = strings.TrimSpace(strings.ToLower(quality))
}
if quality, _ := params["quality"].(string); quality != "" {
body.Quality = strings.TrimSpace(strings.ToLower(quality))
}
References
  1. 性能优化规范建议避免不必要的重复计算。 (link)

ShellMonster added a commit that referenced this pull request Apr 27, 2026
The frontend sends quality (auto/low/medium/high) but the backend
openai_image.go never read it from params, always leaving it empty.
Now quality is properly forwarded to /v1/images/generations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant