Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(llm): trim API keys in google/anthropic model discovery (#6642)

Huang-404-Q23dfffafd28b
diff --git a/changelog.d/6617.bugfix.md b/changelog.d/6617.bugfix.md
new file mode 100644
index 0000000000..a9588d90da
--- /dev/null
+++ b/changelog.d/6617.bugfix.md
@@ -0,0 +1,5 @@
+- Model discovery now trims a pasted API key the same way research does, for
+ the Google and Anthropic providers. A key saved with surrounding whitespace
+ worked for a research run but failed model listing with a bare auth error,
+ and a whitespace-only key — which is truthy — was sent as the credential
+ instead of being treated as absent.
diff --git a/src/local_deep_research/llm/providers/implementations/anthropic.py b/src/local_deep_research/llm/providers/implementations/anthropic.py
index 0f85c7151b..862369eccd 100644
--- a/src/local_deep_research/llm/providers/implementations/anthropic.py
+++ b/src/local_deep_research/llm/providers/implementations/anthropic.py
@@ -186,6 +186,14 @@ class AnthropicProvider(OpenAICompatibleProvider):
from anthropic import Anthropic, Timeout as AnthropicTimeout
+ # Trim a pasted key the way create_llm's resolve_api_key does.
+ # Without this a key with surrounding whitespace works for
+ # research but fails model discovery with a bare auth error, and a
+ # whitespace-only key is truthy so the placeholder is not
+ # substituted.
+ if isinstance(api_key, str):
+ api_key = api_key.strip()
+
# Pass the key explicitly (placeholder when keyless) so the SDK does
# not read ANTHROPIC_API_KEY from the environment and ship a real
# cloud key to a self-hosted endpoint.
diff --git a/src/local_deep_research/llm/providers/implementations/google.py b/src/local_deep_research/llm/providers/implementations/google.py
index 4e466edc10..8a1ee66137 100644
--- a/src/local_deep_research/llm/providers/implementations/google.py
+++ b/src/local_deep_research/llm/providers/implementations/google.py
@@ -47,6 +47,13 @@ class GoogleProvider(OpenAICompatibleProvider):
Google's OpenAI-compatible /models endpoint returns 401 (bug),
so we use the native Gemini API endpoint instead.
"""
+ # Trim a pasted key the way create_llm's resolve_api_key does.
+ # Without this a key with surrounding whitespace works for research
+ # but fails model discovery with a bare auth error, and a
+ # whitespace-only key is truthy so it is still sent.
+ if isinstance(api_key, str):
+ api_key = api_key.strip()
+
if not api_key:
logger.debug("Google Gemini requires API key for listing models")
return []
diff --git a/tests/llm_providers/implementations/test_anthropic_provider.py b/tests/llm_providers/implementations/test_anthropic_provider.py
index b4743e8004..bb4f4bcbe4 100644
--- a/tests/llm_providers/implementations/test_anthropic_provider.py
+++ b/tests/llm_providers/implementations/test_anthropic_provider.py
@@ -6,6 +6,9 @@ from unittest.mock import Mock, patch
from local_deep_research.llm.providers.implementations.anthropic import (
AnthropicProvider,
)
+from local_deep_research.llm.providers.base import (
+ OPTIONAL_API_KEY_PLACEHOLDER,
+)
class TestAnthropicProviderMetadata:
@@ -306,3 +309,38 @@ class TestAnthropicListModels:
assert (
AnthropicProvider.list_models_for_api(api_key="sk-test") == []
)
+
+ def test_strips_surrounding_whitespace_from_api_key(self):
+ """A pasted key with surrounding whitespace reaches the SDK trimmed.
+
+ ``create_llm``'s ``resolve_api_key`` strips, so an untrimmed key here
+ would work for research but fail model discovery with a bare auth
+ error — the asymmetry #6578 closes in ``openai_base``.
+ """
+ fake_client = Mock()
+ fake_client.models.list.return_value = Mock(data=[])
+ with patch(
+ "anthropic.Anthropic", return_value=fake_client
+ ) as mock_anthropic:
+ AnthropicProvider.list_models_for_api(api_key=" sk-test\n")
+
+ assert mock_anthropic.call_args.kwargs["api_key"] == "sk-test"
+
+ def test_whitespace_only_api_key_uses_the_placeholder(self):
+ """A whitespace-only key is not a key, so the placeholder is sent.
+
+ ``api_key or OPTIONAL_API_KEY_PLACEHOLDER`` cannot catch this on its
+ own — ``" "`` is truthy, so the whitespace would be sent as the
+ credential instead of the keyless placeholder.
+ """
+ fake_client = Mock()
+ fake_client.models.list.return_value = Mock(data=[])
+ with patch(
+ "anthropic.Anthropic", return_value=fake_client
+ ) as mock_anthropic:
+ AnthropicProvider.list_models_for_api(api_key=" ")
+
+ assert (
+ mock_anthropic.call_args.kwargs["api_key"]
+ == OPTIONAL_API_KEY_PLACEHOLDER
+ )
diff --git a/tests/llm_providers/implementations/test_google_provider.py b/tests/llm_providers/implementations/test_google_provider.py
index c4b1439c93..b402ab8ffc 100644
--- a/tests/llm_providers/implementations/test_google_provider.py
+++ b/tests/llm_providers/implementations/test_google_provider.py
@@ -65,6 +65,39 @@ class TestGoogleListModelsForApi:
result = GoogleProvider.list_models_for_api(api_key="")
assert result == []
+ def test_returns_empty_for_whitespace_only_api_key(self):
+ """A whitespace-only key is not a key: no request is made.
+
+ ``" "`` is truthy, so the guard has to strip before deciding.
+ Otherwise the empty key is sent as the ``x-goog-api-key`` header and
+ comes back 401 — a guaranteed-failing request per refresh.
+ """
+ with patch("local_deep_research.security.safe_get") as mock_get:
+ result = GoogleProvider.list_models_for_api(api_key=" ")
+
+ assert result == []
+ mock_get.assert_not_called()
+
+ def test_strips_surrounding_whitespace_from_api_key(self):
+ """A pasted key with surrounding whitespace is sent trimmed.
+
+ ``create_llm``'s ``resolve_api_key`` strips, so an untrimmed key here
+ would work for research but fail model discovery with a bare auth
+ error — the asymmetry #6578 closes in ``openai_base``.
+ """
+ mock_response = Mock()
+ mock_response.status_code = 200
+ mock_response.json.return_value = {"models": []}
+
+ with patch("local_deep_research.security.safe_get") as mock_get:
+ mock_get.return_value = mock_response
+
+ GoogleProvider.list_models_for_api(api_key=" my-test-key\n")
+
+ headers = mock_get.call_args[1].get("headers") or {}
+
+ assert headers.get("x-goog-api-key") == "my-test-key"
+
def test_lists_models_with_valid_key(self):
"""Returns models when valid API key provided."""
mock_response = Mock()