Skip to content

The agent that grows with you

fix(auxiliary): reach the main agent model when a sibling aux model fails on the same provider

teknium10a2c245cd6af
diff --git a/agent/auxiliary_client.py b/agent/auxiliary_client.py
index cbc4254e6be..910130daa3a 100644
--- a/agent/auxiliary_client.py
+++ b/agent/auxiliary_client.py
@@ -4188,6 +4188,7 @@ def _try_main_agent_model_fallback(
failed_provider: str,
task: str = None,
reason: str = "error",
+ failed_model: Optional[str] = None,
) -> Tuple[Optional[Any], Optional[str], str]:
"""Last-resort fallback to the user's main agent provider + model.
@@ -4196,8 +4197,23 @@ def _try_main_agent_model_fallback(
layer: if nothing the user asked for can serve the request, try the
main chat model before giving up.
- Skips when the failed provider already IS the main provider (no point
- retrying the same backend that just failed).
+ ``failed_model`` narrows the same-provider skip to the exact
+ (provider, model) pair that just failed, mirroring
+ :func:`_try_configured_fallback_chain`. This matters for self-hosted /
+ custom endpoints serving several models behind one provider label: the
+ aux compression model timing out says nothing about the health of the
+ main agent model deployed on the same URL (real incident: aux
+ ``glm-5.2`` hung and timed out while main ``macaron-v1-venti`` on the
+ identical endpoint was serving 448K-token turns fine — the
+ provider-label skip discarded the one fallback that would have worked).
+
+ - Model-specific runtime failures (timeout, connection, rate limit,
+ model-incompatible, invalid response) pass ``failed_model``: skip the
+ main model only when it IS the exact model that failed.
+ - Provider-wide failures (auth 401, payment 402) and legacy callers
+ leave ``failed_model`` as None, keeping the whole-provider skip —
+ the shared credentials/account are broken, so the main model on the
+ same provider cannot help either.
Returns:
(client, model, provider_label) or (None, None, "") if no fallback.
@@ -4215,8 +4231,12 @@ def _try_main_agent_model_fallback(
return None, None, ""
skip = (failed_provider or "").lower().strip()
- if main_provider.lower() == skip:
- # The thing that failed IS the main model — nothing to fall back to.
+ skip_model = (failed_model or "").strip().lower() or None
+ if main_provider.lower() == skip and (
+ skip_model is None or main_model.lower() == skip_model
+ ):
+ # The thing that failed IS the main model (or the failure was
+ # provider-wide) — nothing to fall back to.
return None, None, ""
if _is_provider_unhealthy(main_provider):
_log_skip_unhealthy(main_provider, task)
@@ -8135,7 +8155,8 @@ def call_llm(
failed_model=_chain_failed_model)
if fb_client is None:
fb_client, fb_model, fb_label = _try_main_agent_model_fallback(
- resolved_provider, task, reason=reason)
+ resolved_provider, task, reason=reason,
+ failed_model=_chain_failed_model)
if fb_client is not None:
fb_resp = _call_fallback_candidate_sync(
@@ -8680,7 +8701,8 @@ async def async_call_llm(
failed_model=_chain_failed_model)
if fb_client is None:
fb_client, fb_model, fb_label = _try_main_agent_model_fallback(
- resolved_provider, task, reason=reason)
+ resolved_provider, task, reason=reason,
+ failed_model=_chain_failed_model)
if fb_client is not None:
# Convert sync fallback client to async
diff --git a/contributors/emails/[email protected] b/contributors/emails/[email protected]
new file mode 100644
index 00000000000..3445cdf6536
--- /dev/null
+++ b/contributors/emails/[email protected]
@@ -0,0 +1 @@
+okalentiev
diff --git a/tests/agent/test_auxiliary_client.py b/tests/agent/test_auxiliary_client.py
index 5fece4c4f78..8da9bd1edf5 100644
--- a/tests/agent/test_auxiliary_client.py
+++ b/tests/agent/test_auxiliary_client.py
@@ -3167,6 +3167,47 @@ class TestTryMainAgentModelFallback:
client, model, label = _try_main_agent_model_fallback("glm", task="vision")
assert client is None
+ def test_same_provider_different_model_falls_back_when_failed_model_given(self):
+ """Self-hosted shape: aux model and main model share one custom
+ provider label. A timeout on the aux model must still reach the main
+ model — same provider, DIFFERENT model (real incident: aux glm-5.2
+ timed out while main macaron-v1-venti on the same endpoint was
+ healthy; the provider-label skip discarded the working fallback)."""
+ from agent.auxiliary_client import _try_main_agent_model_fallback
+ fake_client = MagicMock()
+ with patch("agent.auxiliary_client._read_main_provider", return_value="custom"), \
+ patch("agent.auxiliary_client._read_main_model", return_value="mindai/macaron-v1-venti"), \
+ patch("agent.auxiliary_client._is_provider_unhealthy", return_value=False), \
+ patch("agent.auxiliary_client.resolve_provider_client",
+ return_value=(fake_client, "mindai/macaron-v1-venti")):
+ client, model, label = _try_main_agent_model_fallback(
+ "custom", task="compression", failed_model="zai-org/glm-5.2")
+ assert client is fake_client
+ assert model == "mindai/macaron-v1-venti"
+ assert label == "main-agent(custom)"
+
+ def test_same_provider_same_model_still_skips_with_failed_model(self):
+ """When the model that failed IS the main model, there is nothing to
+ fall back to — the narrowed skip must not regress into a self-retry."""
+ from agent.auxiliary_client import _try_main_agent_model_fallback
+ with patch("agent.auxiliary_client._read_main_provider", return_value="custom"), \
+ patch("agent.auxiliary_client._read_main_model", return_value="mindai/macaron-v1-venti"):
+ client, model, label = _try_main_agent_model_fallback(
+ "custom", task="compression",
+ failed_model="MindAI/Macaron-V1-Venti") # case-insensitive match
+ assert client is None and model is None and label == ""
+
+ def test_same_provider_no_failed_model_keeps_provider_wide_skip(self):
+ """Legacy / provider-wide callers (auth 401, payment 402) pass no
+ failed_model: the whole-provider skip must be preserved so broken
+ shared credentials don't trigger a doomed main-model attempt."""
+ from agent.auxiliary_client import _try_main_agent_model_fallback
+ with patch("agent.auxiliary_client._read_main_provider", return_value="custom"), \
+ patch("agent.auxiliary_client._read_main_model", return_value="mindai/macaron-v1-venti"):
+ client, model, label = _try_main_agent_model_fallback(
+ "custom", task="compression")
+ assert client is None and model is None and label == ""
+
# ---------------------------------------------------------------------------
# Gate: _resolve_api_key_provider must skip anthropic when not configured