Skip to content

πŸ™Œ OpenHands: AI-Driven Development

fix: resolve empty API keys to None and add Bedrock model support (#10573)

Joe Axea25826a5f996
diff --git a/openhands/cli/settings.py b/openhands/cli/settings.py
index 3a7a3015c8..567b1b00c5 100644
--- a/openhands/cli/settings.py
+++ b/openhands/cli/settings.py
@@ -479,11 +479,10 @@ async def modify_llm_settings_basic(
settings = Settings()
settings.llm_model = f'{provider}{organized_models[provider]["separator"]}{model}'
- settings.llm_api_key = SecretStr(api_key)
+ settings.llm_api_key = SecretStr(api_key) if api_key and api_key.strip() else None
settings.llm_base_url = None
settings.agent = OH_DEFAULT_AGENT
settings.enable_default_condenser = True
-
await settings_store.store(settings)
@@ -608,12 +607,11 @@ async def modify_llm_settings_advanced(
settings = Settings()
settings.llm_model = custom_model
- settings.llm_api_key = SecretStr(api_key)
+ settings.llm_api_key = SecretStr(api_key) if api_key and api_key.strip() else None
settings.llm_base_url = base_url
settings.agent = agent
settings.confirmation_mode = enable_confirmation_mode
settings.enable_default_condenser = enable_memory_condensation
-
await settings_store.store(settings)
@@ -685,5 +683,4 @@ async def modify_search_api_settings(
settings = Settings()
settings.search_api_key = SecretStr(search_api_key) if search_api_key else None
-
await settings_store.store(settings)
diff --git a/openhands/server/services/conversation_service.py b/openhands/server/services/conversation_service.py
index 2f6d35f82f..9adfe8849e 100644
--- a/openhands/server/services/conversation_service.py
+++ b/openhands/server/services/conversation_service.py
@@ -105,7 +105,10 @@ async def start_conversation(
session_init_args = {**settings.__dict__, **session_init_args}
# We could use litellm.check_valid_key for a more accurate check,
# but that would run a tiny inference.
- if (
+ model_name = settings.llm_model or ''
+ is_bedrock_model = model_name.startswith('bedrock/')
+
+ if not is_bedrock_model and (
not settings.llm_api_key
or settings.llm_api_key.get_secret_value().isspace()
):
@@ -113,6 +116,8 @@ async def start_conversation(
raise LLMAuthenticationError(
'Error authenticating with the LLM provider. Please check your API key'
)
+ elif is_bedrock_model:
+ logger.info(f'Bedrock model detected ({model_name}), API key not required')
else:
logger.warning('Settings not present, not starting conversation')
diff --git a/openhands/storage/data_models/settings.py b/openhands/storage/data_models/settings.py
index 295d3066a0..aaa50aa9d2 100644
--- a/openhands/storage/data_models/settings.py
+++ b/openhands/storage/data_models/settings.py
@@ -63,9 +63,14 @@ class Settings(BaseModel):
if api_key is None:
return None
+ # Get the secret value to check if it's empty
+ secret_value = api_key.get_secret_value()
+ if not secret_value or not secret_value.strip():
+ return None
+
context = info.context
if context and context.get('expose_secrets', False):
- return api_key.get_secret_value()
+ return secret_value
return pydantic_encoder(api_key)