Skip to content

The agent that grows with you

fix(model_switch): switching to a bare custom endpoint from another provider resolves the configured one

kshitijk4poordaf882e3c283
diff --git a/hermes_cli/model_switch.py b/hermes_cli/model_switch.py
index 1df8d9c65c1..5642a0b0f4c 100644
--- a/hermes_cli/model_switch.py
+++ b/hermes_cli/model_switch.py
@@ -7,6 +7,7 @@ exclusively; colons are reserved for OpenRouter variant suffixes (``:free``, ``:
from __future__ import annotations
import logging
+from contextlib import suppress
import os
import re
from dataclasses import dataclass, field
@@ -1378,7 +1379,17 @@ def _creds_for_switched_provider(st: _Switch) -> Optional[ModelSwitchResult]:
except Exception:
st.api_key, st.base_url, st.api_mode = ukey, user_pdef.base_url, ""
elif st.target_provider == "custom" and st.current_base_url:
+ # A bare-custom session switching models stays on its endpoint (#45597). Arriving from
+ # ANOTHER provider (the per-turn config sync adopting ``provider: custom``), the configured
+ # custom endpoint wins: keeping the session's URL pairs the new model with the previous
+ # provider's host and key (#73680). No configured endpoint ⇒ keep the current one (an alias
+ # may still supply its own below).
st.api_key, st.base_url = st.current_api_key, st.current_base_url
+ if st.current_provider != "custom":
+ with suppress(Exception):
+ st.resolve_runtime(requested="custom")
+ if not st.base_url:
+ st.api_key, st.base_url = st.current_api_key, st.current_base_url
st.api_mode = determine_api_mode(st.target_provider, st.base_url)
else:
# A URL-bearing LOCAL direct alias (ollama, vllm — labels that resolve to `custom`)
diff --git a/tests/hermes_cli/test_model_switch_custom_providers.py b/tests/hermes_cli/test_model_switch_custom_providers.py
index d9b04edd9f4..a1fe627a15d 100644
--- a/tests/hermes_cli/test_model_switch_custom_providers.py
+++ b/tests/hermes_cli/test_model_switch_custom_providers.py
@@ -501,6 +501,36 @@ def test_switch_model_accepts_explicit_bare_custom_current_endpoint(monkeypatch)
assert result.api_key == "sk-test"
+def test_switch_to_bare_custom_from_another_provider_resolves_the_configured_endpoint(monkeypatch, tmp_path):
+ """#73680: the per-turn config sync adopting ``provider: custom`` from an OpenRouter session
+ must land on the configured custom endpoint, not pair the new model with OpenRouter's URL
+ and key."""
+ home = tmp_path / "hermes-home"
+ home.mkdir()
+ (home / "config.yaml").write_text(
+ "model:\n default: qwen3:8b\n provider: custom\n base_url: http://127.0.0.1:11434/v1\n"
+ )
+ monkeypatch.setenv("HERMES_HOME", str(home))
+ monkeypatch.setattr("hermes_cli.models_validate.validate_requested_model", lambda *a, **k: _MOCK_VALIDATION)
+ monkeypatch.setattr("hermes_cli.model_switch.get_model_info", lambda *a, **k: None)
+ monkeypatch.setattr("hermes_cli.model_switch.get_model_capabilities", lambda *a, **k: None)
+
+ result = switch_model(
+ raw_input="qwen3:8b",
+ current_provider="openrouter",
+ current_model="anthropic/claude-sonnet-4",
+ current_base_url="https://openrouter.ai/api/v1",
+ current_api_key="sk-openrouter",
+ explicit_provider="custom",
+ user_providers={},
+ custom_providers=[],
+ )
+
+ assert result.success is True
+ assert result.base_url == "http://127.0.0.1:11434/v1"
+ assert result.api_key != "sk-openrouter"
+
+
def test_is_aggregator_recognizes_named_custom_provider():
assert providers_mod.is_aggregator("custom:hpc-ai") is True
assert providers_mod.is_aggregator("custom:litellm") is True