Skip to content

fix(config): honor key_env for custom provider - #43704

Open
Theagentvikram wants to merge 1 commit into
NousResearch:mainfrom
Theagentvikram:fix/custom-model-key-env
Open

Theagentvikram wants to merge 1 commit into
NousResearch:mainfrom
Theagentvikram:fix/custom-model-key-env

Conversation

@Theagentvikram

Copy link
Copy Markdown

Summary

  • honor model.key_env/api_key_env when resolving bare provider: custom
  • keep inline api_key/api precedence ahead of env hints
  • add regression coverage for custom model key_env

Fixes #43586

Tests

  • scripts/run_tests.sh tests/hermes_cli/test_runtime_provider_resolution.py
  • venv312/bin/ruff check hermes_cli/runtime_provider.py tests/hermes_cli/test_runtime_provider_resolution.py

Copilot AI review requested due to automatic review settings June 10, 2026 18:11

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds support for resolving API keys for custom OpenRouter-compatible endpoints from a model-configured env var (e.g. model.key_env), and verifies the behavior with a new regression test.

Changes:

  • Read key_env / api_key_env from model_cfg and use the referenced env var value as an API key candidate for custom endpoints.
  • Add a test ensuring model.key_env is honored for a configured custom endpoint.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
tests/hermes_cli/test_runtime_provider_resolution.py Adds a regression test for model.key_env-based API key resolution on custom endpoints.
hermes_cli/runtime_provider.py Extends _resolve_openrouter_runtime to resolve API keys via model_cfg.key_env / model_cfg.api_key_env.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +782 to +788
cfg_key_env_api_key = ""
for k in ("key_env", "api_key_env"):
env_name = model_cfg.get(k)
if isinstance(env_name, str) and env_name.strip():
cfg_key_env_api_key = os.getenv(env_name.strip(), "").strip()
if cfg_key_env_api_key:
break
Comment on lines 864 to 870
api_key_candidates = [
explicit_api_key,
(cfg_api_key if use_config_base_url else ""),
(cfg_key_env_api_key if use_config_base_url else ""),
(os.getenv("OLLAMA_API_KEY") if _is_ollama_url else ""),
(os.getenv("OPENAI_API_KEY") if (_is_openai_url or _is_openai_azure) else ""),
(os.getenv("OPENROUTER_API_KEY") if _is_openrouter_url else ""),
Comment on lines +603 to +606
monkeypatch.delenv("OPENAI_BASE_URL", raising=False)
monkeypatch.delenv("OPENAI_API_KEY", raising=False)
monkeypatch.delenv("OPENROUTER_API_KEY", raising=False)
monkeypatch.setenv("MY_CUSTOM_API_KEY", "env-custom-key")
Comment on lines +782 to +788
cfg_key_env_api_key = ""
for k in ("key_env", "api_key_env"):
env_name = model_cfg.get(k)
if isinstance(env_name, str) and env_name.strip():
cfg_key_env_api_key = os.getenv(env_name.strip(), "").strip()
if cfg_key_env_api_key:
break
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists comp/cli CLI entry point, hermes_cli/, setup wizard area/config Config system, migrations, profiles labels Jun 10, 2026

@teknium1 teknium1 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the focused regression fix. The current generic bare-custom path still has the reported gap: hermes_cli/runtime_provider.py:1139-1150 has no candidate derived from model.key_env or model.api_key_env.

Problems

  • The new os.getenv() on hermes_cli/runtime_provider.py:788 bypasses _getenv(), whose current contract is profile-scoped, fail-closed credential access (hermes_cli/runtime_provider.py:49-59). Use _getenv() for this credential read.
  • The patch does not cover the direct-alias custom path: hermes_cli/runtime_provider.py:951-964 still has no model env-hint candidate and can return no-key-required.

Suggested changes

  • Centralize scoped resolution of the model env hint and use it in both custom resolution paths, with regression tests for each.

Automated hermes-sweeper review.

if isinstance(env_name, str) and env_name.strip():
cfg_key_env_api_key = os.getenv(env_name.strip(), "").strip()
if cfg_key_env_api_key:
break

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use the module's _getenv() accessor here rather than os.getenv(). Current main defines _getenv() as the profile-scoped, fail-closed credential reader (hermes_cli/runtime_provider.py:49-59); bypassing it can read an unscoped secret under multiplexing.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/config Config system, migrations, profiles comp/cli CLI entry point, hermes_cli/, setup wizard P2 Medium — degraded but workaround exists sweeper:blast-moderate Sweeper blast radius: moderate — a subsystem or single platform sweeper:risk-compatibility Sweeper risk: may break existing users, config, migrations, defaults, or upgrades sweeper:risk-security-boundary Sweeper risk: may affect sandboxing, auth, credentials, or sensitive data type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: model: block with bare 'provider: custom' + key_env ignores the API key (sends 'no-key-required' → 401)

4 participants