Skip to content

The agent that grows with you

fix(model): coerce YAML integer provider names before picker/CRUD

Brooklyn Nicholsond83dcb4c3ad5
diff --git a/hermes_cli/config.py b/hermes_cli/config.py
index ef939e154bc..5475d4289f3 100644
--- a/hermes_cli/config.py
+++ b/hermes_cli/config.py
@@ -1389,6 +1389,54 @@ def _canonical_api_mode(api_mode: str) -> str:
return _API_MODE_ALIASES.get(cleaned.lower(), cleaned)
+def coerce_provider_id(value: Any) -> str:
+ """Provider identity fields are strings.
+
+ PyYAML loads unquoted scalars like ``provider: 2070`` / ``2070:`` as int,
+ and later ``.strip()`` / ``.lower()`` on that value 500s the Model tab.
+ """
+ if value is None:
+ return ""
+ return str(value).strip()
+
+
+def stringify_provider_map(providers: Any) -> dict:
+ """Copy a ``providers:`` mapping so keys are strings.
+
+ Desktop Custom Endpoints store the name as the dict key. An unquoted
+ YAML key ``2070:`` loads as int; picker code then calls ``ep_name.lower()``
+ and CRUD looks up ``"2070"`` and misses.
+ """
+ if not isinstance(providers, dict):
+ return {}
+ out: Dict[str, Any] = {}
+ for stored, value in providers.items():
+ key = coerce_provider_id(stored)
+ if key:
+ out[key] = value
+ return out
+
+
+def find_provider_entry(providers: Any, key: Any) -> Tuple[Any, Optional[Dict[str, Any]]]:
+ """Return ``(stored_key, entry)`` matching *key* by string identity.
+
+ Needed because PyYAML may have stored the key as ``2070`` (int) while
+ Desktop looks up ``"2070"``. Prefer an exact string hit, then scan.
+ """
+ if not isinstance(providers, dict):
+ return None, None
+ want = coerce_provider_id(key)
+ if not want:
+ return None, None
+ exact = providers.get(want)
+ if isinstance(exact, dict):
+ return want, exact
+ for stored, entry in providers.items():
+ if coerce_provider_id(stored) == want and isinstance(entry, dict):
+ return stored, entry
+ return None, None
+
+
def _normalize_custom_provider_entry(
entry: Any,
*,
@@ -1406,6 +1454,7 @@ def _normalize_custom_provider_entry(
# alias keys back into config.yaml through any later
# save_config(load_config()) round-trip.
entry = dict(entry)
+ provider_key = coerce_provider_id(provider_key)
# Accept camelCase aliases commonly used in hand-written configs.
_CAMEL_ALIASES: Dict[str, str] = {
@@ -1483,12 +1532,7 @@ def _normalize_custom_provider_entry(
if not base_url:
return None
- name = ""
- raw_name = entry.get("name")
- if isinstance(raw_name, str) and raw_name.strip():
- name = raw_name.strip()
- elif provider_key.strip():
- name = provider_key.strip()
+ name = coerce_provider_id(entry.get("name")) or provider_key
if not name:
return None
@@ -1650,7 +1694,9 @@ def providers_dict_to_custom_providers(providers_dict: Any) -> List[Dict[str, An
for key, entry in providers_dict.items():
if isinstance(entry, dict) and not is_provider_enabled(entry):
continue
- normalized = _normalize_custom_provider_entry(entry, provider_key=str(key))
+ normalized = _normalize_custom_provider_entry(
+ entry, provider_key=coerce_provider_id(key)
+ )
if normalized is not None:
custom_providers.append(normalized)
diff --git a/hermes_cli/inventory.py b/hermes_cli/inventory.py
index 2f426f67379..3f2de0820bf 100644
--- a/hermes_cli/inventory.py
+++ b/hermes_cli/inventory.py
@@ -83,26 +83,32 @@ def load_picker_context() -> ConfigContext:
Replaces the inline 17-LOC config-slice that ``web_server.py`` and
``tui_gateway/server.py`` (×2 sites) used to do.
"""
- from hermes_cli.config import get_compatible_custom_providers, load_config
+ from hermes_cli.config import (
+ coerce_provider_id,
+ get_compatible_custom_providers,
+ load_config,
+ stringify_provider_map,
+ )
cfg = load_config()
model_cfg = cfg.get("model", {})
if isinstance(model_cfg, dict):
- current_model = model_cfg.get("default", model_cfg.get("name", "")) or ""
- current_provider = model_cfg.get("provider", "") or ""
- current_base_url = model_cfg.get("base_url", "") or ""
+ # PyYAML parses unquoted scalars as int (`provider: 2070`). Keep these
+ # as strings so picker/options paths never call `.strip()` on an int.
+ current_model = str(model_cfg.get("default", model_cfg.get("name", "")) or "")
+ current_provider = coerce_provider_id(model_cfg.get("provider", ""))
+ current_base_url = str(model_cfg.get("base_url", "") or "")
else:
# config.model can be a bare string in older configs.
current_model = str(model_cfg) if model_cfg else ""
current_provider = ""
current_base_url = ""
- raw = cfg.get("providers")
excluded = cfg.get("model_catalog", {}).get("excluded_providers") or []
return ConfigContext(
current_provider=current_provider,
current_model=current_model,
current_base_url=current_base_url,
- user_providers=raw if isinstance(raw, dict) else {},
+ user_providers=stringify_provider_map(cfg.get("providers")),
custom_providers=get_compatible_custom_providers(cfg),
excluded_providers=excluded if isinstance(excluded, list) else [],
)
diff --git a/hermes_cli/model_switch.py b/hermes_cli/model_switch.py
index 959e0ce8cd7..d4689df8740 100644
--- a/hermes_cli/model_switch.py
+++ b/hermes_cli/model_switch.py
@@ -2643,10 +2643,18 @@ def list_authenticated_providers(
except Exception:
pass
+ from hermes_cli.config import coerce_provider_id, stringify_provider_map
+
results: List[dict] = []
seen_slugs: set = set() # lowercase-normalized to catch case variants (#9545)
- _current_provider_norm = str(current_provider or "").strip().lower()
- _current_base_url_norm = str(current_base_url or "").strip().rstrip("/").lower()
+ # PyYAML parses unquoted numeric names (`provider: 2070`) as int. Later
+ # `.strip()` / `.lower()` on that raw value 500s GET /api/model/options.
+ current_provider = coerce_provider_id(current_provider)
+ current_base_url = str(current_base_url or "").strip()
+ current_model = str(current_model or "").strip()
+ _current_provider_norm = current_provider.lower()
+ _current_base_url_norm = current_base_url.rstrip("/").lower()
+ user_providers = stringify_provider_map(user_providers)
def _can_probe_custom_provider(*, row_is_current: bool) -> bool:
return bool(probe_custom_providers or (probe_current_custom_provider and row_is_current))
@@ -3226,7 +3234,7 @@ def list_authenticated_providers(
continue
if ep_name.lower() in seen_slugs:
continue
- display_name = ep_cfg.get("name", "") or ep_name
+ display_name = coerce_provider_id(ep_cfg.get("name")) or ep_name
api_url = (
ep_cfg.get("base_url", "")
or ep_cfg.get("api", "")
@@ -3574,8 +3582,8 @@ def list_authenticated_providers(
if not isinstance(entry, dict):
continue
- raw_name = (entry.get("name") or "").strip()
- api_url = (
+ raw_name = coerce_provider_id(entry.get("name"))
+ api_url = str(
entry.get("base_url", "")
or entry.get("url", "")
or entry.get("api", "")
@@ -3583,8 +3591,8 @@ def list_authenticated_providers(
).strip().rstrip("/")
if not raw_name or not api_url:
continue
- inline_api_key = (entry.get("api_key") or "").strip()
- key_env = (entry.get("key_env") or "").strip()
+ inline_api_key = str(entry.get("api_key") or "").strip()
+ key_env = str(entry.get("key_env") or "").strip()
api_key = inline_api_key or _scoped_key_env(key_env)
api_mode = str(
entry.get("api_mode")
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py
index 2b5f0fd6b53..6fa676c85f6 100644
--- a/hermes_cli/web_server.py
+++ b/hermes_cli/web_server.py
@@ -76,6 +76,8 @@ from hermes_cli.config import (
save_env_value,
remove_env_value,
custom_endpoint_key_env,
+ coerce_provider_id,
+ find_provider_entry,
check_config_version,
detect_install_method,
format_docker_update_message,
@@ -8392,7 +8394,7 @@ def _parse_model_ids(resp: "Any") -> List[str]:
def _custom_endpoint_id(raw: str, fallback: str = "custom") -> str:
- slug = re.sub(r"[^A-Za-z0-9_-]+", "-", (raw or "").strip()).strip("-_").lower()
+ slug = re.sub(r"[^A-Za-z0-9_-]+", "-", coerce_provider_id(raw)).strip("-_").lower()
return slug or fallback
@@ -8438,8 +8440,7 @@ def _config_api_key_is_env_ref(endpoint_id: str) -> bool:
config.yaml, so migrating it would only copy that secret into a second
env var the user didn't ask for.
"""
- providers = read_raw_config().get("providers")
- entry = providers.get(endpoint_id) if isinstance(providers, dict) else None
+ _stored, entry = find_provider_entry(read_raw_config().get("providers"), endpoint_id)
raw_key = entry.get("api_key") if isinstance(entry, dict) else None
return bool(isinstance(raw_key, str) and re.search(r"\$\{[^}]+\}", raw_key))
@@ -8545,8 +8546,8 @@ def _write_custom_endpoint(cfg: Dict[str, Any], body: CustomEndpointUpdate) -> T
providers = cfg.get("providers")
if not isinstance(providers, dict):
providers = {}
- existing = providers.get(endpoint_id)
- if not isinstance(existing, dict):
+ stored_key, existing = find_provider_entry(providers, endpoint_id)
+ if existing is None:
existing = {}
# Merge onto the existing entry rather than replacing it. A providers.<name>
@@ -8605,6 +8606,8 @@ def _write_custom_endpoint(cfg: Dict[str, Any], body: CustomEndpointUpdate) -> T
entry["key_env"] = env_var
entry.pop("api_key", None)
+ if stored_key is not None and stored_key != endpoint_id:
+ providers.pop(stored_key, None)
providers[endpoint_id] = entry
cfg["providers"] = providers
@@ -8664,9 +8667,8 @@ def activate_custom_endpoint(endpoint_id: str, profile: Optional[str] = None):
with _config_profile_scope(profile):
cfg = load_config()
provider_key = _custom_endpoint_id(endpoint_id)
- providers = cfg.get("providers")
- entry = providers.get(provider_key) if isinstance(providers, dict) else None
- if not isinstance(entry, dict):
+ _stored, entry = find_provider_entry(cfg.get("providers"), provider_key)
+ if entry is None:
raise HTTPException(status_code=404, detail="custom endpoint not found")
models = _models_from_custom_endpoint_entry(entry)
@@ -8699,9 +8701,10 @@ def delete_custom_endpoint(endpoint_id: str, profile: Optional[str] = None):
cfg = load_config()
provider_key = _custom_endpoint_id(endpoint_id)
providers = cfg.get("providers")
- if not isinstance(providers, dict) or provider_key not in providers:
+ stored_key, entry = find_provider_entry(providers, provider_key)
+ if entry is None or not isinstance(providers, dict):
raise HTTPException(status_code=404, detail="custom endpoint not found")
- providers.pop(provider_key, None)
+ providers.pop(stored_key, None)
cfg["providers"] = providers
_detach_main_model_from_provider(cfg, provider_key)
remove_env_value(custom_endpoint_key_env(provider_key))