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): guard ambiguous tool-call index reuse
Emerson Gomescf17a440cdb4
diff --git a/litellm/responses/litellm_completion_transformation/streaming_iterator.py b/litellm/responses/litellm_completion_transformation/streaming_iterator.pyindex ea9b8889d39..5c05526442d 100644--- a/litellm/responses/litellm_completion_transformation/streaming_iterator.py+++ b/litellm/responses/litellm_completion_transformation/streaming_iterator.py@@ -89,6 +89,7 @@ class LiteLLMCompletionStreamingIterator(ResponsesAPIStreamingIterator):self._tool_output_index_by_call_id: dict[str, int] = {}self._tool_args_by_call_id: dict[str, str] = {}self._tool_call_id_by_index: dict[int, str] = {}+ self._ambiguous_tool_call_indexes: set[int] = set()self._next_tool_output_index: int = 1 # output_index=0 reserved for the message itemself._final_tool_events_queued: bool = Falseself._sequence_number: int = 0@@ -164,8 +165,15 @@ class LiteLLMCompletionStreamingIterator(ResponsesAPIStreamingIterator):if call_id_raw:call_id = str(call_id_raw)if tc_index is not None:+ existing_call_id = self._tool_call_id_by_index.get(tc_index)+ if existing_call_id is not None and existing_call_id != call_id:+ # Reusing the same index for multiple call_ids is ambiguous for id-less deltas.+ # Guard against silent misrouting by disabling index fallback for this index.+ self._ambiguous_tool_call_indexes.add(tc_index)self._tool_call_id_by_index[tc_index] = call_idelif tc_index is not None:+ if tc_index in self._ambiguous_tool_call_indexes:+ continuemapped_call_id = self._tool_call_id_by_index.get(tc_index)if mapped_call_id:call_id = mapped_call_iddiff --git a/tests/test_litellm/responses/litellm_completion_transformation/test_tool_call_streaming_transformation.py b/tests/test_litellm/responses/litellm_completion_transformation/test_tool_call_streaming_transformation.pyindex 4efdc217dce..071eefaef47 100644--- a/tests/test_litellm/responses/litellm_completion_transformation/test_tool_call_streaming_transformation.py+++ b/tests/test_litellm/responses/litellm_completion_transformation/test_tool_call_streaming_transformation.py@@ -331,3 +331,62 @@ def test_parallel_tool_calls_without_ids_use_index_mapping():assert arguments_by_call_id["call_a"] == '{"x":1}'assert arguments_by_call_id["call_b"] == '{"y":2}'+++def test_reused_index_with_new_call_id_marks_fallback_ambiguous():+ iterator = LiteLLMCompletionStreamingIterator(+ model="test-model",+ litellm_custom_stream_wrapper=AsyncMock(),+ request_input="Test input",+ responses_api_request={},+ )++ iterator._queue_tool_call_delta_events(+ [+ {+ "index": 0,+ "id": "call_a",+ "type": "function",+ "function": {"name": "tool_a", "arguments": '{"a":'},+ }+ ]+ )+ iterator._queue_tool_call_delta_events(+ [+ {+ "index": 0,+ "id": "call_b",+ "type": "function",+ "function": {"name": "tool_b", "arguments": '{"b":'},+ }+ ]+ )+ # Ambiguous chunk: index reused and id missing. We should skip fallback rather than misroute.+ iterator._queue_tool_call_delta_events(+ [+ {+ "index": 0,+ "type": "function",+ "function": {"arguments": "1}"},+ }+ ]+ )++ all_events = []+ while iterator._pending_tool_events:+ all_events.append(iterator._pending_tool_events.pop(0))++ delta_events = [+ evt+ for evt in all_events+ if evt.type == ResponsesAPIStreamEvents.FUNCTION_CALL_ARGUMENTS_DELTA+ ]+ arguments_by_call_id = {}+ for evt in delta_events:+ arguments_by_call_id.setdefault(evt.item_id, "")+ arguments_by_call_id[evt.item_id] += evt.delta++ assert arguments_by_call_id["call_a"] == '{"a":'+ assert arguments_by_call_id["call_b"] == '{"b":'+ assert arguments_by_call_id["call_a"] != '{"a":1}'+ assert arguments_by_call_id["call_b"] != '{"b":1}'