Skip to content

The agent that grows with you

fix: flatten dict-valued model.default at config load boundary

mariobgsp998329a621e9
diff --git a/hermes_cli/config.py b/hermes_cli/config.py
index 1851d1a3fca..7a6c67e0d7e 100644
--- a/hermes_cli/config.py
+++ b/hermes_cli/config.py
@@ -2882,6 +2882,16 @@ def _normalize_root_model_keys(config: Dict[str, Any]) -> Dict[str, Any]:
# alias, or a model dict whose id lives under a non-canonical key.
model_in = config.get("model")
model_has_alias = isinstance(model_in, dict) and model_in.get("api_base")
+ # A dict-valued ``default``/``model`` (``{provider: ..., model: ...}``)
+ # must be flattened into ``default`` (string) + ``provider`` here at the
+ # single load/save chokepoint, so every reader (doctor, status, fallback
+ # picker, prompt-size, context-switch guard, …) sees plain strings instead
+ # of a nested dict that crashes ``.strip()``/``.lower()`` or routes the
+ # model through the wrong provider.
+ _has_nested_default = isinstance(model_in, dict) and (
+ isinstance(model_in.get("default"), dict)
+ or isinstance(model_in.get("model"), dict)
+ )
# A model dict needs canonicalization if its id lives under a non-canonical
# key (``model``/``name``) — either because ``default`` is empty (we must
# promote the alias) or because ``default`` is set but a stale alias still
@@ -2892,7 +2902,7 @@ def _normalize_root_model_keys(config: Dict[str, Any]) -> Dict[str, Any]:
has_root = any(
config.get(k) for k in ("provider", "base_url", "context_length", "api_base")
)
- if not has_root and not model_has_alias and not model_needs_canon:
+ if not has_root and not model_has_alias and not model_needs_canon and not _has_nested_default:
return config
config = dict(config)
@@ -2903,6 +2913,24 @@ def _normalize_root_model_keys(config: Dict[str, Any]) -> Dict[str, Any]:
model = dict(model)
config["model"] = model
+ # Flatten a dict-valued ``model.default``/``model.model``:
+ # ``{provider: <p>, model: <m>}`` -> ``default: "<m>"`` and, when no
+ # explicit ``model.provider`` is set, ``provider: "<p>"``. The nested
+ # provider must win over the merged default ``"auto"`` (which runtime
+ # resolution treats as authoritative and would otherwise route the model
+ # through the wrong active provider), but never over an explicitly
+ # configured outer provider.
+ for _key in ("default", "model"):
+ _val = model.get(_key)
+ if isinstance(_val, dict):
+ _nested_model = _val.get("model") or _val.get("default")
+ _nested_provider = str(_val.get("provider") or "").strip()
+ model[_key] = str(_nested_model or "").strip()
+ if _nested_provider:
+ _outer_provider = str(model.get("provider") or "").strip()
+ if not _outer_provider or _outer_provider == "auto":
+ model["provider"] = _nested_provider
+
for key in ("provider", "base_url", "context_length"):
root_val = config.get(key)
if root_val and not model.get(key):
diff --git a/tests/cli/test_cli_init.py b/tests/cli/test_cli_init.py
index 6ec0415812b..4de51de7204 100644
--- a/tests/cli/test_cli_init.py
+++ b/tests/cli/test_cli_init.py
@@ -590,5 +590,73 @@ class TestRootLevelProviderOverride:
assert "model" not in result["model"] and "name" not in result["model"]
+ # --- dict-valued model.default flattening (PR #83902 follow-up) --------
+ # ``model.default: {provider: ..., model: ...}`` must flatten into a string
+ # ``model.default`` plus ``model.provider`` at the load chokepoint so every
+ # reader (doctor, status, fallback picker, prompt-size, context-switch
+ # guard) sees plain strings instead of a nested dict that crashes
+ # ``.strip()``/``.lower()`` or routes the model through the wrong provider.
+
+ def test_nested_dict_default_flattens_model_and_provider(self):
+ """dict model.default -> string default + provider, no outer provider set."""
+ from hermes_cli.config import _normalize_root_model_keys
+
+ result = _normalize_root_model_keys({
+ "model": {
+ "default": {"provider": "nous", "model": "nested-default-model"},
+ },
+ })
+ assert result["model"]["default"] == "nested-default-model"
+ assert result["model"]["provider"] == "nous"
+
+ def test_nested_dict_default_provider_wins_over_auto(self):
+ """Nested provider replaces the merged default "auto"."""
+ from hermes_cli.config import _normalize_root_model_keys
+
+ result = _normalize_root_model_keys({
+ "model": {
+ "default": {"provider": "nous", "model": "nested-default-model"},
+ "provider": "auto",
+ },
+ })
+ assert result["model"]["default"] == "nested-default-model"
+ assert result["model"]["provider"] == "nous"
+
+ def test_nested_dict_default_never_overrides_explicit_provider(self):
+ """An explicitly configured model.provider beats the nested provider."""
+ from hermes_cli.config import _normalize_root_model_keys
+
+ result = _normalize_root_model_keys({
+ "model": {
+ "default": {"provider": "nous", "model": "nested-default-model"},
+ "provider": "anthropic",
+ },
+ })
+ assert result["model"]["default"] == "nested-default-model"
+ assert result["model"]["provider"] == "anthropic"
+
+ def test_nested_dict_model_alias_flattens_to_default(self):
+ """dict model.model alias also flattens (default > model > name)."""
+ from hermes_cli.config import _normalize_root_model_keys
+
+ result = _normalize_root_model_keys({
+ "model": {
+ "model": {"provider": "openai", "model": "nested-alias-model"},
+ },
+ })
+ assert result["model"]["default"] == "nested-alias-model"
+ assert result["model"]["provider"] == "openai"
+ assert "model" not in result["model"]
+
+ def test_flat_string_default_untouched(self):
+ """Plain string defaults keep existing behavior exactly."""
+ from hermes_cli.config import _normalize_root_model_keys
+
+ result = _normalize_root_model_keys({
+ "model": {"default": "flat-default-model", "provider": "auto"},
+ })
+ assert result["model"]["default"] == "flat-default-model"
+ assert result["model"]["provider"] == "auto"
+