diff --git a/litellm/responses/streaming_iterator.py b/litellm/responses/streaming_iterator.py index 8c7ba9350d8..48d5af58aad 100644 --- a/litellm/responses/streaming_iterator.py +++ b/litellm/responses/streaming_iterator.py @@ -418,9 +418,7 @@ class BaseResponsesAPIStreamingIterator: # missing/invalid/duplicate index: append after the highest known # index so the fallback can never collide with a real index and # silently discard a streamed item - _fallback_index: Final = ( - max(self._streamed_output_items.keys(), default=-1) + 1 - ) + _fallback_index: Final = max(self._streamed_output_items.keys(), default=-1) + 1 self._streamed_output_items[_fallback_index] = _done_item elif _event_type == ResponsesAPIStreamEvents.OUTPUT_TEXT_ANNOTATION_ADDED: _annotation: Final[object] = getattr(openai_responses_api_chunk, "annotation", None) diff --git a/tests/test_litellm/test_responses_api_bridge_non_stream.py b/tests/test_litellm/test_responses_api_bridge_non_stream.py index c438a409f0a..358134fb5e0 100644 --- a/tests/test_litellm/test_responses_api_bridge_non_stream.py +++ b/tests/test_litellm/test_responses_api_bridge_non_stream.py @@ -65,6 +65,7 @@ def test_should_collect_response_from_stream(): assert collected.id == "resp-1" assert collected._hidden_params.get("headers") == {"x-test": "1"} + class _RecoverableResponsesStream(_FakeResponsesStream): """Terminal response.completed carries an empty output array even though output_item.done events streamed the full answer (issue #41009).""" @@ -104,9 +105,7 @@ def test_collect_response_from_stream_rebuilds_empty_output_from_streamed_items( "type": "message", "status": "completed", "role": "assistant", - "content": [ - {"type": "output_text", "text": "Hi! How can I help?", "annotations": []} - ], + "content": [{"type": "output_text", "text": "Hi! How can I help?", "annotations": []}], } ] stream = _RecoverableResponsesStream(response, streamed_items) @@ -133,9 +132,7 @@ async def test_collect_response_from_stream_async_rebuilds_empty_output_from_str "type": "message", "status": "completed", "role": "assistant", - "content": [ - {"type": "output_text", "text": "Hi! How can I help?", "annotations": []} - ], + "content": [{"type": "output_text", "text": "Hi! How can I help?", "annotations": []}], } ] stream = _RecoverableAsyncResponsesStream(response, streamed_items)