From 663ba82f004700ac8487bd78f08113082e18f233 Mon Sep 17 00:00:00 2001 From: Carmelo Campos Date: Tue, 29 Sep 2026 01:53:39 -0500 Subject: [PATCH] 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 --- nanobot/providers/base.py | 3 ++ tests/agent/test_runner_fallback.py | 50 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/nanobot/providers/base.py b/nanobot/providers/base.py index 4b0824728..26a5efb6b 100644 --- a/nanobot/providers/base.py +++ b/nanobot/providers/base.py @@ -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", diff --git a/tests/agent/test_runner_fallback.py b/tests/agent/test_runner_fallback.py index 4dc9a7773..2f6bf8338 100644 --- a/tests/agent/test_runner_fallback.py +++ b/tests/agent/test_runner_fallback.py @@ -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(