Skip to content

Commit 4711e74

Browse files
neubigopenhands-agentxingyaoww
authored
Fix default provider in CLI to be 'anthropic' instead of 'openai' (OpenHands#9004)
Co-authored-by: openhands <[email protected]> Co-authored-by: Xingyao Wang <[email protected]>
1 parent c87f1cc commit 4711e74

File tree

2 files changed

+113
-6
lines changed

2 files changed

+113
-6
lines changed

openhands/cli/settings.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ async def modify_llm_settings_basic(
158158
provider_completer = FuzzyWordCompleter(provider_list)
159159
session = PromptSession(key_bindings=kb_cancel())
160160

161-
# Set default provider - use the first available provider from the list
162-
provider = provider_list[0] if provider_list else 'openai'
161+
# Set default provider - prefer 'anthropic' if available, otherwise use the first provider
162+
provider = 'anthropic' if 'anthropic' in provider_list else provider_list[0]
163163
model = None
164164
api_key = None
165165

@@ -196,9 +196,11 @@ def provider_validator(x):
196196

197197
# Make sure the provider exists in organized_models
198198
if provider not in organized_models:
199-
# If the provider doesn't exist, use the first available provider
199+
# If the provider doesn't exist, prefer 'anthropic' if available, otherwise use the first provider
200200
provider = (
201-
next(iter(organized_models.keys())) if organized_models else 'openai'
201+
'anthropic'
202+
if 'anthropic' in organized_models
203+
else next(iter(organized_models.keys()))
202204
)
203205

204206
provider_models = organized_models[provider]['models']
@@ -213,8 +215,10 @@ def provider_validator(x):
213215
]
214216
provider_models = VERIFIED_ANTHROPIC_MODELS + provider_models
215217

216-
# Set default model to the first model in the list
217-
default_model = provider_models[0] if provider_models else 'gpt-4'
218+
# Set default model to the first model in the list (which will be a verified model if available)
219+
default_model = (
220+
provider_models[0] if provider_models else 'claude-sonnet-4-20250514'
221+
)
218222

219223
# Show the default model but allow changing it
220224
print_formatted_text(

tests/unit/test_cli_settings.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,109 @@ async def test_modify_llm_settings_basic_invalid_input(
315315
assert settings.llm_api_key.get_secret_value() == 'new-api-key'
316316
assert settings.llm_base_url is None
317317

318+
def test_default_provider_preference(self):
319+
"""Test that the default provider prefers 'anthropic' if available."""
320+
# This is a simple test to verify that the default provider prefers 'anthropic'
321+
# We're directly checking the code in settings.py where the default provider is set
322+
323+
# Import the settings module to check the default provider
324+
# Find the line where the default provider is set
325+
import inspect
326+
327+
import openhands.cli.settings as settings_module
328+
329+
source_lines = inspect.getsource(
330+
settings_module.modify_llm_settings_basic
331+
).splitlines()
332+
333+
# Look for the line that sets the default provider
334+
default_provider_found = False
335+
for i, line in enumerate(source_lines):
336+
if "# Set default provider - prefer 'anthropic' if available" in line:
337+
default_provider_found = True
338+
break
339+
340+
# Assert that the default provider comment exists
341+
assert default_provider_found, 'Could not find the default provider comment'
342+
343+
# Now look for the actual implementation
344+
provider_impl_found = False
345+
for i, line in enumerate(source_lines):
346+
if "'anthropic'" in line and "if 'anthropic' in provider_list" in line:
347+
provider_impl_found = True
348+
break
349+
350+
assert provider_impl_found, (
351+
"Could not find the implementation that prefers 'anthropic'"
352+
)
353+
354+
# Also check the fallback provider when provider not in organized_models
355+
fallback_comment_found = False
356+
for i, line in enumerate(source_lines):
357+
if (
358+
"# If the provider doesn't exist, prefer 'anthropic' if available"
359+
in line
360+
):
361+
fallback_comment_found = True
362+
break
363+
364+
assert fallback_comment_found, 'Could not find the fallback provider comment'
365+
366+
# Now look for the actual implementation
367+
fallback_impl_found = False
368+
for i, line in enumerate(source_lines):
369+
if "'anthropic'" in line and "if 'anthropic' in organized_models" in line:
370+
fallback_impl_found = True
371+
break
372+
373+
assert fallback_impl_found, (
374+
"Could not find the fallback implementation that prefers 'anthropic'"
375+
)
376+
377+
def test_default_model_selection(self):
378+
"""Test that the default model selection uses the first model in the list."""
379+
# This is a simple test to verify that the default model selection uses the first model in the list
380+
# We're directly checking the code in settings.py where the default model is set
381+
382+
import inspect
383+
384+
import openhands.cli.settings as settings_module
385+
386+
source_lines = inspect.getsource(
387+
settings_module.modify_llm_settings_basic
388+
).splitlines()
389+
390+
# Look for the block that sets the default model
391+
default_model_block = []
392+
in_default_model_block = False
393+
for line in source_lines:
394+
if '# Set default model to the first model in the list' in line:
395+
in_default_model_block = True
396+
default_model_block.append(line)
397+
elif in_default_model_block:
398+
default_model_block.append(line)
399+
if '# Show the default model' in line:
400+
break
401+
402+
# Assert that we found the default model selection logic
403+
assert default_model_block, (
404+
'Could not find the block that sets the default model'
405+
)
406+
407+
# Print the actual lines for debugging
408+
print('Default model block found:')
409+
for line in default_model_block:
410+
print(f' {line.strip()}')
411+
412+
# Check that the logic uses the first model in the list
413+
first_model_check = any(
414+
'provider_models[0]' in line for line in default_model_block
415+
)
416+
417+
assert first_model_check, (
418+
'Default model selection should use the first model in the list'
419+
)
420+
318421

319422
class TestModifyLLMSettingsAdvanced:
320423
@pytest.fixture

0 commit comments

Comments
 (0)