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(responses): close the reasoning item before announcing the message item
mateo-berri4468c9fdcb71
diff --git a/litellm/responses/litellm_completion_transformation/streaming_iterator.py b/litellm/responses/litellm_completion_transformation/streaming_iterator.pyindex 0cda83d979d..5173cd04a89 100644--- a/litellm/responses/litellm_completion_transformation/streaming_iterator.py+++ b/litellm/responses/litellm_completion_transformation/streaming_iterator.py@@ -927,12 +927,6 @@ class LiteLLMCompletionStreamingIterator(ResponsesAPIStreamingIterator):def _ensure_output_item_for_chunk(self, chunk: ModelResponseStream) -> None:# Change: Never return a value, just enqueue output item eventsif self.sent_output_item_added_event:- if (- not self.sent_message_item_added_event- and chunk.choices- and self._get_delta_string_from_streaming_choices(chunk.choices)- ):- self._queue_message_item_added_events()returnif not chunk.choices:returndiff --git a/tests/test_litellm/responses/litellm_completion_transformation/test_streaming_iterator_transformation.py b/tests/test_litellm/responses/litellm_completion_transformation/test_streaming_iterator_transformation.pyindex 3581771bc63..8fbba0dbf87 100644--- a/tests/test_litellm/responses/litellm_completion_transformation/test_streaming_iterator_transformation.py+++ b/tests/test_litellm/responses/litellm_completion_transformation/test_streaming_iterator_transformation.py@@ -1058,6 +1058,32 @@ async def test_reasoning_then_text_announces_message_item_before_text_events(synassert announced_indexes_by_item_type["message"] != announced_indexes_by_item_type["reasoning"]+async def test_reasoning_item_closes_before_message_item_opens():+ iterator: Final = _build_iterator(+ [+ _reasoning_chunk("let me think"),+ _chunk("Hello"),+ _chunk("!", finish_reason="stop"),+ ]+ )++ events: Final = await _collect_events(iterator, sync_mode=False)++ item_lifecycle: Final = [+ (event.type, event.item.type)+ for event in events+ if getattr(event, "type", None)+ in (ResponsesAPIStreamEvents.OUTPUT_ITEM_ADDED, ResponsesAPIStreamEvents.OUTPUT_ITEM_DONE)+ ]+ assert item_lifecycle == [+ (ResponsesAPIStreamEvents.OUTPUT_ITEM_ADDED, "reasoning"),+ (ResponsesAPIStreamEvents.OUTPUT_ITEM_DONE, "reasoning"),+ (ResponsesAPIStreamEvents.OUTPUT_ITEM_ADDED, "message"),+ (ResponsesAPIStreamEvents.OUTPUT_ITEM_DONE, "message"),+ ]++@pytest.mark.parametrize("sync_mode", [True, False])@pytest.mark.asyncioasync def test_tool_then_reasoning_then_text_gives_message_its_own_output_index(sync_mode: bool):