bug(openai): reject gpt-image models on chat completions before account scheduling
Summary
An invalid client request can mutate shared scheduler state and temporarily disable otherwise valid native image traffic.
When gpt-image-2 is sent as the top-level model to
POST /v1/chat/completions, the request is forwarded far enough to receive an
upstream HTTP 400 response. That response matches the Codex plan-gated model
error classifier. Sub2API then applies a 30-minute model cooldown to the
selected OAuth account and fails over to another account.
With multiple eligible accounts, the same invalid request can repeat this
process across the pool. Because the cooldown key is the requested model
(gpt-image-2), valid requests to /v1/images/generations and
/v1/images/edits lose the same accounts and may return HTTP 503 until the
cooldowns expire or are cleared.
Affected version
Observed on Sub2API v0.1.155.
Minimal reproduction
- Configure at least two OpenAI OAuth accounts that are eligible for native image requests.
- Send this invalid Chat Completions request:
POST /v1/chat/completions
Content-Type: application/json
Authorization: Bearer YOUR_TEST_API_KEY
{
"model": "gpt-image-2",
"messages": [
{
"role": "user",
"content": "Generate a test image"
}
],
"stream": false
}
- Inspect the account model cooldown state, or immediately send a valid native image request:
POST /v1/images/generations
Content-Type: application/json
Authorization: Bearer YOUR_TEST_API_KEY
{
"model": "gpt-image-2",
"prompt": "A simple test image"
}
Actual behavior
- The invalid Chat Completions request receives an upstream HTTP 400 response.
isOpenAICodexPlanGatedModelErrorclassifies the response as a deterministic plan-gated model error.HandleUpstreamModelNotFoundstores a 30-minute model cooldown with reasonupstream_400_codex_plan_gated_modeland returnstrue, allowing failover to continue across other accounts.- The persisted
gpt-image-2cooldown also removes those accounts from valid native image scheduling. - Valid image generation or edit requests may return HTTP 503 when all eligible accounts have been cooled down.
Expected behavior
The invalid endpoint/model combination should be rejected locally with HTTP 400 before account selection, billing, concurrency acquisition, or upstream dispatch.
The rejection must not:
- retry across OAuth accounts;
- call
SetModelRateLimitor otherwise mutate account state; - affect
/v1/images/generationsor/v1/images/edits; - change the existing handling of genuinely plan-gated text models.
Suggested fix
The smallest fix is an endpoint-boundary validation in
OpenAIGatewayHandler.ChatCompletions, immediately after parsing model and
stream and before content moderation, routing, billing, concurrency, and
account scheduling:
if service.IsImageGenerationIntent("/v1/chat/completions", reqModel, nil) {
h.errorResponse(
c,
http.StatusBadRequest,
"invalid_request_error",
"Image generation models are not supported on /v1/chat/completions; use /v1/images/generations or /v1/images/edits instead",
)
return
}
This keeps the fix scoped to the incompatible Chat Completions route. It should
not reject valid /v1/responses requests that use a text model with the
image_generation tool.
As defense in depth, the upstream error policy could also receive the originating endpoint or request capability. A client-side route mismatch should never be allowed to write a cooldown that is shared with the native image capability.
Regression tests
Please cover the following cases:
gpt-image-1,gpt-image-1.5, andgpt-image-2sent to/v1/chat/completionsreturn HTTP 400 withinvalid_request_error.- The rejection occurs before the gateway or scheduler is invoked and does not
call
SetModelRateLimit. - Native image generation and edit endpoints remain unchanged.
/v1/responseswith a text model and theimage_generationtool remains supported.- The existing Codex plan-gated cooldown behavior for unsupported text models continues to work.