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 index field not populated in streaming mode with n>1 and tool calls (#15962)
Sameer Kankute396ab80f56a8
diff --git a/litellm/litellm_core_utils/streaming_handler.py b/litellm/litellm_core_utils/streaming_handler.pyindex 64223a9ba4e..a5c3862cf30 100644--- a/litellm/litellm_core_utils/streaming_handler.py+++ b/litellm/litellm_core_utils/streaming_handler.py@@ -734,6 +734,14 @@ class CustomStreamWrapper:"function_call" in completion_objand completion_obj["function_call"] is not None)+ or (+ "tool_calls" in model_response.choices[0].delta+ and model_response.choices[0].delta["tool_calls"] is not None+ )+ or (+ "function_call" in model_response.choices[0].delta+ and model_response.choices[0].delta["function_call"] is not None+ )or ("reasoning_content" in model_response.choices[0].deltaand model_response.choices[0].delta.reasoning_content is not Nonediff --git a/tests/llm_translation/test_openai.py b/tests/llm_translation/test_openai.pyindex 55d2d3953c6..d1fa9df916f 100644--- a/tests/llm_translation/test_openai.py+++ b/tests/llm_translation/test_openai.py@@ -1308,3 +1308,107 @@ def test_openai_gpt_5_codex_reasoning():print("response: ", response)for chunk in response:print("chunk: ", chunk)+++# Tests moved from test_streaming_n_with_tools.py+# Regression test for: https://github.com/BerriAI/litellm/issues/8977[email protected]("model", ["gpt-4o", "gpt-4-turbo"])+async def test_streaming_tool_calls_with_n_greater_than_1(model):+ """+ Test that the index field in a choice object is correctly populated+ when using streaming mode with n>1 and tool calls.++ Regression test for: https://github.com/BerriAI/litellm/issues/8977+ """+ tools = [+ {+ "type": "function",+ "function": {+ "strict": True,+ "name": "get_current_weather",+ "description": "Get the current weather in a given location",+ "parameters": {+ "type": "object",+ "properties": {+ "location": {+ "type": "string",+ "description": "The city and state, e.g. San Francisco, CA",+ },+ "unit": {+ "type": "string",+ "enum": ["celsius", "fahrenheit"],+ },+ },+ "required": ["location", "unit"],+ "additionalProperties": False,+ },+ },+ }+ ]++ response = litellm.completion(+ model=model,+ messages=[+ {+ "role": "user",+ "content": "What is the weather in San Francisco?",+ },+ ],+ tools=tools,+ stream=True,+ n=3,+ )++ # Collect all chunks and their indices+ indices_seen = []+ for chunk in response:+ assert len(chunk.choices) == 1, "Each streaming chunk should have exactly 1 choice"+ assert hasattr(chunk.choices[0], "index"), "Choice should have an index attribute"+ index = chunk.choices[0].index+ indices_seen.append(index)++ # Verify that we got chunks with different indices (0, 1, 2 for n=3)+ unique_indices = set(indices_seen)+ assert unique_indices == {0, 1, 2}, f"Should have indices 0, 1, 2 for n=3, got {unique_indices}"++ print(f"✓ Test passed: streaming with n=3 and tool calls correctly populates index field")+ print(f" Indices seen: {indices_seen}")+ print(f" Unique indices: {unique_indices}")++[email protected]("model", ["gpt-4o"])+async def test_streaming_content_with_n_greater_than_1(model):+ """+ Test that the index field is correctly populated for regular content streaming+ (not tool calls) with n>1.+ """+ response = litellm.completion(+ model=model,+ messages=[+ {+ "role": "user",+ "content": "Say hello in one word",+ },+ ],+ stream=True,+ n=2,+ max_tokens=10,+ )++ # Collect all chunks and their indices+ indices_seen = []+ for chunk in response:+ assert len(chunk.choices) == 1, "Each streaming chunk should have exactly 1 choice"+ assert hasattr(chunk.choices[0], "index"), "Choice should have an index attribute"+ index = chunk.choices[0].index+ indices_seen.append(index)++ # Verify that we got chunks with different indices (0, 1 for n=2)+ unique_indices = set(indices_seen)+ assert unique_indices == {0, 1}, f"Should have indices 0, 1 for n=2, got {unique_indices}"++ print(f"✓ Test passed: streaming with n=2 and regular content correctly populates index field")+ print(f" Indices seen: {indices_seen}")+ print(f" Unique indices: {unique_indices}")