diff --git a/python/packages/ollama/agent_framework_ollama/_chat_client.py b/python/packages/ollama/agent_framework_ollama/_chat_client.py index a5f06ba9c5b..02f039f636a 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,18 @@ 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: + 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 - 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 +683,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..3691457355f 100644 --- a/python/packages/ollama/tests/test_ollama_chat_client.py +++ b/python/packages/ollama/tests/test_ollama_chat_client.py @@ -621,6 +621,126 @@ 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_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,