sub2api icon indicating copy to clipboard operation
sub2api copied to clipboard

bug(openai): reject gpt-image models on chat completions before account scheduling

Open okiddo opened this issue 4 months ago • 0 comments

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

  1. Configure at least two OpenAI OAuth accounts that are eligible for native image requests.
  2. 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
}
  1. 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.
  • isOpenAICodexPlanGatedModelError classifies the response as a deterministic plan-gated model error.
  • HandleUpstreamModelNotFound stores a 30-minute model cooldown with reason upstream_400_codex_plan_gated_model and returns true, allowing failover to continue across other accounts.
  • The persisted gpt-image-2 cooldown 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 SetModelRateLimit or otherwise mutate account state;
  • affect /v1/images/generations or /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:

  1. gpt-image-1, gpt-image-1.5, and gpt-image-2 sent to /v1/chat/completions return HTTP 400 with invalid_request_error.
  2. The rejection occurs before the gateway or scheduler is invoked and does not call SetModelRateLimit.
  3. Native image generation and edit endpoints remain unchanged.
  4. /v1/responses with a text model and the image_generation tool remains supported.
  5. The existing Codex plan-gated cooldown behavior for unsupported text models continues to work.

okiddo avatar Jul 15 '26 08:07 okiddo