diff --git a/python/packages/ollama/agent_framework_ollama/_chat_client.py b/python/packages/ollama/agent_framework_ollama/_chat_client.py index 94e612f75db..9d447c6a98f 100644 --- a/python/packages/ollama/agent_framework_ollama/_chat_client.py +++ b/python/packages/ollama/agent_framework_ollama/_chat_client.py @@ -400,8 +400,16 @@ def _prepare_options(self, messages: Sequence[Message], options: Mapping[str, An messages = prepend_instructions_to_messages(list(messages), instructions, role="system") - # Keys to exclude from processing - exclude_keys = {"instructions", "tool_choice"} + # Keys to exclude from processing (unsupported in Ollama API) + exclude_keys = { + "instructions", + "tool_choice", + "allow_multiple_tool_calls", + "user", + "store", + "logit_bias", + "metadata", + } # Build run_options and model_options separately run_options: dict[str, Any] = {} diff --git a/python/packages/ollama/tests/test_ollama_chat_client.py b/python/packages/ollama/tests/test_ollama_chat_client.py index 70f578cc37a..0eeec19c9b0 100644 --- a/python/packages/ollama/tests/test_ollama_chat_client.py +++ b/python/packages/ollama/tests/test_ollama_chat_client.py @@ -810,3 +810,52 @@ def test_format_tool_message_strips_unique_suffix(self) -> None: assert formatted[0].tool_name == "search:advanced", ( f"Expected bare name 'search:advanced', got '{formatted[0].tool_name}'" ) + + +def test_prepare_options_excludes_unsupported_chat_options() -> None: + """Verify that unsupported ChatOptions fields are excluded from Ollama run_options even when non-None.""" + client = OllamaChatClient(host="http://localhost:12345", model="test-model") + messages = [Message(role="user", contents=["hello"])] + options = { + "allow_multiple_tool_calls": False, + "user": "test-user-id", + "store": True, + "logit_bias": {"101": 1.5}, + "metadata": {"session": "123"}, + "tool_choice": "auto", + "instructions": "Be helpful.", + } + + prepared = client._prepare_options(messages, options) + + for unsupported_key in [ + "allow_multiple_tool_calls", + "user", + "store", + "logit_bias", + "metadata", + "tool_choice", + "instructions", + ]: + assert unsupported_key not in prepared, f"Key {unsupported_key} should be excluded from run_options" + + +@pytest.mark.asyncio +async def test_get_response_with_unsupported_options(mock_chat_completion_response: OllamaChatResponse) -> None: + """Verify that get_response succeeds when options include unsupported fields.""" + mock_client = MagicMock(spec=AsyncClient) + mock_client._client = MagicMock() + mock_client._client.base_url = "http://localhost:12345" + mock_client.chat = AsyncMock(return_value=mock_chat_completion_response) + + client = OllamaChatClient(client=mock_client, model="test-model") + messages = [Message(role="user", contents=["hello"])] + options = {"allow_multiple_tool_calls": False, "user": "test-user"} + + res = await client.get_response(messages=messages, options=options) + assert res is not None + + mock_client.chat.assert_called_once() + _, kwargs = mock_client.chat.call_args + assert "allow_multiple_tool_calls" not in kwargs + assert "user" not in kwargs