fix(providers): honor configured fallbacks on "insufficient credits"

An OpenAI-compatible gateway can report exhausted credits as HTTP 400 with
the message "You have insufficient credits to make this request". The
arrearage marker lists cover "insufficient_quota", "insufficient balance"
and "out of credits", but not "insufficient credits", so
is_arrearage_response() returned False and _should_fallback() fell through
to its blanket HTTP-400 short-circuit. Configured fallbackModels were then
silently skipped and the raw provider error was returned to the user.

Add the missing phrasing to both the text-marker tuple and the structured
token frozenset. Arrearage detection already runs first inside
_should_fallback(), so recognizing the phrasing is enough to restore
failover; a plain malformed-request 400 keeps its existing non-fallback
behavior.

Refs #5967
This commit is contained in:
Carmelo Campos authored and chengyongru committed 2026-09-30 09:55:31 +08:00
1 parent 66f5f2df15
commit 663ba82f00
2 files changed
+53

No files matched your search

+3
View File
@@ -665,6 +665,7 @@ class LLMProvider(ABC):
"quota_exhausted",
"billing_hard_limit_reached",
"insufficient_balance",
"insufficient_credits",
"credit_balance_too_low",
"billing_not_active",
"payment_required",
@@ -687,6 +688,8 @@ class LLMProvider(ABC):
"billing not active",
"insufficient balance",
"insufficient_balance",
"insufficient credits",
"insufficient_credits",
"credit balance too low",
"payment required",
"out of credits",
+50
View File
@@ -1458,6 +1458,56 @@ class TestFailoverOnArrearageError:
assert result.content == "fallback ok"
factory.assert_called_once_with(fallback_preset)
@pytest.mark.asyncio
async def test_insufficient_credits_on_400_tries_configured_fallback(self) -> None:
"""Some OpenAI-compatible gateways report exhausted credits as HTTP 400."""
arrearage = _make_response(
"[400]: You have insufficient credits to make this request.",
finish_reason="error",
error_status_code=400,
error_code="BAD_REQUEST",
error_type="invalid_request_error",
error_should_retry=False,
)
primary = _FakeProvider("primary", arrearage)
fallback = _FakeProvider("fallback", _make_response("fallback ok"))
fallback_preset = _fallback("fallback-a")
factory = MagicMock(return_value=fallback)
fb = FallbackProvider(
primary=primary,
fallback_presets=[fallback_preset],
provider_factory=factory,
)
result = await fb.chat(messages=[{"role": "user", "content": "hi"}])
assert result.content == "fallback ok"
factory.assert_called_once_with(fallback_preset)
@pytest.mark.asyncio
async def test_plain_bad_request_still_does_not_fall_back(self) -> None:
"""A 400 with no billing semantics stays non-fallbackable."""
bad_request = _make_response(
"invalid parameter: temperature must be <= 2",
finish_reason="error",
error_status_code=400,
error_code="BAD_REQUEST",
error_type="invalid_request_error",
error_should_retry=False,
)
primary = _FakeProvider("primary", bad_request)
factory = MagicMock()
fb = FallbackProvider(
primary=primary,
fallback_presets=[_fallback("fallback-a")],
provider_factory=factory,
)
result = await fb.chat(messages=[{"role": "user", "content": "hi"}])
assert result is bad_request
factory.assert_not_called()
@pytest.mark.asyncio
async def test_without_fallback_presets_returns_original_error(self) -> None:
arrearage = _make_response(