Skip to content

The agent that grows with you

fix(agent): carry request_overrides through in-place /model switch (TUI/CLI)

Jackd2af9900431b
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.py
index dd30d99ed54..20699283e59 100644
--- a/agent/agent_runtime_helpers.py
+++ b/agent/agent_runtime_helpers.py
@@ -2848,6 +2848,29 @@ def create_openai_client(agent, client_kwargs: dict, *, reason: str, shared: boo
return client
+def _apply_switched_provider_request_overrides(agent, new_provider):
+ """Re-derive the switched-to provider's ``request_overrides`` onto a live agent.
+
+ A ``custom_providers`` entry can carry an ``extra_body`` (e.g.
+ ``chat_template_kwargs`` to toggle a local model's thinking). The gateway
+ rebuild path carries this via ``request_overrides``; an *in-place* swap
+ (CLI / TUI ``/model``) must re-derive it for the new provider, otherwise the
+ previous provider's ``extra_body`` lingers. Non-provider overrides
+ (``service_tier`` / ``speed`` from ``/fast``) are preserved.
+ """
+ from hermes_cli.runtime_provider import (
+ _get_named_custom_provider,
+ _custom_provider_request_overrides,
+ )
+ cp = _get_named_custom_provider(new_provider)
+ new_ro = _custom_provider_request_overrides(cp) if cp else None
+ overrides = dict(getattr(agent, "request_overrides", {}) or {})
+ overrides.pop("extra_body", None)
+ if new_ro and new_ro.get("extra_body"):
+ overrides["extra_body"] = new_ro["extra_body"]
+ agent.request_overrides = overrides
+
+
def switch_model(agent, new_model, new_provider, api_key='', base_url='', api_mode=''):
"""Switch the model/provider in-place for a live agent.
@@ -3278,6 +3301,13 @@ def switch_model(agent, new_model, new_provider, api_key='', base_url='', api_mo
agent._fallback_chain = fallback_chain
agent._fallback_model = fallback_chain[0] if fallback_chain else None
+ # Apply the switched-to provider's request_overrides (custom_providers
+ # extra_body, e.g. chat_template_kwargs). See helper for rationale.
+ try:
+ _apply_switched_provider_request_overrides(agent, new_provider)
+ except Exception:
+ logger.debug("switch_model: request_overrides re-derivation failed", exc_info=True)
+
logger.info(
"Model switched in-place: %s (%s) -> %s (%s)",
old_model, old_provider, new_model, new_provider,
diff --git a/tests/agent/test_switch_model_request_overrides.py b/tests/agent/test_switch_model_request_overrides.py
new file mode 100644
index 00000000000..30543e896dc
--- /dev/null
+++ b/tests/agent/test_switch_model_request_overrides.py
@@ -0,0 +1,52 @@
+"""Regression tests for the in-place /model switch (CLI/TUI) carrying a custom
+provider's request_overrides (extra_body) — _apply_switched_provider_request_overrides.
+
+Before the fix, agent_runtime_helpers.switch_model() swapped model/provider/
+base_url/api_key in place but never touched request_overrides, so a /model
+switch to a thinking-enabled custom provider in the TUI/CLI kept the old
+provider's extra_body.
+"""
+
+import agent.agent_runtime_helpers as arh
+
+
+class _Agent:
+ pass
+
+
+def test_switch_applies_new_provider_extra_body(monkeypatch):
+ a = _Agent()
+ a.request_overrides = {"service_tier": "priority"} # pre-existing /fast override
+ monkeypatch.setattr(
+ "hermes_cli.runtime_provider._get_named_custom_provider",
+ lambda name: {"name": "main-think",
+ "extra_body": {"chat_template_kwargs": {"enable_thinking": True}}},
+ )
+ arh._apply_switched_provider_request_overrides(a, "custom:main-think")
+ assert a.request_overrides["extra_body"] == {"chat_template_kwargs": {"enable_thinking": True}}
+ assert a.request_overrides["service_tier"] == "priority" # preserved
+
+
+def test_switch_to_noncustom_clears_stale_extra_body(monkeypatch):
+ a = _Agent()
+ a.request_overrides = {
+ "extra_body": {"chat_template_kwargs": {"enable_thinking": True}},
+ "service_tier": "priority",
+ }
+ monkeypatch.setattr(
+ "hermes_cli.runtime_provider._get_named_custom_provider", lambda name: None
+ )
+ arh._apply_switched_provider_request_overrides(a, "anthropic")
+ assert "extra_body" not in a.request_overrides # stale extra_body cleared
+ assert a.request_overrides["service_tier"] == "priority" # preserved
+
+
+def test_switch_from_none_overrides(monkeypatch):
+ a = _Agent()
+ a.request_overrides = None
+ monkeypatch.setattr(
+ "hermes_cli.runtime_provider._get_named_custom_provider",
+ lambda name: {"name": "main", "extra_body": {"chat_template_kwargs": {"enable_thinking": False}}},
+ )
+ arh._apply_switched_provider_request_overrides(a, "custom:main")
+ assert a.request_overrides == {"extra_body": {"chat_template_kwargs": {"enable_thinking": False}}}