fix: clear stale api_key on provider switch in _model_flow_api_key_provider

When switching from one API-key provider to another via 'hermes model',
the old provider's api_key was left in config.yaml's model.api_key field.
Built-in providers get their keys from env vars / credential pool, so the
stale key caused credential drift — the new provider would try to
authenticate with the old key and fail with 401.

The sister function in auth.py (set_provider_in_config) correctly pops
both api_key and api_mode on provider switch. This adds the missing
model.pop('api_key', None) call, mirroring auth.py:2764.

Fixes #14134
This commit is contained in:
AJ 2026-04-23 00:42:35 -04:00
parent 4fade39c90
commit fd13002d99
2 changed files with 196 additions and 0 deletions

View file

@ -4191,6 +4191,11 @@ def _model_flow_api_key_provider(config, provider_id, current_model=""):
model["api_mode"] = opencode_model_api_mode(provider_id, selected)
else:
model.pop("api_mode", None)
# Clear stale api_key from a previous provider. Built-in providers
# get their keys from env vars / credential pool — a leftover key
# from a prior provider causes credential drift (401 errors).
# Mirrors auth.py set_provider_in_config (line ~2764). (#14134)
model.pop("api_key", None)
save_config(cfg)
deactivate_provider()