diff --git a/litellm/responses/litellm_completion_transformation/streaming_iterator.py b/litellm/responses/litellm_completion_transformation/streaming_iterator.py index de287e4b985..25e83b1cdc7 100644 --- a/litellm/responses/litellm_completion_transformation/streaming_iterator.py +++ b/litellm/responses/litellm_completion_transformation/streaming_iterator.py @@ -838,11 +838,15 @@ class LiteLLMCompletionStreamingIterator(ResponsesAPIStreamingIterator): # strict consumers require a sequence, and null / missing # / empty lists (omitted by pydantic serialization) cause # the whole added frame to be rejected. + # model_validate (rather than `**{...}`) so the fields read + # as a payload, keeping the strict-ruff kwargs budget clear; + # the model allows extras, so the validation result is the + # same object the constructor would build. _reasoning_added: Final = OutputItemAddedEvent( type=ResponsesAPIStreamEvents.OUTPUT_ITEM_ADDED, output_index=0, - item=BaseLiteLLMOpenAIResponseObject( - **{ + item=BaseLiteLLMOpenAIResponseObject.model_validate( + { "id": self._cached_reasoning_item_id, "type": "reasoning", "status": "in_progress", @@ -902,23 +906,7 @@ class LiteLLMCompletionStreamingIterator(ResponsesAPIStreamingIterator): self._reasoning_done_emitted = True self._reasoning_active = False self._message_item_added_emitted = True - self._cached_item_id = self._cached_item_id or f"msg_{uuid.uuid4()}" - self._sequence_number += 1 - _msg_added: Final = OutputItemAddedEvent( - type=ResponsesAPIStreamEvents.OUTPUT_ITEM_ADDED, - output_index=0, - item=BaseLiteLLMOpenAIResponseObject( - **{ - "id": self._cached_item_id, - "type": "message", - "role": "assistant", - "status": "in_progress", - "content": [], - } - ), - ) - _msg_added.__dict__["sequence_number"] = self._sequence_number - self._pending_response_events.append(_msg_added) + self._pending_response_events.append(self.create_output_item_added_event()) if not self.sent_content_part_added_event: self.sent_content_part_added_event = True _content_part_event: Final = self.create_content_part_added_event() diff --git a/tests/test_litellm/responses/litellm_completion_transformation/test_function_call_output_normalization.py b/tests/test_litellm/responses/litellm_completion_transformation/test_function_call_output_normalization.py index 3af1ef51e3e..e617489a556 100644 --- a/tests/test_litellm/responses/litellm_completion_transformation/test_function_call_output_normalization.py +++ b/tests/test_litellm/responses/litellm_completion_transformation/test_function_call_output_normalization.py @@ -6,11 +6,29 @@ as tool/function response parts; if the tool output is passed as a list of input we normalize it to text/image blocks or a string. """ +import pytest + from litellm.responses.litellm_completion_transformation.transformation import ( + TOOL_CALLS_CACHE, LiteLLMCompletionResponsesConfig, ) +@pytest.fixture(autouse=True) +def clear_cached_tool_use_definition(): + """ + TOOL_CALLS_CACHE is module-level state shared by every test in a worker: + transforming a completion that carries tool_calls stores the definition + under the call id, and this transformation prepends that assistant turn + when the ids match. Under xdist there is no per-module litellm reload to + clear it, so drop the id around each test -- otherwise the message count + below depends on whichever tests happened to run earlier in the worker. + """ + TOOL_CALLS_CACHE.delete_cache(key="call_1") + yield + TOOL_CALLS_CACHE.delete_cache(key="call_1") + + def test_function_call_output_list_input_text_is_converted_to_tool_string_content(): out = LiteLLMCompletionResponsesConfig._transform_responses_api_tool_call_output_to_chat_completion_message( tool_call_output={