Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.
Python agent-evaluationaiai-alignmentai-evaluationai-governanceai-safetyclidiagnostic-tooleu-ai-acthallucination-detectioniso-42001llm-evaluationllm-securitynist-ai-rmfowasp-llmprompt-injectionpythonresponsible-airisk-assessmentrisk-management
fix(gemini): treat rejected API keys as fatal auth errors (#338)
Rudy Mizrahi Celekli5d62e0ccfa79
diff --git a/ifixai/providers/gemini.py b/ifixai/providers/gemini.pyindex 45a6342..74c8154 100644--- a/ifixai/providers/gemini.py+++ b/ifixai/providers/gemini.py@@ -185,6 +185,12 @@ class GeminiProvider(ChatProvider):details=str(exc),) from excexcept google_exceptions.GoogleAPIError as exc:+ if _invalid_api_key(exc):+ raise ProviderAuthError(+ provider="gemini",+ endpoint=endpoint,+ details=str(exc),+ ) from excif getattr(exc, "code", None) in RETRYABLE_HTTP_STATUS_CODES:if attempt < attempts - 1:await asyncio.sleep(backoff)@@ -227,6 +233,14 @@ def _format_messages(return GeminiMessages(system_instruction="\n".join(system_instructions), contents=contents)+def _invalid_api_key(exc: google_exceptions.GoogleAPIError) -> bool:+ # Google rejects a bad key as 400 INVALID_ARGUMENT, not UNAUTHENTICATED.+ return (+ getattr(exc, "reason", None) == "API_KEY_INVALID"+ or "api key not valid" in str(exc).lower()+ )++def _json_mode_unsupported(exc: google_exceptions.InvalidArgument) -> bool:message = " ".join(str(exc).lower().split())return bool(diff --git a/ifixai/tests/test_gemini_json_fallback_native.py b/ifixai/tests/test_gemini_json_fallback_native.pyindex edc5eda..6b1785a 100644--- a/ifixai/tests/test_gemini_json_fallback_native.py+++ b/ifixai/tests/test_gemini_json_fallback_native.py@@ -99,6 +99,18 @@ async def test_unrelated_invalid_argument_and_auth_do_not_fallback(native_endpoiassert len(calls) == 1+async def test_invalid_api_key_is_a_fatal_auth_error(native_endpoint):+ from ifixai.providers.base import is_fatal_provider_error++ gemini, calls, state = native_endpoint+ import grpc+ state.update(error="API key not valid. Please pass a valid API key.", code=grpc.StatusCode.INVALID_ARGUMENT, reject_plain=True)+ with pytest.raises(ProviderAuthError) as raised:+ await gemini.GeminiProvider().send_message([ChatMessage(content="hello")], _config())+ assert is_fatal_provider_error(raised.value)+ assert len(calls) == 1++async def test_fallback_rejection_is_terminal(native_endpoint):gemini, calls, state = native_endpointstate["reject_plain"] = True