Skip to content

Commit 808fee1

Browse files
nftpoetristteknium1
authored andcommitted
fix(auxiliary): propagate explicit_api_key to _try_anthropic()
_try_anthropic() lacked the explicit_api_key parameter added to _try_openrouter() in #18768. When resolve_provider_client() is called with provider="anthropic" and an explicit key (e.g. from a fallback_model entry with api_key set), the key was silently ignored — _try_anthropic() always fell back to resolve_anthropic_token(), so the fallback returned None,None for users without a default Anthropic credential configured. Fix: add explicit_api_key: str = None to _try_anthropic() and use explicit_api_key or <pool/env fallback> in both the pool-present and no-pool paths. Pass explicit_api_key=explicit_api_key at the call site in resolve_provider_client(). Symmetric with the _try_openrouter() fix. No behavior change when explicit_api_key is None.
1 parent 74636f9 commit 808fee1

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

‎agent/auxiliary_client.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,7 +1529,7 @@ def _build_codex_client(model: str) -> Tuple[Optional[Any], Optional[str]]:
15291529
return CodexAuxiliaryClient(real_client, model), model
15301530

15311531

1532-
def _try_anthropic() -> Tuple[Optional[Any], Optional[str]]:
1532+
def _try_anthropic(explicit_api_key: str = None) -> Tuple[Optional[Any], Optional[str]]:
15331533
try:
15341534
from agent.anthropic_adapter import build_anthropic_client, resolve_anthropic_token
15351535
except ImportError:
@@ -1539,10 +1539,10 @@ def _try_anthropic() -> Tuple[Optional[Any], Optional[str]]:
15391539
if pool_present:
15401540
if entry is None:
15411541
return None, None
1542-
token = _pool_runtime_api_key(entry)
1542+
token = explicit_api_key or _pool_runtime_api_key(entry)
15431543
else:
15441544
entry = None
1545-
token = resolve_anthropic_token()
1545+
token = explicit_api_key or resolve_anthropic_token()
15461546
if not token:
15471547
return None, None
15481548

@@ -2336,7 +2336,7 @@ def _wrap_if_needed(client_obj, final_model_str: str, base_url_str: str = "",
23362336

23372337
if pconfig.auth_type == "api_key":
23382338
if provider == "anthropic":
2339-
client, default_model = _try_anthropic()
2339+
client, default_model = _try_anthropic(explicit_api_key=explicit_api_key)
23402340
if client is None:
23412341
logger.warning("resolve_provider_client: anthropic requested but no Anthropic credentials found")
23422342
return None, None

‎tests/agent/test_auxiliary_client.py‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,3 +1893,53 @@ def test_resolve_provider_client_without_explicit_api_key_falls_back_to_env(
18931893
assert call_kwargs["api_key"] == "env-fallback-key", (
18941894
f"Expected env fallback key to be used when explicit_api_key is None, got: {call_kwargs['api_key']}"
18951895
)
1896+
1897+
1898+
class TestAnthropicExplicitApiKey:
1899+
"""Test that explicit_api_key is correctly propagated to _try_anthropic().
1900+
1901+
Parity with the OpenRouter fix in #18768: resolve_provider_client() passes
1902+
explicit_api_key to _try_openrouter(), but the anthropic branch was not
1903+
updated — _try_anthropic() always fell back to resolve_anthropic_token()
1904+
even when an explicit key was supplied (e.g. from a fallback_model entry).
1905+
"""
1906+
1907+
def test_try_anthropic_uses_explicit_api_key_over_env(self):
1908+
"""_try_anthropic(explicit_api_key) must use the supplied key, not the env fallback."""
1909+
with patch("agent.anthropic_adapter.resolve_anthropic_token", return_value="env-fallback-key"), \
1910+
patch("agent.anthropic_adapter.build_anthropic_client") as mock_build, \
1911+
patch("agent.auxiliary_client._select_pool_entry", return_value=(False, None)):
1912+
mock_build.return_value = MagicMock()
1913+
from agent.auxiliary_client import _try_anthropic
1914+
client, model = _try_anthropic("explicit-pool-key")
1915+
assert client is not None
1916+
assert mock_build.call_args.args[0] == "explicit-pool-key", (
1917+
f"Expected explicit_api_key to be passed, got: {mock_build.call_args.args[0]}"
1918+
)
1919+
assert mock_build.call_args.args[0] != "env-fallback-key"
1920+
1921+
def test_try_anthropic_without_explicit_key_falls_back_to_resolve(self):
1922+
"""Without explicit_api_key, _try_anthropic falls back to resolve_anthropic_token."""
1923+
with patch("agent.anthropic_adapter.resolve_anthropic_token", return_value="env-fallback-key"), \
1924+
patch("agent.anthropic_adapter.build_anthropic_client") as mock_build, \
1925+
patch("agent.auxiliary_client._select_pool_entry", return_value=(False, None)):
1926+
mock_build.return_value = MagicMock()
1927+
from agent.auxiliary_client import _try_anthropic
1928+
client, model = _try_anthropic()
1929+
assert client is not None
1930+
assert mock_build.call_args.args[0] == "env-fallback-key"
1931+
1932+
def test_resolve_provider_client_passes_explicit_api_key_to_anthropic(self):
1933+
"""resolve_provider_client(provider='anthropic', explicit_api_key=...) must propagate the key."""
1934+
with patch("agent.anthropic_adapter.resolve_anthropic_token", return_value="env-key"), \
1935+
patch("agent.anthropic_adapter.build_anthropic_client") as mock_build, \
1936+
patch("agent.auxiliary_client._select_pool_entry", return_value=(False, None)):
1937+
mock_build.return_value = MagicMock()
1938+
client, model = resolve_provider_client(
1939+
provider="anthropic",
1940+
explicit_api_key="explicit-fallback-key",
1941+
)
1942+
assert client is not None
1943+
assert mock_build.call_args.args[0] == "explicit-fallback-key", (
1944+
"resolve_provider_client must forward explicit_api_key to _try_anthropic()"
1945+
)

0 commit comments

Comments
 (0)