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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
Fix: trigger fallbacks on mid-stream httpx.TimeoutException
Ryan Crabbe35133e35504b
diff --git a/litellm/litellm_core_utils/streaming_handler.py b/litellm/litellm_core_utils/streaming_handler.pyindex e281b172685..fa7faf3035d 100644--- a/litellm/litellm_core_utils/streaming_handler.py+++ b/litellm/litellm_core_utils/streaming_handler.py@@ -2244,7 +2244,7 @@ class CustomStreamWrapper:asyncio.create_task(self.logging_obj.async_failure_handler(e, traceback_exception))- raise e+ self._handle_stream_fallback_error(e)except Exception as e:traceback_exception = traceback.format_exc()if self.logging_obj is not None:diff --git a/tests/test_litellm/litellm_core_utils/test_streaming_handler.py b/tests/test_litellm/litellm_core_utils/test_streaming_handler.pyindex d6281703a0a..49d3c51e340 100644--- a/tests/test_litellm/litellm_core_utils/test_streaming_handler.py+++ b/tests/test_litellm/litellm_core_utils/test_streaming_handler.py@@ -878,6 +878,39 @@ def test_sync_streaming_bad_request_not_midstream(logging_obj: Logging):assert "invalid maxOutputTokens" in str(excinfo.value)+async def test_async_streaming_read_timeout_triggers_midstream_fallback(+ logging_obj: Logging,+):+ """A mid-stream httpx.ReadTimeout must wrap into MidStreamFallbackError so+ the Router's FallbackStreamWrapper can switch to a fallback model.++ Previously __anext__ caught httpx.TimeoutException and re-raised it raw,+ which bypassed _handle_stream_fallback_error and prevented stream_timeout+ from triggering fallbacks the way connection-phase timeout does.+ """+ import httpx++ from litellm.exceptions import MidStreamFallbackError++ async def _raise_read_timeout(**kwargs):+ raise httpx.ReadTimeout("Timeout on reading data from socket")++ response = CustomStreamWrapper(+ completion_stream=None,+ model="gpt-4",+ logging_obj=logging_obj,+ custom_llm_provider="openai",+ make_call=_raise_read_timeout,+ )++ with pytest.raises(MidStreamFallbackError) as excinfo:+ await response.__anext__()++ assert excinfo.value.is_pre_first_chunk is True+ assert isinstance(excinfo.value.original_exception, Exception)++def test_streaming_handler_with_created_time_propagation(initialized_custom_stream_wrapper: CustomStreamWrapper, logging_obj: Logging):