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(router): deliver the hold-back retrieval error to the client instead of falling back
mateo-berri64d8b24c10a0
diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/agentic_streaming_iterator.py b/litellm/llms/anthropic/experimental_pass_through/messages/agentic_streaming_iterator.pyindex 3d3d3a12b17..55a85c011d0 100644--- a/litellm/llms/anthropic/experimental_pass_through/messages/agentic_streaming_iterator.py+++ b/litellm/llms/anthropic/experimental_pass_through/messages/agentic_streaming_iterator.py@@ -31,6 +31,10 @@ SERVER_FULFILLED_TOOL_LEAK_ERROR_SSE_BYTES: Final = ()+def is_server_fulfilled_tool_leak_error(chunk: object) -> bool:+ return chunk == SERVER_FULFILLED_TOOL_LEAK_ERROR_SSE_BYTES++async def _anext_or_none(iterator: AsyncIterator) -> bytes | None:try:return await iterator.__anext__()diff --git a/litellm/router.py b/litellm/router.pyindex 4cceecf50d6..b99eb2598c1 100644--- a/litellm/router.py+++ b/litellm/router.py@@ -398,6 +398,16 @@ def _is_retriable_anthropic_status(status_code: int) -> bool:return status_code == 429 or status_code >= 500+def _anthropic_stream_error_is_gateway_verdict(chunk: object) -> bool:+ """AgenticAnthropicStreamingIterator's own retrieval-failure frame is the gateway's verdict, not a provider+ failure: another deployment would rerun the same failed hook, so it reaches the client instead of falling back."""+ from litellm.llms.anthropic.experimental_pass_through.messages.agentic_streaming_iterator import (+ is_server_fulfilled_tool_leak_error,+ )++ return is_server_fulfilled_tool_leak_error(chunk)++def _anthropic_stream_should_decline_fallback(has_generated_content: bool, error: "MidStreamFallbackError") -> bool:"""A MidStreamFallbackError raised directly by the source iterator (the@@ -5018,6 +5028,7 @@ class Router:not has_generated_contentand error_event is not Noneand _is_retriable_anthropic_status(error_event[2])+ and not _anthropic_stream_error_is_gateway_verdict(chunk))if not has_generated_content and not retriable_pending_error and error_event is None:buffered_lifecycle_chunks = (*buffered_lifecycle_chunks, chunk)diff --git a/tests/code_coverage_tests/router_code_coverage.py b/tests/code_coverage_tests/router_code_coverage.pyindex 014f3a16b59..c541c035db7 100644--- a/tests/code_coverage_tests/router_code_coverage.py+++ b/tests/code_coverage_tests/router_code_coverage.py@@ -80,6 +80,7 @@ ignored_function_names = ["_override_vector_store_methods_for_router", # No-op placeholder, called during Router init"_merge_tools_from_deployment", # Tested indirectly via _update_kwargs_with_deployment (test files lack "router" in name)"_invalidate_access_groups_cache", # Tested indirectly via set_model_list, upsert_model etc. (test files lack "router" in name)+ "has_buffered_provider_output", # Property, so its reads in test_router.py are never an ast.Call]diff --git a/tests/test_litellm/test_router.py b/tests/test_litellm/test_router.pyindex 4b9b91caf2c..25c33c4a202 100644--- a/tests/test_litellm/test_router.py+++ b/tests/test_litellm/test_router.py@@ -15,11 +15,15 @@ import litellmfrom litellm import Routerfrom litellm.exceptions import MidStreamFallbackErrorfrom litellm.integrations.custom_logger import CustomLogger+from litellm.llms.anthropic.experimental_pass_through.messages.agentic_streaming_iterator import (+ SERVER_FULFILLED_TOOL_LEAK_ERROR_SSE_BYTES,+)from litellm.router import (MAX_BUFFERED_PRE_CONTENT_ANTHROPIC_CHUNKS,FallbackAwareAnthropicMessagesStream,_anthropic_stream_commits_now,_anthropic_stream_should_decline_fallback,+ _anthropic_stream_error_is_gateway_verdict,_anthropic_stream_forwards_ping_live,_anthropic_stream_should_drop_pre_content_ping,_is_retriable_anthropic_status,@@ -9545,6 +9549,30 @@ async def test_anthropic_messages_leading_ping_does_not_disqualify_fallback():]+async def test_anthropic_messages_hold_back_retrieval_failure_reaches_client_without_fallback():+ """The hold-back iterator's own retrieval-failure frame is the gateway's verdict, not a+ provider failure: a configured fallback stays untouched and the client reads the error+ right after the live keepalive."""+ router = _anthropic_messages_make_router()+ source = _AnthropicMessagesFakeByteStream(+ [_anthropic_messages_ping_chunk(), SERVER_FULFILLED_TOOL_LEAK_ERROR_SSE_BYTES]+ )+ fallback = AsyncMock(+ return_value=_AnthropicMessagesFallbackByteStream([_anthropic_messages_content_chunk("fallback answer")])+ )++ with patch.object(router, "async_function_with_fallbacks_common_utils", new=fallback):+ wrapped = await router._aanthropic_messages_streaming_iterator(+ response=source,+ initial_kwargs={"model": "primary"},+ )+ collected = [chunk async for chunk in wrapped]++ fallback.assert_not_called()+ assert collected == [_anthropic_messages_ping_chunk(), SERVER_FULFILLED_TOOL_LEAK_ERROR_SSE_BYTES]++@pytest.mark.asyncioasync def test_anthropic_messages_pre_content_buffer_cap_forces_commit():"""Bugbot regression: a hostile or pathological upstream that never emits@@ -9883,6 +9911,12 @@ def test_anthropic_stream_forwards_ping_live_direct_call():assert _anthropic_stream_forwards_ping_live(content, has_generated_content=False, buffered_chunk_count=0) is False+def test_anthropic_stream_error_is_gateway_verdict_direct_call():+ assert _anthropic_stream_error_is_gateway_verdict(SERVER_FULFILLED_TOOL_LEAK_ERROR_SSE_BYTES) is True+ assert _anthropic_stream_error_is_gateway_verdict(_anthropic_messages_overloaded_error_chunk()) is False+ assert _anthropic_stream_error_is_gateway_verdict(_anthropic_messages_ping_chunk()) is False++def test_fallback_aware_stream_reports_withheld_output_of_its_current_source():"""The proxy's cancel-refund guard reads this flag off the router wrapper, so itmust reflect the stream actually being drained: the primary, then the fallback."""