Skip to content

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]

Merge remote-tracking branch 'upstream/main' into pr-22553

David Steele565a52780bef
diff --cc tests/test_litellm/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py
index 715d3f7b062,f696859b082..490f7e7da62
--- a/tests/test_litellm/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py
+++ b/tests/test_litellm/completion_extras/litellm_responses_transformation/test_completion_extras_litellm_responses_transformation_transformation.py
@@@ -1324,151 -1317,95 +1324,244 @@@ def test_transform_response_preserves_a
assert result.usage.completion_tokens == 20
assert result.usage.total_tokens == 30
+ print("✓ Annotations from Responses API are correctly preserved in Chat Completions format")
+
+def test_multi_tool_call_stream_no_premature_finish():
+ """
+ Regression test for multi-tool-call streaming bug.
+
+ When a response contains multiple tool calls, the stream used to be prematurely
+ terminated after the first output_item.done event because that handler emitted
+ finish_reason="tool_calls". This caused ~58% of streaming requests with multiple
+ tool calls to fail.
+
+ The fix: output_item.done for function_call emits delta=Delta() and finish_reason=None.
+ Only response.completed emits the terminal finish_reason.
+
+ Synthetic event sequence:
+ response.created
+ response.output_item.added (function_call: read_file, call_id: call_1)
+ response.function_call_arguments.delta (read_file args)
+ response.output_item.done (function_call: read_file) <- must NOT end stream
+ response.output_item.added (function_call: list_dir, call_id: call_2)
+ response.function_call_arguments.delta (list_dir args)
+ response.output_item.done (function_call: list_dir) <- must NOT end stream
+ response.completed (response with 2 function_call outputs) <- terminal
+ """
+ from litellm.completion_extras.litellm_responses_transformation.transformation import (
+ OpenAiResponsesToChatCompletionStreamIterator,
+ )
+
+ iterator = OpenAiResponsesToChatCompletionStreamIterator(streaming_response=None, sync_stream=True)
+
+ chunks = [
+ # 0: response created
+ {"type": "response.created", "response": {"id": "resp_001", "status": "in_progress"}},
+ # 1: first tool call added
+ {
+ "type": "response.output_item.added",
+ "item": {"type": "function_call", "name": "read_file", "call_id": "call_1"},
+ },
+ # 2: first tool call arguments delta
+ {"type": "response.function_call_arguments.delta", "delta": '{"path":"/etc/hostname"}'},
+ # 3: first tool call done ← must NOT emit finish_reason
+ {
+ "type": "response.output_item.done",
+ "item": {
+ "type": "function_call",
+ "name": "read_file",
+ "call_id": "call_1",
+ "arguments": '{"path":"/etc/hostname"}',
+ },
+ },
+ # 4: second tool call added
+ {
+ "type": "response.output_item.added",
+ "item": {"type": "function_call", "name": "list_dir", "call_id": "call_2"},
+ },
+ # 5: second tool call arguments delta
+ {"type": "response.function_call_arguments.delta", "delta": '{"path":"/tmp"}'},
+ # 6: second tool call done ← must NOT emit finish_reason
+ {
+ "type": "response.output_item.done",
+ "item": {
+ "type": "function_call",
+ "name": "list_dir",
+ "call_id": "call_2",
+ "arguments": '{"path":"/tmp"}',
+ },
+ },
+ # 7: response completed with both tool calls in output ← ONLY terminal chunk
+ {
+ "type": "response.completed",
+ "response": {
+ "id": "resp_001",
+ "status": "completed",
+ "output": [
+ {
+ "type": "function_call",
+ "name": "read_file",
+ "call_id": "call_1",
+ "arguments": '{"path":"/etc/hostname"}',
+ },
+ {
+ "type": "function_call",
+ "name": "list_dir",
+ "call_id": "call_2",
+ "arguments": '{"path":"/tmp"}',
+ },
+ ],
+ },
+ },
+ ]
+
+ results = [iterator.chunk_parser(chunk) for chunk in chunks]
+
+ # 1. output_item.done events (indices 3 and 6) must NOT emit finish_reason
+ for done_idx, label in [(3, "read_file done"), (6, "list_dir done")]:
+ r = results[done_idx]
+ assert r is not None, f"{label}: chunk_parser must return a result"
+ assert len(r.choices) > 0, f"{label}: result must have choices"
+ assert r.choices[0].finish_reason is None, (
+ f"{label}: output_item.done must not emit finish_reason (stream would terminate prematurely)"
+ )
+ assert not r.choices[0].delta.tool_calls, (
+ f"{label}: output_item.done must not include a duplicate tool_calls delta"
+ )
+
+ # 2. output_item.added events (indices 1 and 4) should carry name + call_id
+ for added_idx, expected_name, expected_call_id in [
+ (1, "read_file", "call_1"),
+ (4, "list_dir", "call_2"),
+ ]:
+ r = results[added_idx]
+ if r is not None and r.choices and r.choices[0].delta.tool_calls:
+ tc = r.choices[0].delta.tool_calls[0]
+ assert tc.function.name == expected_name, (
+ f"output_item.added for {expected_name}: tool_call name mismatch"
+ )
+ assert tc.id == expected_call_id, (
+ f"output_item.added for {expected_name}: call_id mismatch"
+ )
+
+ # 3. argument delta events (indices 2 and 5) should carry arguments
+ for delta_idx, expected_args, label in [
+ (2, '{"path":"/etc/hostname"}', "read_file args"),
+ (5, '{"path":"/tmp"}', "list_dir args"),
+ ]:
+ r = results[delta_idx]
+ if r is not None and r.choices and r.choices[0].delta.tool_calls:
+ tc = r.choices[0].delta.tool_calls[0]
+ assert tc.function.arguments == expected_args, (
+ f"{label}: argument delta mismatch"
+ )
+
+ # 4. Only response.completed (index 7) emits the terminal finish_reason
+ completed_result = results[7]
+ assert completed_result is not None, "response.completed must return a result"
+ assert len(completed_result.choices) > 0, "response.completed must have choices"
+ assert completed_result.choices[0].finish_reason == "tool_calls", (
+ "response.completed with function_call outputs must emit finish_reason='tool_calls'"
+ )
+
+ # 5. No chunk before the last one should have finish_reason set
+ for idx, r in enumerate(results[:-1]):
+ if r is not None and r.choices:
+ assert r.choices[0].finish_reason is None, (
+ f"Chunk at index {idx} (type={chunks[idx]['type']!r}) must not emit finish_reason "
+ f"— only response.completed should terminate the stream"
+ )
+
+ print("✓ Multi-tool-call stream completes without premature finish_reason termination")
++
++
+ # =============================================================================
+ # Tests for issue #21331: Parallel tool call indices in streaming
+ # =============================================================================
+
+
+ def test_streaming_parallel_tool_calls_have_distinct_indices():
+ """
+ Test that parallel tool calls get distinct indices matching output_index
+ from the Responses API streaming chunks.
+
+ Regression test for issue #21331 where all tool calls were emitted with
+ index=0, making it impossible to distinguish parallel calls.
+ """
+ from litellm.completion_extras.litellm_responses_transformation.transformation import (
+ OpenAiResponsesToChatCompletionStreamIterator,
+ )
+
+ # Simulate two parallel tool calls with output_index 0 and 1
+ chunks = [
+ {
+ "type": "response.output_item.added",
+ "output_index": 0,
+ "item": {
+ "type": "function_call",
+ "id": "fc_001",
+ "call_id": "call_abc",
+ "name": "get_weather",
+ "arguments": "",
+ },
+ },
+ {
+ "type": "response.function_call_arguments.delta",
+ "output_index": 0,
+ "item_id": "fc_001",
+ "delta": '{"city": "SF"}',
+ },
+ {
+ "type": "response.output_item.done",
+ "output_index": 0,
+ "item": {
+ "type": "function_call",
+ "id": "fc_001",
+ "call_id": "call_abc",
+ "name": "get_weather",
+ "arguments": '{"city": "SF"}',
+ },
+ },
+ {
+ "type": "response.output_item.added",
+ "output_index": 1,
+ "item": {
+ "type": "function_call",
+ "id": "fc_002",
+ "call_id": "call_def",
+ "name": "get_weather",
+ "arguments": "",
+ },
+ },
+ {
+ "type": "response.function_call_arguments.delta",
+ "output_index": 1,
+ "item_id": "fc_002",
+ "delta": '{"city": "NY"}',
+ },
+ {
+ "type": "response.output_item.done",
+ "output_index": 1,
+ "item": {
+ "type": "function_call",
+ "id": "fc_002",
+ "call_id": "call_def",
+ "name": "get_weather",
+ "arguments": '{"city": "NY"}',
+ },
+ },
+ ]
+
+ for chunk in chunks:
+ result = OpenAiResponsesToChatCompletionStreamIterator.translate_responses_chunk_to_openai_stream(
+ chunk
+ )
+ expected_index = chunk["output_index"]
+ for choice in result.choices:
+ if choice.delta.tool_calls:
+ for tc in choice.delta.tool_calls:
+ assert tc.index == expected_index, (
+ f"Event {chunk['type']}: expected tool_call.index={expected_index}, "
+ f"got {tc.index}"
+ )