Skip to content

fix: fail fast when explicit provider has no API key instead of silent OpenRouter fallback#2272

Closed
StefanIsMe wants to merge 1 commit intoNousResearch:mainfrom
StefanIsMe:fix/explicit-provider-no-credential-fail-fast
Closed

fix: fail fast when explicit provider has no API key instead of silent OpenRouter fallback#2272
StefanIsMe wants to merge 1 commit intoNousResearch:mainfrom
StefanIsMe:fix/explicit-provider-no-credential-fail-fast

Conversation

@StefanIsMe
Copy link
Copy Markdown
Contributor

Problem

When provider: minimax (or any non-OpenRouter provider) is set in config.yaml but the corresponding API key environment variable (MINIMAX_API_KEY) is not set, Hermes silently falls back to OpenRouter.

This causes a confusing 404 error because:

  1. OpenRouter receives the request with the MiniMax model name
  2. OpenRouter tries to route to MiniMax's API but gets a 404 (wrong endpoint or no credentials for that route)
  3. The user sees a 404 with no explanation of why Hermes is talking to OpenRouter at all

Root Cause

Two code paths silently fell back to OpenRouter when a provider had no credentials:

1. run_agent.py AIAgent.init (lines 734-744):

else:
    # Final fallback: try raw OpenRouter key
    client_kwargs = {api_key: ..., base_url: OPENROUTER_BASE_URL}

2. auxiliary_client.py call_llm (lines 1429-1435):

if client is None:
    if resolved_provider != "openrouter" and not resolved_base_url:
        logger.warning("Provider %s unavailable, falling back to openrouter")
        client, final_model = _get_cached_client("openrouter", ...)

Fix

In both locations, before falling back to OpenRouter, check if the user explicitly configured a non-OpenRouter provider. If so, raise RuntimeError with a clear message:

For auto/custom/openrouter providers, the OpenRouter fallback behavior is preserved — appropriate for auto-detection scenarios where the user hasn't pinned a specific provider.

Testing

Logic verified:

  • 'minimax' with no key → raises error telling user to set MINIMAX_API_KEY
  • 'anthropic' with no key → raises error telling user to set ANTHROPIC_API_KEY
  • 'auto'/'openrouter'/'custom' → falls through to OpenRouter (unchanged)
…t OpenRouter fallback

When provider: minimax (or any non-OpenRouter provider) is set in
config.yaml but the corresponding API key environment variable is not set,
Hermes was silently falling back to OpenRouter. This produces confusing
404 errors since OpenRouter routes the model name to a different endpoint
than the user intended.

The fix adds a fail-fast check in two locations:

1. run_agent.py AIAgent.__init__: after resolve_provider_client() returns
   None, raise RuntimeError with the exact env var to set (e.g.
   MINIMAX_API_KEY) and the command to switch providers.

2. auxiliary_client.py call_llm(): same fail-fast for the same condition.

auto/custom/openrouter providers are exempted — their OpenRouter fallback
behavior is preserved since those are auto-detection scenarios.
teknium1 pushed a commit that referenced this pull request Mar 22, 2026
…t OpenRouter fallback

When a non-OpenRouter provider (e.g. minimax, anthropic) is set in
config.yaml but its API key is missing, Hermes silently fell back to
OpenRouter, causing confusing 404 errors.

Now checks if the user explicitly configured a provider before falling
back. Explicit providers raise RuntimeError with a clear message naming
the missing env var. Auto/openrouter/custom providers still fall through
to OpenRouter as before.

Three code paths fixed:
- run_agent.py AIAgent.__init__ — main client initialization
- auxiliary_client.py call_llm — sync auxiliary calls
- auxiliary_client.py call_llm_streaming — async auxiliary calls

Based on PR #2272 by @StefanIsMe. Applied manually to fix a
pconfig NameError in the original and extend to call_llm_streaming.
teknium1 added a commit that referenced this pull request Mar 22, 2026
…t OpenRouter fallback (#2445)

When a non-OpenRouter provider (e.g. minimax, anthropic) is set in
config.yaml but its API key is missing, Hermes silently fell back to
OpenRouter, causing confusing 404 errors.

Now checks if the user explicitly configured a provider before falling
back. Explicit providers raise RuntimeError with a clear message naming
the missing env var. Auto/openrouter/custom providers still fall through
to OpenRouter as before.

Three code paths fixed:
- run_agent.py AIAgent.__init__ — main client initialization
- auxiliary_client.py call_llm — sync auxiliary calls
- auxiliary_client.py call_llm_streaming — async auxiliary calls

Based on PR #2272 by @StefanIsMe. Applied manually to fix a
pconfig NameError in the original and extend to call_llm_streaming.

Co-authored-by: StefanIsMe <StefanIsMe@users.noreply.github.com>
@teknium1
Copy link
Copy Markdown
Contributor

Merged via PR #2445. Your fix was applied manually onto current main with two improvements:

  1. Fixed a pconfig.api_key_env_vars NameError in the auxiliary_client.py path (pconfig doesn't exist in call_llm's scope)
  2. Extended the guard to call_llm_streaming which had the same silent OpenRouter fallback

Your authorship is preserved on the commit. Thanks @StefanIsMe!

@teknium1 teknium1 closed this Mar 22, 2026
outsourc-e pushed a commit to outsourc-e/hermes-agent that referenced this pull request Mar 26, 2026
…t OpenRouter fallback (NousResearch#2445)

When a non-OpenRouter provider (e.g. minimax, anthropic) is set in
config.yaml but its API key is missing, Hermes silently fell back to
OpenRouter, causing confusing 404 errors.

Now checks if the user explicitly configured a provider before falling
back. Explicit providers raise RuntimeError with a clear message naming
the missing env var. Auto/openrouter/custom providers still fall through
to OpenRouter as before.

Three code paths fixed:
- run_agent.py AIAgent.__init__ — main client initialization
- auxiliary_client.py call_llm — sync auxiliary calls
- auxiliary_client.py call_llm_streaming — async auxiliary calls

Based on PR NousResearch#2272 by @StefanIsMe. Applied manually to fix a
pconfig NameError in the original and extend to call_llm_streaming.

Co-authored-by: StefanIsMe <StefanIsMe@users.noreply.github.com>
aashizpoudel pushed a commit to aashizpoudel/hermes-agent that referenced this pull request Mar 30, 2026
…t OpenRouter fallback (NousResearch#2445)

When a non-OpenRouter provider (e.g. minimax, anthropic) is set in
config.yaml but its API key is missing, Hermes silently fell back to
OpenRouter, causing confusing 404 errors.

Now checks if the user explicitly configured a provider before falling
back. Explicit providers raise RuntimeError with a clear message naming
the missing env var. Auto/openrouter/custom providers still fall through
to OpenRouter as before.

Three code paths fixed:
- run_agent.py AIAgent.__init__ — main client initialization
- auxiliary_client.py call_llm — sync auxiliary calls
- auxiliary_client.py call_llm_streaming — async auxiliary calls

Based on PR NousResearch#2272 by @StefanIsMe. Applied manually to fix a
pconfig NameError in the original and extend to call_llm_streaming.

Co-authored-by: StefanIsMe <StefanIsMe@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants