Skip to content

Commit b11d98b

Browse files
authored
Python: fix(ollama): preserve streamed parallel tool calls by assigning call index (#14623)
### Summary Fixes #14614 When Ollama streams tool calls across response chunks, each chunk contains a tool call without an `id` or `index`. When the stream is reduced across chunks via `StreamingChatMessageContent.__add__`, `FunctionCallContent.__add__` collapsed all parallel tool calls with `index=None` into a single tool call (keeping the first function name and merging subsequent argument dictionaries into it). ### Changes 1. In `OllamaChatCompletion._inner_get_streaming_chat_message_contents`, maintain a running `tool_call_index` across streamed chunks and pass it to message parsers. 2. In `_create_streaming_chat_message_content_from_chat_response`, `_parse_tool_calls`, `_create_chat_message_content`, and `_create_streaming_chat_message_content`, assign the sequential `index` (falling back to `start_index + idx` if `index` is not present on the tool call). 3. Added unit tests for both mapping/dict chunks and `ChatResponse` chunks verifying that when parallel tool call chunks are combined via `reduce(lambda x, y: x + y, chunks)`, both function calls remain distinct with their respective names, arguments, and sequential indices. ### Verification - Ran `uv run --with "ollama" pytest tests/unit/connectors/ai/ollama/` (62 passed). - Ran `uv run ruff check semantic_kernel/connectors/ai/ollama/ tests/unit/connectors/ai/ollama/` (all checks passed).
1 parent cb59096 commit b11d98b

2 files changed

Lines changed: 163 additions & 8 deletions

File tree

‎python/semantic_kernel/connectors/ai/ollama/services/ollama_chat_completion.py‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,21 @@ async def _inner_get_streaming_chat_message_contents(
201201
f"Expected AsyncIterator but got {type(response_object)}."
202202
)
203203

204+
tool_call_index = 0
204205
async for part in response_object:
205206
if isinstance(part, ChatResponse):
206-
yield [self._create_streaming_chat_message_content_from_chat_response(part, function_invoke_attempt)]
207+
msg = self._create_streaming_chat_message_content_from_chat_response(
208+
part, function_invoke_attempt, tool_call_index=tool_call_index
209+
)
210+
tool_call_index += sum(1 for item in msg.items if isinstance(item, FunctionCallContent))
211+
yield [msg]
207212
continue
208213
if isinstance(part, Mapping):
209-
yield [self._create_streaming_chat_message_content(part, function_invoke_attempt)]
214+
msg = self._create_streaming_chat_message_content(
215+
part, function_invoke_attempt, tool_call_index=tool_call_index
216+
)
217+
tool_call_index += sum(1 for item in msg.items if isinstance(item, FunctionCallContent))
218+
yield [msg]
210219
continue
211220
raise ServiceInvalidResponseError(
212221
"Invalid response type from Ollama streaming chat completion. "
@@ -219,6 +228,7 @@ def _create_streaming_chat_message_content_from_chat_response(
219228
self,
220229
response: ChatResponse,
221230
function_invoke_attempt: int,
231+
tool_call_index: int = 0,
222232
) -> StreamingChatMessageContent:
223233
"""Create a chat message content from the response."""
224234
items: list[STREAMING_ITEM_TYPES] = []
@@ -230,7 +240,7 @@ def _create_streaming_chat_message_content_from_chat_response(
230240
inner_content=response.message,
231241
)
232242
)
233-
self._parse_tool_calls(response.message.tool_calls, items)
243+
self._parse_tool_calls(response.message.tool_calls, items, start_index=tool_call_index)
234244
return StreamingChatMessageContent(
235245
choice_index=0,
236246
role=AuthorRole.ASSISTANT,
@@ -241,13 +251,19 @@ def _create_streaming_chat_message_content_from_chat_response(
241251
function_invoke_attempt=function_invoke_attempt,
242252
)
243253

244-
def _parse_tool_calls(self, tool_calls: Sequence[Message.ToolCall] | None, items: list[Any]):
254+
def _parse_tool_calls(
255+
self, tool_calls: Sequence[Message.ToolCall] | None, items: list[Any], start_index: int = 0
256+
) -> None:
245257
if tool_calls:
246-
for tool_call in tool_calls:
258+
for idx, tool_call in enumerate(tool_calls, start=start_index):
259+
index = getattr(tool_call, "index", None)
260+
if index is None:
261+
index = idx
247262
items.append(
248263
FunctionCallContent(
249264
inner_content=tool_call,
250265
ai_model_id=self.ai_model_id,
266+
index=index,
251267
name=tool_call.function.name,
252268
arguments=tool_call.function.arguments,
253269
)
@@ -286,11 +302,15 @@ def _create_chat_message_content(self, response: Mapping[str, Any]) -> ChatMessa
286302
)
287303
)
288304
if tool_calls := message.get("tool_calls", None):
289-
for tool_call in tool_calls:
305+
for idx, tool_call in enumerate(tool_calls):
306+
index = tool_call.get("index")
307+
if index is None:
308+
index = idx
290309
items.append(
291310
FunctionCallContent(
292311
inner_content=tool_call,
293312
ai_model_id=self.ai_model_id,
313+
index=index,
294314
name=tool_call.get("function").get("name"),
295315
arguments=tool_call.get("function").get("arguments"),
296316
)
@@ -304,7 +324,10 @@ def _create_chat_message_content(self, response: Mapping[str, Any]) -> ChatMessa
304324
)
305325

306326
def _create_streaming_chat_message_content(
307-
self, part: Mapping[str, Any], function_invoke_attempt: int
327+
self,
328+
part: Mapping[str, Any],
329+
function_invoke_attempt: int,
330+
tool_call_index: int = 0,
308331
) -> StreamingChatMessageContent:
309332
"""Create a streaming chat message content from the response part."""
310333
items: list[STREAMING_ITEM_TYPES] = []
@@ -320,11 +343,15 @@ def _create_streaming_chat_message_content(
320343
)
321344
)
322345
if tool_calls := message.get("tool_calls", None):
323-
for tool_call in tool_calls:
346+
for idx, tool_call in enumerate(tool_calls, start=tool_call_index):
347+
index = tool_call.get("index")
348+
if index is None:
349+
index = idx
324350
items.append(
325351
FunctionCallContent(
326352
inner_content=tool_call,
327353
ai_model_id=self.ai_model_id,
354+
index=index,
328355
name=tool_call.get("function").get("name"),
329356
arguments=tool_call.get("function").get("arguments"),
330357
)

‎python/tests/unit/connectors/ai/ollama/services/test_ollama_chat_completion.py‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,3 +451,131 @@ async def fake_stream() -> AsyncGenerator[DummyChatResponse, None]:
451451

452452
# Only one batch should be collected
453453
assert len(collected) == 1
454+
455+
456+
@patch("ollama.AsyncClient.chat", new_callable=AsyncMock)
457+
async def test_streaming_parallel_tool_calls_mapping(
458+
mock_chat: AsyncMock,
459+
model_id: str,
460+
service_id: str,
461+
default_options: dict,
462+
chat_history,
463+
) -> None:
464+
"""Test that parallel tool calls across stream chunks keep distinct indices and do not merge."""
465+
from functools import reduce
466+
467+
async def fake_stream() -> AsyncGenerator[dict, None]:
468+
yield {
469+
"model": "m",
470+
"message": {
471+
"role": "assistant",
472+
"content": "",
473+
"tool_calls": [{"function": {"name": "Weather-get_weather", "arguments": {"city": "Paris"}}}],
474+
},
475+
}
476+
yield {
477+
"model": "m",
478+
"message": {
479+
"role": "assistant",
480+
"content": "",
481+
"tool_calls": [{"function": {"name": "Time-get_time", "arguments": {"zone": "Tokyo"}}}],
482+
},
483+
}
484+
yield {"model": "m", "message": {"role": "assistant", "content": ""}, "done": True}
485+
486+
mock_chat.return_value = fake_stream()
487+
488+
ollama = OllamaChatCompletion(ai_model_id=model_id)
489+
settings = OllamaChatPromptExecutionSettings(service_id=service_id, options=default_options)
490+
491+
chunks: list[StreamingChatMessageContent] = []
492+
async for batch in ollama.get_streaming_chat_message_contents(chat_history, settings):
493+
chunks.extend(batch)
494+
495+
full = reduce(lambda x, y: x + y, chunks)
496+
function_calls = [item for item in full.items if isinstance(item, FunctionCallContent)]
497+
498+
assert len(function_calls) == 2
499+
assert function_calls[0].name == "Weather-get_weather"
500+
assert function_calls[0].arguments == {"city": "Paris"}
501+
assert function_calls[0].index == 0
502+
503+
assert function_calls[1].name == "Time-get_time"
504+
assert function_calls[1].arguments == {"zone": "Tokyo"}
505+
assert function_calls[1].index == 1
506+
507+
508+
@patch("ollama.AsyncClient.chat", new_callable=AsyncMock)
509+
async def test_streaming_parallel_tool_calls_chat_response(
510+
mock_chat: AsyncMock,
511+
model_id: str,
512+
service_id: str,
513+
default_options: dict,
514+
chat_history,
515+
monkeypatch,
516+
) -> None:
517+
"""Test that parallel tool calls across ChatResponse stream chunks keep distinct indices."""
518+
from functools import reduce
519+
520+
class DummyFunction:
521+
def __init__(self, name, arguments):
522+
self.name = name
523+
self.arguments = arguments
524+
525+
class DummyToolCall:
526+
def __init__(self, function):
527+
self.function = function
528+
529+
class DummyMessage:
530+
def __init__(self, content: str, tool_calls=None) -> None:
531+
self.content = content
532+
self.tool_calls = tool_calls or []
533+
534+
class DummyChatResponse:
535+
def __init__(self, content: str, model: str, prompt_eval_count: int, eval_count: int, tool_calls=None) -> None:
536+
function_calls = [
537+
DummyToolCall(DummyFunction(tc["function"]["name"], tc["function"]["arguments"])) for tc in tool_calls
538+
]
539+
self.message = DummyMessage(content, function_calls)
540+
self.model = model
541+
self.prompt_eval_count = prompt_eval_count
542+
self.eval_count = eval_count
543+
544+
monkeypatch.setattr(occ_module, "ChatResponse", DummyChatResponse)
545+
546+
async def fake_stream() -> AsyncGenerator[DummyChatResponse, None]:
547+
yield DummyChatResponse(
548+
content="",
549+
model="m",
550+
prompt_eval_count=0,
551+
eval_count=0,
552+
tool_calls=[{"function": {"name": "Weather-get_weather", "arguments": {"city": "Paris"}}}],
553+
)
554+
yield DummyChatResponse(
555+
content="",
556+
model="m",
557+
prompt_eval_count=0,
558+
eval_count=0,
559+
tool_calls=[{"function": {"name": "Time-get_time", "arguments": {"zone": "Tokyo"}}}],
560+
)
561+
562+
mock_chat.return_value = fake_stream()
563+
564+
ollama = OllamaChatCompletion(ai_model_id=model_id)
565+
settings = OllamaChatPromptExecutionSettings(service_id=service_id, options=default_options)
566+
567+
chunks: list[StreamingChatMessageContent] = []
568+
async for batch in ollama.get_streaming_chat_message_contents(chat_history, settings):
569+
chunks.extend(batch)
570+
571+
full = reduce(lambda x, y: x + y, chunks)
572+
function_calls = [item for item in full.items if isinstance(item, FunctionCallContent)]
573+
574+
assert len(function_calls) == 2
575+
assert function_calls[0].name == "Weather-get_weather"
576+
assert function_calls[0].arguments == {"city": "Paris"}
577+
assert function_calls[0].index == 0
578+
579+
assert function_calls[1].name == "Time-get_time"
580+
assert function_calls[1].arguments == {"zone": "Tokyo"}
581+
assert function_calls[1].index == 1

0 commit comments

Comments
 (0)