π OpenHands: AI-Driven Development
Fix issue #7837: [Bug]: Unit tests for tool use support (#7838)
OpenHands78b8e58561c2
diff --git a/tests/unit/test_llm.py b/tests/unit/test_llm.pyindex f3fb5b71e0..d764cb84c8 100644--- a/tests/unit/test_llm.py+++ b/tests/unit/test_llm.py@@ -449,35 +449,84 @@ def test_completion_retry_with_llm_no_response_error_nonzero_temp(This test verifies that when LLMNoResponseError is raised with a non-zero temperature:1. The retry mechanism is triggered2. The temperature remains unchanged (not set to 0.2)- 3. After all retries are exhausted, the exception is propagated+ 3. After all retries are exhausted, the error is raised"""- # Define a side effect function that always raises LLMNoResponseErrormock_litellm_completion.side_effect = LLMNoResponseError('LLM did not return a response')- # Create LLM instance and make a completion call with non-zero temperaturellm = LLM(config=default_config)-- # We expect this to raise an exception after all retries are exhaustedwith pytest.raises(LLMNoResponseError):llm.completion(messages=[{'role': 'user', 'content': 'Hello!'}],stream=False,- temperature=0.7, # Initial temperature is non-zero+ temperature=0.7, # Non-zero temperature)- # Verify that litellm_completion was called multiple times (retries happened)- # The default_config has num_retries=2, so it should be called 2 times- assert mock_litellm_completion.call_count == 2+ # Verify that litellm_completion was called the expected number of times+ assert mock_litellm_completion.call_count == default_config.num_retries- # Check the temperature in the first call (should be 0.7)- first_call_kwargs = mock_litellm_completion.call_args_list[0][1]- assert first_call_kwargs.get('temperature') == 0.7+ # Check that all calls used the original temperature+ for call in mock_litellm_completion.call_args_list:+ assert call[1].get('temperature') == 0.7- # Check the temperature in the second call (should remain 0.7, not changed to 0.2)- second_call_kwargs = mock_litellm_completion.call_args_list[1][1]- assert second_call_kwargs.get('temperature') == 0.7++@patch('openhands.llm.llm.litellm.get_model_info')+@patch('openhands.llm.llm.httpx.get')+def test_gemini_25_pro_function_calling(mock_httpx_get, mock_get_model_info):+ """+ Test that Gemini 2.5 Pro models have function calling enabled by default.+ This includes testing various model name formats with different prefixes.+ """+ # Mock the model info response+ mock_get_model_info.return_value = {+ 'max_input_tokens': 8000,+ 'max_output_tokens': 2000,+ }++ # Mock the httpx response for litellm proxy+ mock_response = MagicMock()+ mock_response.json.return_value = {+ 'data': [+ {+ 'model_name': 'gemini-2.5-pro-preview-03-25',+ 'model_info': {+ 'max_input_tokens': 8000,+ 'max_output_tokens': 2000,+ },+ }+ ]+ }+ mock_httpx_get.return_value = mock_response++ # Test cases with model names and expected function calling support+ test_cases = [+ # Base model names+ ('gemini-2.5-pro-preview-03-25', True),+ ('gemini-2.5-pro-exp-03-25', True),+ # With gemini/ prefix+ ('gemini/gemini-2.5-pro-preview-03-25', True),+ ('gemini/gemini-2.5-pro-exp-03-25', True),+ # With litellm_proxy/ prefix+ ('litellm_proxy/gemini-2.5-pro-preview-03-25', True),+ ('litellm_proxy/gemini-2.5-pro-exp-03-25', True),+ # With openrouter/gemini/ prefix+ ('openrouter/gemini/gemini-2.5-pro-preview-03-25', True),+ ('openrouter/gemini/gemini-2.5-pro-exp-03-25', True),+ # With litellm_proxy/gemini/ prefix+ ('litellm_proxy/gemini/gemini-2.5-pro-preview-03-25', True),+ ('litellm_proxy/gemini/gemini-2.5-pro-exp-03-25', True),+ # Control case - a model that shouldn't have function calling+ ('gemini-1.0-pro', False),+ ]++ for model_name, expected_support in test_cases:+ config = LLMConfig(model=model_name, api_key='test_key')+ llm = LLM(config)++ assert (+ llm.is_function_calling_active() == expected_support+ ), f'Expected function calling support to be {expected_support} for model {model_name}'@patch('openhands.llm.llm.litellm_completion')