Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(router): surface clean RateLimitError on mid-stream 429 with no fallbacks (#31298)

Yassin Kortam248389c27651
diff --git a/litellm/router.py b/litellm/router.py
index 0008fcaa30b..1aba259a328 100644
--- a/litellm/router.py
+++ b/litellm/router.py
@@ -2323,6 +2323,14 @@ class Router:
verbose_router_logger.error(
f"Fallback also failed: {fallback_error}"
)
+ # No fallback handled the mid-stream error, so surface the
+ # real provider exception (e.g. RateLimitError) instead of
+ # leaking the internal MidStreamFallbackError to the client
+ if (
+ isinstance(fallback_error, MidStreamFallbackError)
+ and fallback_error.original_exception is not None
+ ):
+ raise fallback_error.original_exception from fallback_error
raise fallback_error
finally:
# Close the underlying streams to release HTTP connections
@@ -2754,6 +2762,11 @@ class Router:
verbose_router_logger.error(
f"Responses streaming fallback also failed: {fallback_error}"
)
+ if (
+ isinstance(fallback_error, MidStreamFallbackError)
+ and fallback_error.original_exception is not None
+ ):
+ raise fallback_error.original_exception from fallback_error
raise fallback_error
finally:
with anyio.CancelScope(shield=True):
@@ -2890,6 +2903,11 @@ class Router:
verbose_router_logger.error(
f"Fallback also failed: {fallback_error}"
)
+ if (
+ isinstance(fallback_error, MidStreamFallbackError)
+ and fallback_error.original_exception is not None
+ ):
+ raise fallback_error.original_exception from fallback_error
raise fallback_error
finally:
if hasattr(model_response, "close"):
diff --git a/tests/test_litellm/test_router.py b/tests/test_litellm/test_router.py
index 470d38caf10..7be176fffc7 100644
--- a/tests/test_litellm/test_router.py
+++ b/tests/test_litellm/test_router.py
@@ -1,3 +1,4 @@
+import asyncio
import copy
import json
import os
@@ -12,6 +13,7 @@ sys.path.insert(
import litellm
+from litellm.exceptions import MidStreamFallbackError
def test_update_kwargs_does_not_mutate_defaults_and_merges_metadata():
@@ -2409,6 +2411,177 @@ async def test_aresponses_streaming_iterator_combines_partial_usage():
assert merged.total_tokens == 49
+def _midstream_rate_limit_error():
+ rate_limit_error = litellm.RateLimitError(
+ message="vertex_ai_betaException - Resource exhausted.",
+ model="gemini",
+ llm_provider="vertex_ai_beta",
+ )
+ midstream_error = MidStreamFallbackError(
+ message=str(rate_limit_error),
+ model="gemini",
+ llm_provider="vertex_ai_beta",
+ original_exception=rate_limit_error,
+ is_pre_first_chunk=True,
+ )
+ return rate_limit_error, midstream_error
+
+
+async def test_acompletion_streaming_iterator_surfaces_rate_limit_without_fallbacks():
+ """Regression for #26015: a mid-stream 429 with no fallbacks configured must
+ surface a clean RateLimitError, not leak the internal MidStreamFallbackError
+ wrapper to the client, and must terminate instead of hanging."""
+ rate_limit_error, midstream_error = _midstream_rate_limit_error()
+
+ router = litellm.Router(
+ model_list=[
+ {
+ "model_name": "gemini",
+ "litellm_params": {
+ "model": "vertex_ai/gemini-2.0-flash",
+ "api_key": "fake-key",
+ },
+ },
+ ],
+ num_retries=0,
+ )
+
+ class _RaisingStream:
+ def __init__(self):
+ self.chunks = []
+
+ def __aiter__(self):
+ return self
+
+ async def __anext__(self):
+ raise midstream_error
+
+ stream = _RaisingStream()
+ setattr(stream, "model", "gemini")
+ setattr(stream, "custom_llm_provider", "vertex_ai_beta")
+ setattr(stream, "logging_obj", MagicMock())
+
+ with patch.object(
+ router,
+ "async_function_with_fallbacks_common_utils",
+ new=AsyncMock(side_effect=midstream_error),
+ ):
+ result = await router._acompletion_streaming_iterator(
+ model_response=stream,
+ messages=[{"role": "user", "content": "Hello"}],
+ initial_kwargs={"model": "gemini", "stream": True},
+ )
+
+ async def _consume():
+ async for _ in result:
+ pass
+
+ with pytest.raises(litellm.RateLimitError) as exc_info:
+ await asyncio.wait_for(_consume(), timeout=10)
+
+ assert not isinstance(exc_info.value, MidStreamFallbackError)
+ assert exc_info.value.status_code == 429
+ assert exc_info.value is rate_limit_error
+
+
+def test_completion_streaming_iterator_surfaces_rate_limit_without_fallbacks():
+ """Sync counterpart of
+ test_acompletion_streaming_iterator_surfaces_rate_limit_without_fallbacks."""
+ rate_limit_error, midstream_error = _midstream_rate_limit_error()
+
+ router = litellm.Router(
+ model_list=[
+ {
+ "model_name": "gemini",
+ "litellm_params": {
+ "model": "vertex_ai/gemini-2.0-flash",
+ "api_key": "fake-key",
+ },
+ },
+ ],
+ num_retries=0,
+ )
+
+ class _RaisingSyncStream:
+ def __init__(self):
+ self.model = "gemini"
+ self.custom_llm_provider = "vertex_ai_beta"
+ self.logging_obj = MagicMock()
+ self.chunks = []
+
+ def __iter__(self):
+ return self
+
+ def __next__(self):
+ raise midstream_error
+
+ with patch.object(
+ router,
+ "function_with_fallbacks",
+ side_effect=midstream_error,
+ ):
+ result = router._completion_streaming_iterator(
+ model_response=_RaisingSyncStream(),
+ messages=[{"role": "user", "content": "Hello"}],
+ initial_kwargs={"model": "gemini", "stream": True},
+ )
+
+ with pytest.raises(litellm.RateLimitError) as exc_info:
+ list(result)
+
+ assert not isinstance(exc_info.value, MidStreamFallbackError)
+ assert exc_info.value.status_code == 429
+ assert exc_info.value is rate_limit_error
+
+
+async def test_aresponses_streaming_iterator_surfaces_rate_limit_without_fallbacks():
+ """Responses-API counterpart of
+ test_acompletion_streaming_iterator_surfaces_rate_limit_without_fallbacks."""
+ rate_limit_error, midstream_error = _midstream_rate_limit_error()
+
+ router = litellm.Router(
+ model_list=[
+ {
+ "model_name": "gemini",
+ "litellm_params": {
+ "model": "vertex_ai/gemini-2.0-flash",
+ "api_key": "fake-key",
+ },
+ },
+ ],
+ num_retries=0,
+ )
+ src = _make_responses_iterator(error=midstream_error, model="gemini")
+
+ with patch.object(
+ router,
+ "async_function_with_fallbacks_common_utils",
+ new=AsyncMock(side_effect=midstream_error),
+ ):
+ wrapped = await router._aresponses_streaming_iterator(
+ response=src,
+ initial_kwargs={
+ "model": "gemini",
+ "stream": True,
+ "input": "Hello",
+ "original_generic_function": litellm.aresponses,
+ },
+ )
+
+ async def _consume():
+ async for _ in wrapped:
+ pass
+
+ with pytest.raises(litellm.RateLimitError) as exc_info:
+ await asyncio.wait_for(_consume(), timeout=10)
+
+ assert not isinstance(exc_info.value, MidStreamFallbackError)
+ assert exc_info.value.status_code == 429
+ assert exc_info.value is rate_limit_error
+
+
@pytest.mark.asyncio
async def test_async_function_with_fallbacks_common_utils():
"""Test the async_function_with_fallbacks_common_utils method"""