From 0a963fe72380cefda7902aca222d979a950fc148 Mon Sep 17 00:00:00 2001 From: Frank Song Date: Sun, 7 Jun 2026 23:48:53 +0800 Subject: [PATCH] fix(runtime_provider, model_switch): resolve custom provider key_env from .env file When a custom provider declares key_env in config.yaml (e.g. VOLCENGINE_CODING_PLAN_API_KEY), three code paths only read from os.environ instead of using get_env_value() which also checks ~/.hermes/.env. This causes named custom providers to silently fall back to 'no-key-required' when the key only exists in .env. Affected paths: - _get_named_custom_provider(): line 531, os.getenv() -> get_env_value() - _resolve_named_custom_runtime(): line 735, os.getenv() -> get_env_value() - switch_model() user-provider path: line 921, os.environ.get() -> get_env_value() Fix: All three now use get_env_value() from hermes_cli.config, which reads os.environ first then falls back to ~/.hermes/.env. This is a regression for any user who stores provider credentials in .env (the documented approach) rather than pre-loading them into the process environment. --- hermes_cli/model_switch.py | 3 ++- hermes_cli/runtime_provider.py | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/hermes_cli/model_switch.py b/hermes_cli/model_switch.py index 61a58d8754e7c..a5fc98d31f48a 100644 --- a/hermes_cli/model_switch.py +++ b/hermes_cli/model_switch.py @@ -35,6 +35,7 @@ from hermes_cli.model_normalize import ( normalize_model_for_provider, ) +from hermes_cli.config import get_env_value from agent.models_dev import ( ModelCapabilities, ModelInfo, @@ -917,7 +918,7 @@ def switch_model( if not _ukey: _kenv = str(_ucfg.get("key_env", "") or "").strip() if _kenv: - _ukey = os.environ.get(_kenv, "").strip() + _ukey = (get_env_value(_kenv) or "").strip() try: runtime = resolve_runtime_provider( requested=target_provider, diff --git a/hermes_cli/runtime_provider.py b/hermes_cli/runtime_provider.py index b81659785381a..d2c94cde4f7e1 100644 --- a/hermes_cli/runtime_provider.py +++ b/hermes_cli/runtime_provider.py @@ -29,7 +29,7 @@ resolve_external_process_provider_credentials, has_usable_secret, ) -from hermes_cli.config import get_compatible_custom_providers, load_config +from hermes_cli.config import get_compatible_custom_providers, get_env_value, load_config from hermes_constants import OPENROUTER_BASE_URL from utils import base_url_host_matches, base_url_hostname, env_int @@ -528,7 +528,7 @@ def _get_named_custom_provider(requested_provider: str) -> Optional[Dict[str, An name_norm = _normalize_custom_provider_name(ep_name) # Resolve the API key from the env var name stored in key_env key_env = str(entry.get("key_env", "") or "").strip() - resolved_api_key = os.getenv(key_env, "").strip() if key_env else "" + resolved_api_key = (get_env_value(key_env) or "").strip() if key_env else "" # Fall back to inline api_key when key_env is absent or unresolvable if not resolved_api_key: resolved_api_key = str(entry.get("api_key", "") or "").strip() @@ -732,7 +732,7 @@ def _resolve_named_custom_runtime( api_key_candidates = [ (explicit_api_key or "").strip(), str(custom_provider.get("api_key", "") or "").strip(), - os.getenv(str(custom_provider.get("key_env", "") or "").strip(), "").strip(), + (get_env_value(str(custom_provider.get("key_env", "") or "").strip()) or "").strip(), # Gate provider env keys on their authoritative hosts — sending # OPENAI_API_KEY to a local-llm endpoint leaks credentials (#28660). (os.getenv("OPENAI_API_KEY", "").strip() if _cp_is_openai_url else ""),