Skip to content

fix(relay): reject image models on chat completions - #6478

Open
sl999ops wants to merge 1 commit into
QuantumNous:mainfrom
sl999ops:main
Open

fix(relay): reject image models on chat completions#6478
sl999ops wants to merge 1 commit into
QuantumNous:mainfrom
sl999ops:main

Conversation

@sl999ops

@sl999ops sl999ops commented Jul 26, 2026

Copy link
Copy Markdown

⚠️ 提交说明 / PR Notice

📝 变更描述 / Description

修复图片生成模型可通过 /v1/chat/completions 被当作文本补全转发和计费的问题。

  • 在文本请求校验阶段拒绝图片生成模型访问 Chat Completions,返回明确的 400 错误并提示使用 /v1/images/generations
  • gpt-image-* 识别改为前缀匹配,覆盖 gpt-image-2、模型快照和后续版本;同时覆盖 chatgpt-image-latest 与 DALL·E。
  • 增加回归测试,验证图片模型被拒绝、普通文本模型仍可通过。

校验发生在 token 估算、预扣费和上游转发之前,避免错误端点请求造成额度消耗。

🚀 变更类型 / Type of change

  • 🐛 Bug 修复 (Bug fix)
  • ✨ 新功能 (New feature)
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

Related to #6308.

✅ 提交前检查项 / Checklist

  • 人工确认: 已人工确认
  • 非重复提交: 已检查关联 Issue 与现有实现。
  • Bug fix 说明: 修复公共 Relay 请求中图片模型与 Chat Completions 端点不兼容的问题。
  • 变更理解: 校验在计费和转发前执行,且仅影响已识别的图片生成模型。
  • 范围聚焦: 仅包含模型分类、入口校验和回归测试。
  • 本地验证: go test ./relay/helper -run 'TestChatCompletionsRejectsImageGenerationModels|TestChatCompletionsAcceptsTextModel' -count=1 通过;其余后端包测试也已通过。
  • 安全合规: 未包含凭据或用户数据。

📸 运行证明 / Proof of Work

修复后,以下请求会在 Relay 入口返回 400,而不会选择渠道或扣费:

POST /v1/chat/completions
{ "model": "gpt-image-2", "stream": true, "messages": [{"role":"user","content":"Generate an image"}] }

错误信息:model "gpt-image-2" is not supported on /v1/chat/completions; use /v1/images/generations instead

📸 运行证明 / Proof of Work

使用 gpt-image-2 请求 /v1/chat/completions,现在在 Relay 入口返回 400,
未进入上游转发或计费流程。

9dec06f3-2c0d-44af-bf8d-9bef5dabecb1

本地回归测试:

ok github.com/QuantumNous/new-api/relay/helper

<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

## Summary by CodeRabbit

- **Bug Fixes**
  - Improved recognition of “response-only” OpenAI image model identifiers (including a broader image prefix pattern and the latest image model name).
  - Requests to **POST /v1/chat/completions** using image-generation models are now rejected with guidance to use **POST /v1/images/generations** instead.
- **Tests**
  - Added coverage to confirm chat-completions rejects image-generation models and accepts text models as expected.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

@coderabbitai

coderabbitai Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 49e4db53-c916-4f14-88e1-a8ebb22e8ec8

📥 Commits

Reviewing files that changed from the base of the PR and between 1186b91 and 4734f58.

📒 Files selected for processing (3)
  • common/model.go
  • relay/helper/text_endpoint_compatibility_test.go
  • relay/helper/valid_request.go
🚧 Files skipped from review as they are similar to previous changes (3)
  • relay/helper/valid_request.go
  • common/model.go
  • relay/helper/text_endpoint_compatibility_test.go

Walkthrough

Image-generation model patterns now include current image model variants. Chat-completions validation rejects image-generation models with guidance to use the images endpoint, while text-model requests continue to succeed.

Changes

Image model endpoint validation

Layer / File(s) Summary
Image model classification
common/model.go
Updates response-only model patterns to match gpt-image-* names and chatgpt-image-latest.
Chat-completions validation and tests
relay/helper/valid_request.go, relay/helper/text_endpoint_compatibility_test.go
Rejects image-generation models on chat completions, directs callers to the images endpoint, and verifies text-model acceptance.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Poem

A rabbit checked the model line,
Image chats now hop to a proper sign.
Text models pass without delay,
While image requests find their way.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: rejecting image models on chat completions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@sl999ops
sl999ops force-pushed the main branch 2 times, most recently from 7708a1e to 1186b91 Compare July 26, 2026 13:50
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.

1 participant