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(responses): init completed_response on bridge streaming iterator (#35413)

devin-ai-integration[bot]7a55ca811b6d
diff --git a/litellm/responses/litellm_completion_transformation/streaming_iterator.py b/litellm/responses/litellm_completion_transformation/streaming_iterator.py
index ddd05075763..5a7380a55b8 100644
--- a/litellm/responses/litellm_completion_transformation/streaming_iterator.py
+++ b/litellm/responses/litellm_completion_transformation/streaming_iterator.py
@@ -82,6 +82,7 @@ class LiteLLMCompletionStreamingIterator(ResponsesAPIStreamingIterator):
self.sent_output_item_done_event: bool = False
self.sent_annotation_events: bool = False
self.litellm_model_response: ModelResponse | TextCompletionResponse | None = None
+ self.completed_response: Any = None
self.final_text: str = ""
self._cached_item_id: str | None = None
self._cached_response_id: str | None = None
diff --git a/tests/router_unit_tests/test_router_aresponses_streaming_fallback.py b/tests/router_unit_tests/test_router_aresponses_streaming_fallback.py
index 2fb7bdfceb5..17124a94a8f 100644
--- a/tests/router_unit_tests/test_router_aresponses_streaming_fallback.py
+++ b/tests/router_unit_tests/test_router_aresponses_streaming_fallback.py
@@ -90,6 +90,35 @@ def test_extract_partial_responses_usage_no_completed_response():
assert usage is None
+def test_extract_partial_responses_usage_bridge_iterator_no_completed_response():
+ """
+ Regression for #35411: the bridge iterator
+ (LiteLLMCompletionStreamingIterator) overrides __init__ without calling
+ super().__init__(), so completed_response was never set until the stream
+ reached RESPONSE_COMPLETED. On a mid-stream provider error (before
+ completion) the fallback recovery path read source_iterator.completed_response
+ and raised AttributeError, masking the real provider error and bypassing
+ fallbacks. The attribute must always exist and default to None.
+ """
+ from litellm.responses.litellm_completion_transformation.streaming_iterator import (
+ LiteLLMCompletionStreamingIterator,
+ )
+
+ wrapper = MagicMock()
+ wrapper.logging_obj = MagicMock()
+ iterator = LiteLLMCompletionStreamingIterator(
+ model="anthropic/claude-sonnet-4-5",
+ litellm_custom_stream_wrapper=wrapper,
+ request_input="hi",
+ responses_api_request={},
+ )
+
+ assert iterator.completed_response is None
+ # No chat chunks collected yet and no completed_response → must return
+ # None instead of raising AttributeError.
+ assert Router._extract_partial_responses_usage(iterator) is None
+
+
# -------- _combine_responses_fallback_usage --------