From fc7c3e368e469ef4e8e26237eab17113c60c7c65 Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Wed, 28 Oct 2026 03:06:42 -0700 Subject: [PATCH] fix(responses): count a stream of tool arguments as a started tool call --- litellm/responses/streaming_iterator.py | 1 + .../base_llm/responses/test_transformation.py | 46 +++++++++---------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/litellm/responses/streaming_iterator.py b/litellm/responses/streaming_iterator.py index 86d0019d02e..ec5dbc09069 100644 --- a/litellm/responses/streaming_iterator.py +++ b/litellm/responses/streaming_iterator.py @@ -486,6 +486,7 @@ class BaseResponsesAPIStreamingIterator: if isinstance(_delta, str): self._generated_content += _delta elif _event_type in _TOOL_ARGUMENTS_DELTA_EVENTS: + self._started_tool_call = True _args_delta: Final = getattr(openai_responses_api_chunk, "delta", None) if isinstance(_args_delta, str): self._generated_tool_arguments += _args_delta diff --git a/tests/unit/llms/base_llm/responses/test_transformation.py b/tests/unit/llms/base_llm/responses/test_transformation.py index 5b1672ce34e..8ef420219ae 100644 --- a/tests/unit/llms/base_llm/responses/test_transformation.py +++ b/tests/unit/llms/base_llm/responses/test_transformation.py @@ -1576,28 +1576,27 @@ async def test_responses_fallback_surfaces_the_primary_error_once_the_cut_stream router.discard() -def _openai_stream_cut_mid_tool_call(preamble: str) -> httpx.Response: - return _sse( - { - "type": "response.output_text.delta", "sequence_number": 0, - "item_id": "msg_primary", "output_index": 0, "content_index": 0, "delta": preamble, +def _openai_stream_cut_mid_tool_call(preamble: str, *, announce_item: bool = True) -> httpx.Response: + text: Final = { + "type": "response.output_text.delta", "sequence_number": 0, + "item_id": "msg_primary", "output_index": 0, "content_index": 0, "delta": preamble, + } + item_added: Final = { + "type": "response.output_item.added", "sequence_number": 1, "output_index": 1, + "item": { + "type": "function_call", "id": "fc_primary", "call_id": "call_primary", + "name": "lookup", "arguments": "", "status": "in_progress", }, - { - "type": "response.output_item.added", "sequence_number": 1, "output_index": 1, - "item": { - "type": "function_call", "id": "fc_primary", "call_id": "call_primary", - "name": "lookup", "arguments": "", "status": "in_progress", - }, - }, - { - "type": "response.function_call_arguments.delta", "sequence_number": 2, - "item_id": "fc_primary", "output_index": 1, "delta": '{"city": "Par', - }, - { - "type": "error", "sequence_number": 3, - "error": {"type": "server_error", "code": "internal_error", "message": "interrupted"}, - }, - ) + } + arguments: Final = { + "type": "response.function_call_arguments.delta", "sequence_number": 2, + "item_id": "fc_primary", "output_index": 1, "delta": '{"city": "Par', + } + error: Final = { + "type": "error", "sequence_number": 3, + "error": {"type": "server_error", "code": "internal_error", "message": "interrupted"}, + } + return _sse(*((text, item_added, arguments, error) if announce_item else (text, arguments, error))) def _anthropic_stream_cut_mid_tool_call(preamble: str) -> httpx.Response: @@ -1618,12 +1617,13 @@ def _anthropic_stream_cut_mid_tool_call(preamble: str) -> httpx.Response: @pytest.mark.asyncio +@pytest.mark.parametrize("announce_item", [True, False], ids=["announced_item", "arguments_only"]) async def test_responses_fallback_surfaces_the_primary_error_once_the_cut_stream_began_a_tool_call( - respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch + announce_item: bool, respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch ) -> None: monkeypatch.setattr(litellm, "disable_aiohttp_transport", True) respx_mock.post("https://api.openai.com/v1/responses").mock( - return_value=_openai_stream_cut_mid_tool_call("Let me look that up. ") + return_value=_openai_stream_cut_mid_tool_call("Let me look that up. ", announce_item=announce_item) ) fallback: Final = respx_mock.post("https://api.anthropic.com/v1/messages").mock( return_value=_anthropic_stream("It is sunny in Paris.")