fix: pass quality parameter to openai-image provider#54
Conversation
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 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 · |
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
| Duplication | 0 |
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
| quality, _ := params["quality"].(string) | ||
| switch strings.TrimSpace(strings.ToLower(quality)) { | ||
| case "", "auto", "low", "medium", "high": | ||
| default: | ||
| return fmt.Errorf("quality 仅支持 auto、low、medium、high") |
There was a problem hiding this comment.
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
Summary of ChangesHello, 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
Using Gemini Code AssistThe 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
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 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
|
|
CodeAnt AI finished reviewing your PR. |
There was a problem hiding this comment.
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.
| quality, _ := params["quality"].(string) | ||
| switch strings.TrimSpace(strings.ToLower(quality)) { | ||
| case "", "auto", "low", "medium", "high": | ||
| default: | ||
| return fmt.Errorf("quality 仅支持 auto、low、medium、high") | ||
| } |
There was a problem hiding this comment.
建议在 quality 的校验白名单中增加 OpenAI 官方支持的 standard 和 hd。虽然目前项目可能主要配合特定的代理(如 yunwu.ai)使用,但保留对官方标准值的支持可以增强代码的兼容性,防止在切换回官方 API 时发生验证错误。
| 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
- 所有用户输入必须进行验证和清理。 (link)
| if quality, _ := params["quality"].(string); strings.TrimSpace(quality) != "" { | ||
| body.Quality = strings.TrimSpace(strings.ToLower(quality)) | ||
| } |
There was a problem hiding this comment.
此处对 quality 进行了重复的 strings.TrimSpace 调用。建议优化逻辑以减少不必要的计算,这符合项目性能优化规范中关于避免冗余计算的要求。
| 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
- 性能优化规范建议避免不必要的重复计算。 (link)
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.
User description
变更类型
变更说明
前端在选择
openai-imageprovider 时会发送quality参数(auto/low/medium/high),但后端openai_image.go的buildImagesGenerationRequestBody从未读取该参数,导致 API 请求中 quality 始终为空。本次修复:
buildImagesGenerationRequestBody:从params["quality"]读取并设置到请求体ValidateParams:增加 quality 值的白名单校验(auto/low/medium/high)验证
go vet ./internal/provider/...通过/v1/images/generations带quality: "low"返回 200 正常检查清单
CodeAnt-AI Description
OpenAI image quality is now validated and sent in image requests
What Changed
qualityvalue instead of dropping itqualityis checked before sending, and onlyauto,low,medium, orhighare acceptedqualityvalues now fail early with a clear errorImpact
✅ 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:
This lets you have a chat with CodeAnt AI about your pull request, making it easier to understand and improve your code.
Example
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:
This helps CodeAnt AI learn and adapt to your team's coding style and standards.
Example
Retrigger review
Ask CodeAnt AI to review the PR again, by typing:
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.