The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: preserve anthropic_messages api_mode during fallback activation
sjungwon0317a675574c2d
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.pyindex 658919a5fd3..44fd782fa65 100644--- a/agent/chat_completion_helpers.py+++ b/agent/chat_completion_helpers.py@@ -2034,6 +2034,26 @@ def try_activate_fallback(agent, reason: "FailoverReason | None" = None) -> boolfb_base_url_hint = (fb.get("base_url") or "").strip() or Nonefb_api_key_hint = resolve_entry_api_key(fb)+ # Determine api_mode from the ORIGINAL base_url (before URL transformation).+ # resolve_provider_client() calls _to_openai_base_url() which rewrites+ # /anthropic to /v1, losing the anthropic wire signal. Pre-compute here.+ fb_api_mode = "chat_completions"+ if fb.get("api_mode"):+ fb_api_mode = str(fb.get("api_mode")).strip()+ elif fb_provider == "anthropic":+ # Provider-name check must not be gated on fb_base_url_hint:+ # an entry that names provider: anthropic without an explicit+ # base_url uses the provider's default endpoint and must still+ # resolve to anthropic_messages, not chat_completions.+ fb_api_mode = "anthropic_messages"+ elif fb_base_url_hint:+ _orig_url = fb_base_url_hint.rstrip("/").lower()+ if (+ _orig_url.endswith("/anthropic")+ or base_url_hostname(fb_base_url_hint) == "api.anthropic.com"+ ):+ fb_api_mode = "anthropic_messages"+# For Ollama Cloud endpoints, pull OLLAMA_API_KEY from env# when no explicit key is in the fallback config. Host match# (not substring) — see GHSA-76xc-57q6-vm5m.@@ -2044,7 +2064,8 @@ def try_activate_fallback(agent, reason: "FailoverReason | None" = None) -> boolfb_client, _resolved_fb_model = resolve_provider_client(fb_provider, model=fb_model, raw_codex=True,explicit_base_url=fb_base_url_hint,- explicit_api_key=fb_api_key_hint)+ explicit_api_key=fb_api_key_hint,+ api_mode=fb_api_mode)if fb_client is None:logger.warning("Fallback to %s failed: provider not configured",@@ -2062,50 +2083,43 @@ def try_activate_fallback(agent, reason: "FailoverReason | None" = None) -> bool)# Determine api_mode from provider / base URL / model- fb_api_mode = "chat_completions"+ # If fb_api_mode was already pre-computed from original URL, preserve it+ if not locals().get('fb_api_mode'):+ fb_api_mode = "chat_completions"fb_base_url = str(fb_client.base_url)_fb_is_azure = agent._is_azure_openai_url(fb_base_url)- if fb_provider == "openai-codex":- fb_api_mode = "codex_responses"- elif fb_provider in {"nous", "nous-portal", "nousresearch"}:- # Portal is dual-wire: anthropic/* must land on /v1/messages.- # resolve_provider_client still returns an OpenAI client for- # Nous; the anthropic_messages branch below rebuilds the native- # client from that credential + base_url.- from hermes_cli.providers import nous_api_mode-- fb_api_mode = nous_api_mode(fb_model)- elif (- fb_provider == "anthropic"- or fb_base_url.rstrip("/").lower().endswith("/anthropic")- or base_url_hostname(fb_base_url) == "api.anthropic.com"- ):- # Custom providers (e.g. cron-anthropic) point at the native- # api.anthropic.com host with no "/anthropic" path suffix, so the- # name/suffix checks above miss them and they default to- # chat_completions → POST /v1/chat/completions → 404. Match the- # host the same way determine_api_mode() and _detect_api_mode_for_url()- # do on the primary path. (#32243, #49247)- fb_api_mode = "anthropic_messages"- elif _fb_is_azure:- # Azure OpenAI serves gpt-5.x on /chat/completions — does NOT- # support the Responses API. Stay on chat_completions.- fb_api_mode = "chat_completions"- elif agent._is_direct_openai_url(fb_base_url):- fb_api_mode = "codex_responses"- elif agent._provider_model_requires_responses_api(- fb_model,- provider=fb_provider,- ):- # GPT-5.x models usually need Responses API, but keep- # provider-specific exceptions like Copilot gpt-5-mini on- # chat completions.- fb_api_mode = "codex_responses"- elif fb_provider == "bedrock" or (- base_url_hostname(fb_base_url).startswith("bedrock-runtime.")- and base_url_host_matches(fb_base_url, "amazonaws.com")- ):- fb_api_mode = "bedrock_converse"++ # Only re-determine if not already set from original URL+ if fb_api_mode == "chat_completions":+ if fb_provider == "openai-codex":+ fb_api_mode = "codex_responses"+ elif fb_provider in {"nous", "nous-portal", "nousresearch"}:+ # Portal is dual-wire: anthropic/* must land on /v1/messages.+ # resolve_provider_client still returns an OpenAI client for+ # Nous; the anthropic_messages branch below rebuilds the native+ # client from that credential + base_url.+ from hermes_cli.providers import nous_api_mode++ fb_api_mode = nous_api_mode(fb_model)+ elif _fb_is_azure:+ # Azure OpenAI serves gpt-5.x on /chat/completions — does NOT+ # support the Responses API. Stay on chat_completions.+ fb_api_mode = "chat_completions"+ elif agent._is_direct_openai_url(fb_base_url):+ fb_api_mode = "codex_responses"+ elif agent._provider_model_requires_responses_api(+ fb_model,+ provider=fb_provider,+ ):+ # GPT-5.x models usually need Responses API, but keep+ # provider-specific exceptions like Copilot gpt-5-mini on+ # chat completions.+ fb_api_mode = "codex_responses"+ elif fb_provider == "bedrock" or (+ base_url_hostname(fb_base_url).startswith("bedrock-runtime.")+ and base_url_host_matches(fb_base_url, "amazonaws.com")+ ):+ fb_api_mode = "bedrock_converse"old_model = agent.modelold_provider = agent.provider