fix(responses): clear the PIE804 delta and isolate the tool-call cache in tests

- build the deferred reasoning item with model_validate and reuse
  create_output_item_added_event() for the message item: both avoid the
  `**{...}` dict unpacking that pushed PIE804 past its ceiling
- drop the cached tool-use definition around the function_call_output
  normalization tests: TOOL_CALLS_CACHE is module-level state and xdist
  skips the per-module litellm reload, so a tool call cached by an
  earlier test made the transformation return an extra assistant turn
This commit is contained in:
liracle 2026-08-29 12:40:42 +08:00
parent 4afeaeceea
commit 14433e6d01
2 changed files with 25 additions and 19 deletions

View file

@ -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()

View file

@ -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={