π OpenHands: AI-Driven Development
Fix CLI model selection to allow custom model names (#9205)
Graham Neubig8b90d610c619
diff --git a/openhands/cli/settings.py b/openhands/cli/settings.pyindex 7ce8f7de41..e23ba3be91 100644--- a/openhands/cli/settings.py+++ b/openhands/cli/settings.py@@ -264,23 +264,28 @@ async def modify_llm_settings_basic(if change_model:model_completer = FuzzyWordCompleter(provider_models)- # Define a validator function that prints an error message+ # Define a validator function that allows custom models but shows a warningdef model_validator(x):- is_valid = x in provider_models- if not is_valid:+ # Allow any non-empty model name+ if not x.strip():+ return False++ # Show a warning for models not in the predefined list, but still allow them+ if x not in provider_models:print_formatted_text(HTML(- f'<grey>Invalid model selected for provider {provider}: {x}</grey>'+ f'<yellow>Warning: {x} is not in the predefined list for provider {provider}. '+ f'Make sure this model name is correct.</yellow>'))- return is_valid+ return Truemodel = await get_validated_input(session,'(Step 2/3) Select LLM Model (TAB for options, CTRL-c to cancel): ',completer=model_completer,validator=model_validator,- error_message=f'Invalid model selected for provider {provider}',+ error_message='Model name cannot be empty',)else:# Use the default modeldiff --git a/tests/unit/test_cli_settings.py b/tests/unit/test_cli_settings.pyindex ea2c0ac70f..8de111fd89 100644--- a/tests/unit/test_cli_settings.py+++ b/tests/unit/test_cli_settings.py@@ -171,16 +171,17 @@ class TestModifyLLMSettingsBasic:session_instance = MagicMock()session_instance.prompt_async = AsyncMock(side_effect=[- 'openai', # Provider'gpt-4', # Model'new-api-key', # API Key])mock_session.return_value = session_instance- # Mock cli_confirm to select the second option (change provider/model) for the first two calls- # and then select the first option (save settings) for the last call- mock_confirm.side_effect = [1, 1, 0]+ # Mock cli_confirm to:+ # 1. Select the first provider (openai) from the list+ # 2. Select "Select another model" option+ # 3. Select "Yes, save" option+ mock_confirm.side_effect = [0, 1, 0]# Call the functionawait modify_llm_settings_basic(app_config, settings_store)@@ -189,8 +190,8 @@ class TestModifyLLMSettingsBasic:app_config.set_llm_config.assert_called_once()args, kwargs = app_config.set_llm_config.call_args# The model name might be different based on the default model in the list- # Just check that it starts with 'openai/'- assert args[0].model.startswith('openai/')+ # Just check that it contains 'gpt-4' instead of checking for prefix+ assert 'gpt-4' in args[0].modelassert args[0].api_key.get_secret_value() == 'new-api-key'assert args[0].base_url is None@@ -199,8 +200,8 @@ class TestModifyLLMSettingsBasic:args, kwargs = settings_store.store.call_argssettings = args[0]# The model name might be different based on the default model in the list- # Just check that it starts with openai/- assert settings.llm_model.startswith('openai/')+ # Just check that it contains 'gpt-4' instead of checking for prefix+ assert 'gpt-4' in settings.llm_modelassert settings.llm_api_key.get_secret_value() == 'new-api-key'assert settings.llm_base_url is None@@ -249,7 +250,7 @@ class TestModifyLLMSettingsBasic:'openhands.cli.settings.LLMSummarizingCondenserConfig',MockLLMSummarizingCondenserConfig,)- async def test_modify_llm_settings_basic_invalid_input(+ async def test_modify_llm_settings_basic_invalid_provider_input(self,mock_print,mock_confirm,@@ -270,8 +271,7 @@ class TestModifyLLMSettingsBasic:side_effect=['invalid-provider', # First invalid provider'openai', # Valid provider- 'invalid-model', # Invalid model- 'gpt-4', # Valid model+ 'custom-model', # Custom model (now allowed with warning)'new-api-key', # API key])@@ -284,34 +284,32 @@ class TestModifyLLMSettingsBasic:# Call the functionawait modify_llm_settings_basic(app_config, settings_store)- # Verify error messages were shown for invalid inputs- assert (- mock_print.call_count >= 2- ) # At least two error messages should be printed+ # Verify error message was shown for invalid provider and warning for custom model+ assert mock_print.call_count >= 2 # At least two messages should be printed- # Check for invalid provider error+ # Check for invalid provider error and custom model warningprovider_error_found = False- model_error_found = False+ model_warning_found = Falsefor call in mock_print.call_args_list:args, _ = callif args and isinstance(args[0], HTML):if 'Invalid provider selected' in args[0].value:provider_error_found = True- if 'Invalid model selected' in args[0].value:- model_error_found = True+ if 'Warning:' in args[0].value and 'custom-model' in args[0].value:+ model_warning_found = Trueassert provider_error_found, 'No error message for invalid provider'- assert model_error_found, 'No error message for invalid model'+ assert model_warning_found, 'No warning message for custom model'- # Verify LLM config was updated with correct values+ # Verify LLM config was updated with the custom modelapp_config.set_llm_config.assert_called_once()- # Verify settings were saved+ # Verify settings were saved with the custom modelsettings_store.store.assert_called_once()args, kwargs = settings_store.store.call_argssettings = args[0]- assert settings.llm_model == 'openai/gpt-4'+ assert 'custom-model' in settings.llm_modelassert settings.llm_api_key.get_secret_value() == 'new-api-key'assert settings.llm_base_url is None