Fix API version in the resolver (#7756)

Co-authored-by: openhands <openhands@all-hands.dev>
This commit is contained in:
Engel Nyst
2025-04-08 11:01:23 +02:00
committed by GitHub
parent 22cf5144cc
commit 9fa211bc27
4 changed files with 58 additions and 6 deletions

View File

@@ -164,8 +164,8 @@ class LLMConfig(BaseModel):
if self.openrouter_app_name:
os.environ['OR_APP_NAME'] = self.openrouter_app_name
# Assign an API version for Azure models
# While it doesn't seem required, the format supported by the API without version seems old and will likely break.
# Azure issue: https://github.com/All-Hands-AI/OpenHands/issues/6777
# Set an API version by default for Azure models
# Required for newer models.
# Azure issue: https://github.com/All-Hands-AI/OpenHands/issues/7755
if self.model.startswith('azure') and self.api_version is None:
self.api_version = '2024-08-01-preview'
self.api_version = '2024-12-01-preview'

View File

@@ -349,6 +349,7 @@ def main() -> None:
model=my_args.llm_model or os.environ['LLM_MODEL'],
api_key=SecretStr(api_key) if api_key else None,
base_url=my_args.llm_base_url or os.environ.get('LLM_BASE_URL', None),
api_version=os.environ.get('LLM_API_VERSION', None),
)
repo_instruction = None

View File

@@ -656,12 +656,21 @@ def main() -> None:
raise ValueError('Token is invalid.')
api_key = my_args.llm_api_key or os.environ['LLM_API_KEY']
model = my_args.llm_model or os.environ['LLM_MODEL']
base_url = my_args.llm_base_url or os.environ.get('LLM_BASE_URL', None)
api_version = os.environ.get('LLM_API_VERSION', None)
# Create LLMConfig instance
llm_config = LLMConfig(
model=my_args.llm_model or os.environ['LLM_MODEL'],
model=model,
api_key=SecretStr(api_key) if api_key else None,
base_url=my_args.llm_base_url or os.environ.get('LLM_BASE_URL', None),
base_url=base_url,
)
# Only set api_version if it was explicitly provided, otherwise let LLMConfig handle it
if api_version is not None:
llm_config.api_version = api_version
repo_instruction = None
if my_args.repo_instruction_file:
with open(my_args.repo_instruction_file, 'r') as f:

View File

@@ -212,3 +212,45 @@ unknown_attr = "should_not_exist"
assert custom_invalid.model == 'base-model'
assert custom_invalid.api_key.get_secret_value() == 'base-api-key'
assert custom_invalid.num_retries == 3 # default value
def test_azure_model_api_version(
default_config: AppConfig, tmp_path: pathlib.Path
) -> None:
"""Test that Azure models get the correct API version by default."""
toml_content = """
[core]
workspace_base = "./workspace"
[llm]
model = "azure/o3-mini"
api_key = "test-api-key"
"""
toml_file = tmp_path / 'azure_llm.toml'
toml_file.write_text(toml_content)
load_from_toml(default_config, str(toml_file))
# Verify Azure model gets default API version
azure_llm = default_config.get_llm_config('llm')
assert azure_llm.model == 'azure/o3-mini'
assert azure_llm.api_version == '2024-12-01-preview'
# Test that non-Azure models don't get default API version
toml_content = """
[core]
workspace_base = "./workspace"
[llm]
model = "anthropic/claude-3-sonnet"
api_key = "test-api-key"
"""
toml_file = tmp_path / 'non_azure_llm.toml'
toml_file.write_text(toml_content)
load_from_toml(default_config, str(toml_file))
# Verify non-Azure model doesn't get default API version
non_azure_llm = default_config.get_llm_config('llm')
assert non_azure_llm.model == 'anthropic/claude-3-sonnet'
assert non_azure_llm.api_version is None