:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(aio): map Gemini model-not-found errors to ModelNotFoundError (#69070)
posthog[bot]d01c279547ed
diff --git a/products/ai_observability/backend/llm/providers/gemini.py b/products/ai_observability/backend/llm/providers/gemini.pyindex 452339bee23f..5f580fff8757 100644--- a/products/ai_observability/backend/llm/providers/gemini.py+++ b/products/ai_observability/backend/llm/providers/gemini.py@@ -17,6 +17,8 @@ from pydantic import BaseModelfrom products.ai_observability.backend.llm.errors import (AuthenticationError,+ ModelNotFoundError,+ ModelPermissionError,QuotaExceededError,RateLimitError,StructuredOutputParseError,@@ -46,7 +48,6 @@ class GeminiConfig:"gemini-2.5-flash-lite","gemini-2.5-pro","gemini-2.0-flash",- "gemini-2.0-flash-lite","gemini-1.5-flash","gemini-1.5-pro",]@@ -58,7 +59,6 @@ class GeminiConfig:"gemini-2.5-flash","gemini-2.5-flash-lite","gemini-2.0-flash",- "gemini-2.0-flash-lite","gemini-1.5-flash",]@@ -140,10 +140,17 @@ class GeminiAdapter:status_code = getattr(e, "code", None) or getattr(e, "status_code", None)if status_code == 401 or "authentication" in error_message or "api key" in error_message:raise AuthenticationError(str(e))+ if status_code == 403 or "permission denied" in error_message:+ raise ModelPermissionError(request.model)if status_code == 429 or "rate limit" in error_message or "resource exhausted" in error_message:if "quota" in error_message or "billing" in error_message:raise QuotaExceededError(str(e))raise RateLimitError(str(e))+ # Google returns a 404-class error (often with "no longer available") when a+ # model is retired/deprecated. Map it so call_llm_judge disables the eval+ # gracefully instead of burning Temporal retries on an unhandled exception.+ if status_code == 404 or "no longer available" in error_message or "not found" in error_message:+ raise ModelNotFoundError(request.model)raisedef stream(diff --git a/products/ai_observability/backend/llm/providers/test/test_gemini_adapter.py b/products/ai_observability/backend/llm/providers/test/test_gemini_adapter.pyindex 55373d9c6c24..238211d10dca 100644--- a/products/ai_observability/backend/llm/providers/test/test_gemini_adapter.py+++ b/products/ai_observability/backend/llm/providers/test/test_gemini_adapter.py@@ -1,6 +1,63 @@+import pytest+from unittest.mock import MagicMock, patch++from google.genai.errors import ClientError+from parameterized import parameterized++from products.ai_observability.backend.llm.errors import (+ AuthenticationError,+ ModelNotFoundError,+ ModelPermissionError,+ RateLimitError,+)from products.ai_observability.backend.llm.providers.gemini import GeminiAdapter, GeminiConfig+from products.ai_observability.backend.llm.types import AnalyticsContext, CompletionRequestclass TestGeminiRecommendedModels:def test_recommended_models_equals_supported_models(self):assert GeminiAdapter.recommended_models() == set(GeminiConfig.SUPPORTED_MODELS)+++def _make_client_error(code: int, status: str, message: str) -> ClientError:+ return ClientError(code, {"error": {"code": code, "status": status, "message": message}})+++class TestGeminiAdapterErrorMapping:+ @parameterized.expand(+ [+ # Retired/deprecated model: Google returns a 404 or a message saying it's gone.+ ("404_not_found", 404, "NOT_FOUND", "model not found", ModelNotFoundError),+ (+ "deprecated_message",+ 400,+ "INVALID_ARGUMENT",+ "This model is no longer available",+ ModelNotFoundError,+ ),+ ("403_permission", 403, "PERMISSION_DENIED", "permission denied", ModelPermissionError),+ ("401_auth", 401, "UNAUTHENTICATED", "invalid api key", AuthenticationError),+ ("429_rate_limit", 429, "RESOURCE_EXHAUSTED", "rate limit exceeded", RateLimitError),+ ]+ )+ def test_api_error_is_mapped_to_llm_error(+ self,+ _name: str,+ code: int,+ status: str,+ message: str,+ expected: type[Exception],+ ):+ request = CompletionRequest(+ model="gemini-2.0-flash-lite",+ system="s",+ messages=[{"role": "user", "content": "hi"}],+ provider="gemini",+ )+ adapter = GeminiAdapter()+ mock_client = MagicMock()+ mock_client.models.generate_content.side_effect = _make_client_error(code, status, message)++ with patch("products.ai_observability.backend.llm.providers.gemini.genai.Client", return_value=mock_client):+ with pytest.raises(expected):+ adapter.complete(request, api_key="test-key", analytics=AnalyticsContext(capture=False))