From 4cbf4b046a36c5e4dd67baa3396658ae9a1c3ebd Mon Sep 17 00:00:00 2001 From: Dhruv-Sh-git Date: Tue, 27 Oct 2026 18:48:43 +0530 Subject: [PATCH 1/2] Python: Report finish_reason tool_calls on streaming turns in Ollama --- .../agent_framework_ollama/_chat_client.py | 17 +++++--- .../ollama/tests/test_ollama_chat_client.py | 40 +++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/python/packages/ollama/agent_framework_ollama/_chat_client.py b/python/packages/ollama/agent_framework_ollama/_chat_client.py index a5f06ba9c5b..039d4b25331 100644 --- a/python/packages/ollama/agent_framework_ollama/_chat_client.py +++ b/python/packages/ollama/agent_framework_ollama/_chat_client.py @@ -394,9 +394,12 @@ async def _stream() -> AsyncIterable[ChatResponseUpdate]: # The SDK stream owns the HTTP response; close it as soon as the consumer stops early # instead of leaving the response open until the generator is garbage collected. + has_tool_calls = False try: async for part in response_object: - yield self._parse_streaming_response_from_ollama(part) + if getattr(part, "message", None) and getattr(part.message, "tool_calls", None): + has_tool_calls = True + yield self._parse_streaming_response_from_ollama(part, has_tool_calls=has_tool_calls) finally: aclose = getattr(response_object, "aclose", None) if aclose is not None: @@ -650,12 +653,16 @@ def _parse_contents_from_ollama( contents.extend(tool_calls) return contents - def _get_finish_reason_from_ollama(self, response: OllamaChatResponse) -> FinishReason | None: - if response.message.tool_calls: + def _get_finish_reason_from_ollama( + self, response: OllamaChatResponse, *, has_tool_calls: bool = False + ) -> FinishReason | None: + if (response.message and response.message.tool_calls) or has_tool_calls: return FinishReason("tool_calls") return FinishReason(response.done_reason) if response.done_reason else None - def _parse_streaming_response_from_ollama(self, response: OllamaChatResponse) -> ChatResponseUpdate: + def _parse_streaming_response_from_ollama( + self, response: OllamaChatResponse, *, has_tool_calls: bool = False + ) -> ChatResponseUpdate: contents = self._parse_contents_from_ollama(response, streaming=True) finish_reason = None if response.done: @@ -674,7 +681,7 @@ def _parse_streaming_response_from_ollama(self, response: OllamaChatResponse) -> ) if usage_details: contents.append(Content.from_usage(usage_details, merge_mode="replace", raw_representation=response)) - finish_reason = self._get_finish_reason_from_ollama(response) + finish_reason = self._get_finish_reason_from_ollama(response, has_tool_calls=has_tool_calls) return ChatResponseUpdate( contents=contents, role="assistant", diff --git a/python/packages/ollama/tests/test_ollama_chat_client.py b/python/packages/ollama/tests/test_ollama_chat_client.py index 706497f0ad9..84fb3eb9c17 100644 --- a/python/packages/ollama/tests/test_ollama_chat_client.py +++ b/python/packages/ollama/tests/test_ollama_chat_client.py @@ -621,6 +621,46 @@ async def test_cmc_streaming_with_tool_call( assert text_result.text == "test" +@patch.object(AsyncClient, "chat", new_callable=AsyncMock) +async def test_cmc_streaming_tool_call_finish_reason( + mock_chat: AsyncMock, + ollama_unit_test_env: dict[str, str], + chat_history: list[Message], +) -> None: + chunk1 = OllamaChatResponse( + message=OllamaMessage( + content="", + role="assistant", + tool_calls=cast(Any, [{"function": {"name": "hello_world", "arguments": {"arg1": "value1"}}}]), + ), + model="test", + done=False, + ) + chunk2 = OllamaChatResponse( + message=OllamaMessage( + content="", + role="assistant", + ), + model="test", + done=True, + done_reason="stop", + prompt_eval_count=6, + eval_count=4, + ) + stream = MagicMock(spec=AsyncStream) + stream.__aiter__.return_value = [chunk1, chunk2] + mock_chat.return_value = stream + chat_history.append(Message(contents=["hello world"], role="user")) + + ollama_client = OllamaChatClient(function_invocation_configuration={"enabled": False}) + result = ollama_client.get_response(messages=chat_history, stream=True) + async for _ in result: + pass + final_response = await result.get_final_response() + + assert final_response.finish_reason == "tool_calls" + + @patch.object(AsyncClient, "chat", new_callable=AsyncMock) async def test_cmc_with_dict_tool_passthrough( mock_chat: AsyncMock, From 7dcc5fd99a9022ab5ade05ce9d4a0b0b17d00667 Mon Sep 17 00:00:00 2001 From: Dhruv-Sh-git Date: Sat, 31 Oct 2026 02:07:32 +0530 Subject: [PATCH 2/2] Python: Preserve non-stop terminal finish reasons when tool calls occurred in stream --- .../agent_framework_ollama/_chat_client.py | 4 +- .../ollama/tests/test_ollama_chat_client.py | 80 +++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/python/packages/ollama/agent_framework_ollama/_chat_client.py b/python/packages/ollama/agent_framework_ollama/_chat_client.py index 039d4b25331..02f039f636a 100644 --- a/python/packages/ollama/agent_framework_ollama/_chat_client.py +++ b/python/packages/ollama/agent_framework_ollama/_chat_client.py @@ -656,7 +656,9 @@ def _parse_contents_from_ollama( def _get_finish_reason_from_ollama( self, response: OllamaChatResponse, *, has_tool_calls: bool = False ) -> FinishReason | None: - if (response.message and response.message.tool_calls) or has_tool_calls: + if response.message and response.message.tool_calls: + return FinishReason("tool_calls") + if has_tool_calls and (not response.done_reason or response.done_reason == "stop"): return FinishReason("tool_calls") return FinishReason(response.done_reason) if response.done_reason else None diff --git a/python/packages/ollama/tests/test_ollama_chat_client.py b/python/packages/ollama/tests/test_ollama_chat_client.py index 84fb3eb9c17..3691457355f 100644 --- a/python/packages/ollama/tests/test_ollama_chat_client.py +++ b/python/packages/ollama/tests/test_ollama_chat_client.py @@ -661,6 +661,86 @@ async def test_cmc_streaming_tool_call_finish_reason( assert final_response.finish_reason == "tool_calls" +@patch.object(AsyncClient, "chat", new_callable=AsyncMock) +async def test_cmc_streaming_tool_call_preserves_terminal_length( + mock_chat: AsyncMock, + ollama_unit_test_env: dict[str, str], + chat_history: list[Message], +) -> None: + chunk1 = OllamaChatResponse( + message=OllamaMessage( + content="", + role="assistant", + tool_calls=cast(Any, [{"function": {"name": "hello_world", "arguments": {"arg1": "value1"}}}]), + ), + model="test", + done=False, + ) + chunk2 = OllamaChatResponse( + message=OllamaMessage( + content="", + role="assistant", + ), + model="test", + done=True, + done_reason="length", + prompt_eval_count=6, + eval_count=4, + ) + stream = MagicMock(spec=AsyncStream) + stream.__aiter__.return_value = [chunk1, chunk2] + mock_chat.return_value = stream + chat_history.append(Message(contents=["hello world"], role="user")) + + ollama_client = OllamaChatClient(function_invocation_configuration={"enabled": False}) + result = ollama_client.get_response(messages=chat_history, stream=True) + async for _ in result: + pass + final_response = await result.get_final_response() + + assert final_response.finish_reason == "length" + + +@patch.object(AsyncClient, "chat", new_callable=AsyncMock) +async def test_cmc_streaming_tool_call_with_missing_terminal_finish_reason( + mock_chat: AsyncMock, + ollama_unit_test_env: dict[str, str], + chat_history: list[Message], +) -> None: + chunk1 = OllamaChatResponse( + message=OllamaMessage( + content="", + role="assistant", + tool_calls=cast(Any, [{"function": {"name": "hello_world", "arguments": {"arg1": "value1"}}}]), + ), + model="test", + done=False, + ) + chunk2 = OllamaChatResponse( + message=OllamaMessage( + content="", + role="assistant", + ), + model="test", + done=True, + done_reason=None, + prompt_eval_count=6, + eval_count=4, + ) + stream = MagicMock(spec=AsyncStream) + stream.__aiter__.return_value = [chunk1, chunk2] + mock_chat.return_value = stream + chat_history.append(Message(contents=["hello world"], role="user")) + + ollama_client = OllamaChatClient(function_invocation_configuration={"enabled": False}) + result = ollama_client.get_response(messages=chat_history, stream=True) + async for _ in result: + pass + final_response = await result.get_final_response() + + assert final_response.finish_reason == "tool_calls" + + @patch.object(AsyncClient, "chat", new_callable=AsyncMock) async def test_cmc_with_dict_tool_passthrough( mock_chat: AsyncMock,