Merge pull request #5395 from ryanchou1994/fix/openai-images-capability-loss-cooldown

fix(openai): 图像能力丢失时冷却 image 调度,不再反复选中坏号
This commit is contained in:
Wesley Liddick
2026-08-28 11:06:22 +08:00
committed by GitHub
4 changed files with 189 additions and 0 deletions
@@ -133,6 +133,17 @@ func (s *OpenAIGatewayService) handleOpenAIAccountUpstreamError(ctx context.Cont
return false
}
// Self-built images requests always carry a matching image_generation tool, so a
// "tool choice not found in 'tools'" 400 means upstream revoked this account's
// image capability. Gated on the self-built marker: passthrough clients control
// their own tools/tool_choice and could otherwise poison a healthy account.
if isOpenAIImagesSelfBuiltRequest(ctx) && isOpenAIImageCapabilityLossError(statusCode, responseBody) {
if s != nil && s.rateLimitService != nil {
_ = s.rateLimitService.HandleOpenAIImageCapabilityLoss(stateCtx, account, statusCode, responseBody)
}
return false
}
if s == nil || account == nil {
return false
}
@@ -335,6 +335,26 @@ func openAIImageUploadToDataURL(upload OpenAIImagesUpload) (string, error) {
return "data:" + contentType + ";base64," + base64.StdEncoding.EncodeToString(upload.Data), nil
}
// openAIImagesSelfBuiltRequestContextKey marks a request whose upstream body was
// fully constructed by buildOpenAIImagesResponsesRequest, i.e. tool_choice and the
// matching image_generation tool are always both present and never client-controlled.
type openAIImagesSelfBuiltRequestContextKey struct{}
func withOpenAIImagesSelfBuiltRequest(ctx context.Context) context.Context {
if ctx == nil {
ctx = context.Background()
}
return context.WithValue(ctx, openAIImagesSelfBuiltRequestContextKey{}, true)
}
func isOpenAIImagesSelfBuiltRequest(ctx context.Context) bool {
if ctx == nil {
return false
}
selfBuilt, _ := ctx.Value(openAIImagesSelfBuiltRequestContextKey{}).(bool)
return selfBuilt
}
func buildOpenAIImagesResponsesRequest(parsed *OpenAIImagesRequest, toolModel string) ([]byte, error) {
if parsed == nil {
return nil, fmt.Errorf("parsed images request is required")
@@ -1786,6 +1806,7 @@ func (s *OpenAIGatewayService) forwardOpenAIImagesOAuth(
if err != nil {
return nil, err
}
upstreamCtx = withOpenAIImagesSelfBuiltRequest(upstreamCtx)
upstreamReq, err := s.buildUpstreamRequest(upstreamCtx, c, account, responsesBody, token, true, parsed.StickySessionSeed(), false)
if err != nil {
return nil, err
@@ -75,6 +75,8 @@ const (
const (
openAIImageRateLimitDefaultCooldown = time.Minute
openAIImageRateLimitReason = "openai_image_rate_limited"
openAIImageCapabilityLossCooldown = 30 * time.Minute
openAIImageCapabilityLossReason = "openai_image_capability_lost"
)
var openAIImageTryAgainPattern = regexp.MustCompile(`(?i)try again in\s+([0-9]+(?:\.[0-9]+)?)\s*(ms|s|sec|secs|second|seconds|m|min|mins|minute|minutes)`)
@@ -2190,6 +2192,44 @@ func (s *RateLimitService) HandleOpenAIImageRateLimit(ctx context.Context, accou
return true
}
func (s *RateLimitService) HandleOpenAIImageCapabilityLoss(ctx context.Context, account *Account, statusCode int, responseBody []byte) bool {
if s == nil || account == nil || s.accountRepo == nil {
return false
}
if account.Platform != PlatformOpenAI {
return false
}
if !account.ShouldHandleErrorCode(statusCode) {
slog.Info("openai_image_capability_loss_skipped_by_error_code_policy", "account_id", account.ID, "status_code", statusCode)
return false
}
if !isOpenAIImageCapabilityLossError(statusCode, responseBody) {
return false
}
resetAt := time.Now().Add(openAIImageCapabilityLossCooldown)
if err := s.accountRepo.SetModelRateLimit(ctx, account.ID, openAIImageGenerationRateLimitKey, resetAt, openAIImageCapabilityLossReason); err != nil {
slog.Warn("openai_image_capability_loss_set_model_rate_limit_failed", "account_id", account.ID, "scope", openAIImageGenerationRateLimitKey, "error", err)
return true
}
slog.Info("openai_image_capability_lost", "account_id", account.ID, "scope", openAIImageGenerationRateLimitKey, "reset_at", resetAt, "reset_in", time.Until(resetAt).Truncate(time.Second))
return true
}
// isOpenAIImageCapabilityLossError reports whether upstream rejected the
// image_generation tool choice that sub2api itself put into the request body.
// Only meaningful for self-built images requests, where tools always carries a
// matching image_generation entry — upstream saying otherwise means the account
// lost the capability.
func isOpenAIImageCapabilityLossError(statusCode int, body []byte) bool {
if statusCode != http.StatusBadRequest || len(body) == 0 {
return false
}
lower := strings.ToLower(string(body))
return strings.Contains(lower, "image_generation") &&
strings.Contains(lower, "not found in 'tools' parameter")
}
func isOpenAIImageRateLimitError(statusCode int, body []byte) bool {
if statusCode != http.StatusTooManyRequests || len(body) == 0 {
return false
@@ -222,3 +222,120 @@ func TestOpenAIGatewayServiceForwardImages_StructuredUnavailableCoolsImageCapabi
require.Equal(t, openAIImagesOAuthUnavailableReason, call.reason)
require.WithinDuration(t, before.Add(openAIImagesOAuthUnavailableCooldown), call.resetAt, time.Second)
}
func TestOpenAIGatewayServiceForwardImages_CapabilityLossCoolsImageScope(t *testing.T) {
gin.SetMode(gin.TestMode)
repo := &modelNotFoundAccountRepoStub{}
body := []byte(`{"model":"gpt-image-2","prompt":"draw a cat"}`)
errorBody := `{"error":{"message":"Tool choice 'image_generation' not found in 'tools' parameter.","param":"tool_choice","type":"invalid_request_error"}}`
req := httptest.NewRequest(http.MethodPost, "/v1/images/generations", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
c, _ := gin.CreateTestContext(rec)
c.Request = req
svc := &OpenAIGatewayService{
rateLimitService: &RateLimitService{accountRepo: repo},
httpUpstream: &httpUpstreamRecorder{
resp: &http.Response{
StatusCode: http.StatusBadRequest,
Header: http.Header{"X-Request-Id": []string{"req_img_capability_lost"}},
Body: io.NopCloser(strings.NewReader(errorBody)),
},
},
}
parsed, err := svc.ParseOpenAIImagesRequest(c, body)
require.NoError(t, err)
account := &Account{
ID: 205,
Name: "openai-oauth",
Platform: PlatformOpenAI,
Type: AccountTypeOAuth,
Credentials: map[string]any{
"access_token": "token-123",
},
}
before := time.Now()
result, err := svc.ForwardImages(context.Background(), c, account, body, parsed, "")
require.Nil(t, result)
require.Error(t, err)
require.Len(t, repo.modelRateLimitCalls, 1)
call := repo.modelRateLimitCalls[0]
require.Equal(t, account.ID, call.accountID)
require.Equal(t, openAIImageGenerationRateLimitKey, call.scope)
require.Equal(t, openAIImageCapabilityLossReason, call.reason)
require.WithinDuration(t, before.Add(openAIImageCapabilityLossCooldown), call.resetAt, time.Second)
}
func TestOpenAIGatewayServiceHandleUpstreamError_PassthroughCapabilityLossDoesNotCool(t *testing.T) {
repo := &modelNotFoundAccountRepoStub{}
svc := &OpenAIGatewayService{rateLimitService: &RateLimitService{accountRepo: repo}}
account := &Account{ID: 206, Platform: PlatformOpenAI, Type: AccountTypeOAuth}
body := []byte(`{"error":{"message":"Tool choice 'image_generation' not found in 'tools' parameter.","param":"tool_choice","type":"invalid_request_error"}}`)
disabled := svc.handleOpenAIAccountUpstreamError(context.Background(), account, http.StatusBadRequest, http.Header{}, body, "gpt-5.5")
require.False(t, disabled)
require.Empty(t, repo.modelRateLimitCalls)
_, wholeAccountBlocked := svc.openaiAccountRuntimeBlockUntil.Load(account.ID)
require.False(t, wholeAccountBlocked)
}
func TestRateLimitServiceHandleOpenAIImageCapabilityLoss_IgnoresGenericBadRequest(t *testing.T) {
repo := &modelNotFoundAccountRepoStub{}
svc := &RateLimitService{accountRepo: repo}
account := &Account{ID: 207, Platform: PlatformOpenAI, Type: AccountTypeOAuth}
body := []byte(`{"error":{"message":"Invalid type for input[0].arguments"}}`)
handled := svc.HandleOpenAIImageCapabilityLoss(context.Background(), account, http.StatusBadRequest, body)
require.False(t, handled)
require.Empty(t, repo.modelRateLimitCalls)
}
func TestRateLimitServiceHandleOpenAIImageCapabilityLoss_RespectsPlatformAndErrorCodePolicy(t *testing.T) {
body := []byte(`{"error":{"message":"Tool choice 'image_generation' not found in 'tools' parameter.","param":"tool_choice","type":"invalid_request_error"}}`)
t.Run("non_openai_platform", func(t *testing.T) {
repo := &modelNotFoundAccountRepoStub{}
svc := &RateLimitService{accountRepo: repo}
account := &Account{ID: 208, Platform: PlatformAnthropic, Type: AccountTypeOAuth}
handled := svc.HandleOpenAIImageCapabilityLoss(context.Background(), account, http.StatusBadRequest, body)
require.False(t, handled)
require.Empty(t, repo.modelRateLimitCalls)
})
t.Run("custom_error_code_policy_excludes_400", func(t *testing.T) {
repo := &modelNotFoundAccountRepoStub{}
svc := &RateLimitService{accountRepo: repo}
account := &Account{
ID: 209,
Platform: PlatformOpenAI,
Type: AccountTypeAPIKey,
Credentials: map[string]any{
"custom_error_codes_enabled": true,
"custom_error_codes": []any{float64(http.StatusTooManyRequests)},
},
}
require.False(t, account.ShouldHandleErrorCode(http.StatusBadRequest))
handled := svc.HandleOpenAIImageCapabilityLoss(context.Background(), account, http.StatusBadRequest, body)
require.False(t, handled)
require.Empty(t, repo.modelRateLimitCalls)
})
}
func TestIsOpenAIImageCapabilityLossError(t *testing.T) {
capabilityLossBody := []byte(`{"error":{"message":"Tool choice 'image_generation' not found in 'tools' parameter.","param":"tool_choice","type":"invalid_request_error"}}`)
genericBadRequestBody := []byte(`{"error":{"message":"Invalid type for input[0].arguments"}}`)
require.True(t, isOpenAIImageCapabilityLossError(http.StatusBadRequest, capabilityLossBody))
require.False(t, isOpenAIImageCapabilityLossError(http.StatusBadRequest, genericBadRequestBody))
require.False(t, isOpenAIImageCapabilityLossError(http.StatusTooManyRequests, capabilityLossBody))
}