hermes - ✅(Solved) Fix [Bug] fallback_providers ignores key_env — only reads api_key field [1 pull requests, 2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
NousResearch/hermes-agent#14105Fetched 2026-04-23 07:46:46
View on GitHub
Comments
2
Participants
2
Timeline
8
Reactions
0
Author
Timeline (top)
labeled ×4commented ×2closed ×1cross-referenced ×1

Error Message

  1. Observe that the fallback fails with an authentication error — the env var is never read.

Root Cause

In run_agent.py ~line 6149:

fb_api_key_hint = (fb.get("api_key") or "").strip() or None

This only checks the api_key key in the fallback config dict. There is no lookup for key_env to resolve the actual API key from environment variables.

Note that PR #9016 fixed key_env resolution for providers: dict in runtime_provider.py and auxiliary_client.py, but the fallback chain path in run_agent.py was not covered.

Fix Action

Workaround

Use api_key with the literal value instead of key_env:

fallback_providers:
  - base_url: https://ollama.com/v1
    model: minimax-m2.7
    provider: openai-compatible
    api_key: your-actual-key-here

PR fix notes

PR #14116: fix(agent): resolve fallback provider key_env secrets

Description (problem / solution / changelog)

Summary

  • resolve fallback_providers API keys from key_env when api_key is unset
  • preserve the existing explicit api_key path and Ollama-specific env fallback
  • add a regression test covering env-backed fallback credentials

Testing

  • pytest -o addopts= tests/run_agent/test_provider_fallback.py
  • pytest -o addopts= tests/run_agent/test_fallback_model.py tests/run_agent/test_provider_fallback.py

Closes #14105

Changed files

  • run_agent.py (modified, +4/-0)
  • tests/run_agent/test_provider_fallback.py (modified, +26/-0)

Code Example

fb_api_key_hint = (fb.get("api_key") or "").strip() or None

---

fallback_providers:
     - base_url: https://ollama.com/v1
       model: minimax-m2.7
       provider: openai-compatible
       key_env: OLLAMA_KEY_1

---

fb_api_key_hint = (fb.get("api_key") or "").strip() or None
if not fb_api_key_hint:
    _key_env = fb.get("key_env")
    if _key_env:
        fb_api_key_hint = os.getenv(_key_env) or None

---

fallback_providers:
  - base_url: https://ollama.com/v1
    model: minimax-m2.7
    provider: openai-compatible
    api_key: your-actual-key-here
RAW_BUFFERClick to expand / collapse

Bug Description

The fallback_providers chain in run_agent.py does not resolve key_env from the config. It only reads the api_key field directly, so any fallback entry using key_env is silently treated as having no API key.

Root Cause

In run_agent.py ~line 6149:

fb_api_key_hint = (fb.get("api_key") or "").strip() or None

This only checks the api_key key in the fallback config dict. There is no lookup for key_env to resolve the actual API key from environment variables.

Note that PR #9016 fixed key_env resolution for providers: dict in runtime_provider.py and auxiliary_client.py, but the fallback chain path in run_agent.py was not covered.

Steps to Reproduce

  1. Configure fallback providers using key_env in config.yaml:
    fallback_providers:
      - base_url: https://ollama.com/v1
        model: minimax-m2.7
        provider: openai-compatible
        key_env: OLLAMA_KEY_1
  2. Ensure OLLAMA_KEY_1 is set in .env.
  3. Trigger a rate limit on the primary provider so the fallback chain activates.
  4. Observe that the fallback fails with an authentication error — the env var is never read.

Expected Behavior

When api_key is not present in the fallback config, Hermes should check key_env and resolve the API key from the specified environment variable, consistent with how providers: dict works after PR #9016.

Suggested Fix

In _try_activate_fallback() around line 6149, add key_env resolution:

fb_api_key_hint = (fb.get("api_key") or "").strip() or None
if not fb_api_key_hint:
    _key_env = fb.get("key_env")
    if _key_env:
        fb_api_key_hint = os.getenv(_key_env) or None

Workaround

Use api_key with the literal value instead of key_env:

fallback_providers:
  - base_url: https://ollama.com/v1
    model: minimax-m2.7
    provider: openai-compatible
    api_key: your-actual-key-here

Environment

  • Hermes Agent: latest (post v0.8)
  • Config schema: v12+
  • OS: Linux (Ubuntu)

extent analysis

TL;DR

To fix the issue, update the _try_activate_fallback() function in run_agent.py to resolve key_env from the config and retrieve the API key from the specified environment variable.

Guidance

  • Verify that the key_env field is correctly set in the fallback_providers config and that the corresponding environment variable is defined.
  • Update the _try_activate_fallback() function as suggested in the issue to add key_env resolution.
  • Test the fallback chain with the updated code to ensure it correctly resolves the API key from the environment variable.
  • As a temporary workaround, use the api_key field with the literal API key value instead of key_env in the fallback_providers config.

Example

The suggested fix code snippet is already provided in the issue:

fb_api_key_hint = (fb.get("api_key") or "").strip() or None
if not fb_api_key_hint:
    _key_env = fb.get("key_env")
    if _key_env:
        fb_api_key_hint = os.getenv(_key_env) or None

Notes

This fix assumes that the os module is already imported in the run_agent.py file. If not, add import os at the top of the file.

Recommendation

Apply the suggested fix to update the _try_activate_fallback() function, as it provides a more flexible and consistent way to resolve API keys from environment variables.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING