From c86729ee4d848e64895b0950246d04cf92512435 Mon Sep 17 00:00:00 2001 From: Lanre Shittu <136805224+Shizoqua@users.noreply.github.com> Date: Sun, 8 Nov 2026 15:23:18 +0000 Subject: [PATCH 1/2] Python: Use the tool call index for streamed Azure AI Inference tool calls The Azure AI Inference connector set the index of each streamed tool call to the choice index, so every parallel tool call in a stream got index 0. Argument deltas after the first one carry only the index, so when the chunks were added together the arguments of the second call were appended to the first call, which then had invalid JSON arguments. Read the index the service sends for each tool call, and fall back to the position in the delta when it is missing. --- .../azure_ai_inference_chat_completion.py | 6 ++- ...test_azure_ai_inference_chat_completion.py | 50 ++++++++++++++++++- 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/python/semantic_kernel/connectors/ai/azure_ai_inference/services/azure_ai_inference_chat_completion.py b/python/semantic_kernel/connectors/ai/azure_ai_inference/services/azure_ai_inference_chat_completion.py index 5b0288a02752..a3c166751ee6 100644 --- a/python/semantic_kernel/connectors/ai/azure_ai_inference/services/azure_ai_inference_chat_completion.py +++ b/python/semantic_kernel/connectors/ai/azure_ai_inference/services/azure_ai_inference_chat_completion.py @@ -349,12 +349,14 @@ def _create_streaming_chat_message_content( ) ) if choice.delta.tool_calls: - for tool_call in choice.delta.tool_calls: + for idx, tool_call in enumerate(choice.delta.tool_calls): if isinstance(tool_call, StreamingChatResponseToolCallUpdate): + # The SDK model has no index field, but the service sends one for each tool call. + index = tool_call.get("index") items.append( FunctionCallContent( id=tool_call.id, - index=choice.index, + index=idx if index is None else index, name=tool_call.function.name, arguments=tool_call.function.arguments, ) diff --git a/python/tests/unit/connectors/ai/azure_ai_inference/services/test_azure_ai_inference_chat_completion.py b/python/tests/unit/connectors/ai/azure_ai_inference/services/test_azure_ai_inference_chat_completion.py index 4a17cc84f954..62d81ceca08e 100644 --- a/python/tests/unit/connectors/ai/azure_ai_inference/services/test_azure_ai_inference_chat_completion.py +++ b/python/tests/unit/connectors/ai/azure_ai_inference/services/test_azure_ai_inference_chat_completion.py @@ -1,10 +1,10 @@ # Copyright (c) Microsoft. All rights reserved. -from unittest.mock import AsyncMock, patch +from unittest.mock import AsyncMock, MagicMock, patch import pytest from azure.ai.inference.aio import ChatCompletionsClient -from azure.ai.inference.models import JsonSchemaFormat, UserMessage +from azure.ai.inference.models import JsonSchemaFormat, StreamingChatCompletionsUpdate, UserMessage from azure.core.credentials import AzureKeyCredential from pydantic import BaseModel, Field @@ -15,6 +15,7 @@ from semantic_kernel.connectors.ai.azure_ai_inference.azure_ai_inference_settings import AzureAIInferenceSettings from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior from semantic_kernel.contents.chat_history import ChatHistory +from semantic_kernel.contents.function_call_content import FunctionCallContent from semantic_kernel.contents.utils.finish_reason import FinishReason from semantic_kernel.exceptions.service_exceptions import ( ServiceInitializationError, @@ -707,4 +708,49 @@ async def test_azure_ai_inference_streaming_response_format_json_schema( assert kwargs["model"] == model_id +@pytest.mark.parametrize( + "azure_ai_inference_service", + [AzureAIInferenceChatCompletion.__name__], + indirect=True, +) +@patch.object(ChatCompletionsClient, "complete", new_callable=AsyncMock) +async def test_azure_ai_inference_streaming_parallel_tool_calls_keep_their_index( + mock_complete, + azure_ai_inference_service, + model_id, + chat_history: ChatHistory, +) -> None: + """Test that streamed parallel tool calls use the tool call index, not the choice index.""" + + def chunk(tool_call: dict) -> StreamingChatCompletionsUpdate: + return StreamingChatCompletionsUpdate({ + "id": "test_id", + "created": 0, + "model": model_id, + "choices": [{"index": 0, "delta": {"role": "assistant", "tool_calls": [tool_call]}}], + }) + + stream = MagicMock() + stream.__aiter__.return_value = [ + chunk({"index": 0, "id": "call_1", "function": {"name": "Weather-get_weather", "arguments": ""}}), + chunk({"index": 0, "function": {"arguments": '{"city": "Paris"}'}}), + chunk({"index": 1, "id": "call_2", "function": {"name": "Time-get_time", "arguments": ""}}), + chunk({"index": 1, "function": {"arguments": '{"zone": "Tokyo"}'}}), + ] + mock_complete.return_value = stream + chat_history.add_user_message("What is the weather in Paris and the time in Tokyo?") + + full_message = None + async for messages in azure_ai_inference_service.get_streaming_chat_message_contents( + chat_history, AzureAIInferenceChatPromptExecutionSettings() + ): + full_message = messages[0] if full_message is None else full_message + messages[0] + + function_calls = [item for item in full_message.items if isinstance(item, FunctionCallContent)] + assert [(fc.id, fc.index, fc.name, fc.arguments) for fc in function_calls] == [ + ("call_1", 0, "Weather-get_weather", '{"city": "Paris"}'), + ("call_2", 1, "Time-get_time", '{"zone": "Tokyo"}'), + ] + + # endregion streaming chat completion From cf9fdf470fccb1914e02dbe462eae853c36df514 Mon Sep 17 00:00:00 2001 From: Lanre Shittu <136805224+Shizoqua@users.noreply.github.com> Date: Sat, 14 Nov 2026 08:29:22 +0000 Subject: [PATCH 2/2] Python: Test streamed Azure AI Inference tool calls without an index Cover the fallback where the service sends several tool calls in one delta without an index. They should be indexed by their position. --- ...test_azure_ai_inference_chat_completion.py | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/python/tests/unit/connectors/ai/azure_ai_inference/services/test_azure_ai_inference_chat_completion.py b/python/tests/unit/connectors/ai/azure_ai_inference/services/test_azure_ai_inference_chat_completion.py index 62d81ceca08e..b5917699f2a8 100644 --- a/python/tests/unit/connectors/ai/azure_ai_inference/services/test_azure_ai_inference_chat_completion.py +++ b/python/tests/unit/connectors/ai/azure_ai_inference/services/test_azure_ai_inference_chat_completion.py @@ -753,4 +753,55 @@ def chunk(tool_call: dict) -> StreamingChatCompletionsUpdate: ] +@pytest.mark.parametrize( + "azure_ai_inference_service", + [AzureAIInferenceChatCompletion.__name__], + indirect=True, +) +@patch.object(ChatCompletionsClient, "complete", new_callable=AsyncMock) +async def test_azure_ai_inference_streaming_tool_calls_without_index_use_their_position( + mock_complete, + azure_ai_inference_service, + model_id, + chat_history: ChatHistory, +) -> None: + """Test that tool calls without an index in a single delta are indexed by their position.""" + stream = MagicMock() + stream.__aiter__.return_value = [ + StreamingChatCompletionsUpdate({ + "id": "test_id", + "created": 0, + "model": model_id, + "choices": [ + { + "index": 0, + "delta": { + "role": "assistant", + "tool_calls": [ + { + "id": "call_1", + "function": {"name": "Weather-get_weather", "arguments": '{"city": "Paris"}'}, + }, + {"id": "call_2", "function": {"name": "Time-get_time", "arguments": '{"zone": "Tokyo"}'}}, + ], + }, + } + ], + }) + ] + mock_complete.return_value = stream + chat_history.add_user_message("What is the weather in Paris and the time in Tokyo?") + + messages = [ + message + async for chunk in azure_ai_inference_service.get_streaming_chat_message_contents( + chat_history, AzureAIInferenceChatPromptExecutionSettings() + ) + for message in chunk + ] + + function_calls = [item for item in messages[0].items if isinstance(item, FunctionCallContent)] + assert [(fc.id, fc.index) for fc in function_calls] == [("call_1", 0), ("call_2", 1)] + + # endregion streaming chat completion