From 1113700b093b9f2c54d26bc5d357b0dc2f3ab11f Mon Sep 17 00:00:00 2001 From: Leonid Kuligin Date: Wed, 24 Jan 2024 22:58:46 +0100 Subject: [PATCH] google-genai[patch]: better error message when location is not supported (#16535) Replace this entire comment with: - **Description:** a better error message when location is not supported --- .../langchain_google_genai/chat_models.py | 18 +++++++++++++----- .../langchain_google_genai/llms.py | 18 +++++++++++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/libs/partners/google-genai/langchain_google_genai/chat_models.py b/libs/partners/google-genai/langchain_google_genai/chat_models.py index 62cb707cdca..5f38693ce5a 100644 --- a/libs/partners/google-genai/langchain_google_genai/chat_models.py +++ b/libs/partners/google-genai/langchain_google_genai/chat_models.py @@ -21,6 +21,8 @@ from typing import ( ) from urllib.parse import urlparse +import google.api_core + # TODO: remove ignore once the google package is published with types import google.generativeai as genai # type: ignore[import] import requests @@ -87,8 +89,6 @@ def _create_retry_decorator() -> Callable[[Any], Any]: Callable[[Any], Any]: A retry decorator configured for handling specific Google API exceptions. """ - import google.api_core.exceptions - multiplier = 2 min_seconds = 1 max_seconds = 60 @@ -123,14 +123,22 @@ def _chat_with_retry(generation_method: Callable, **kwargs: Any) -> Any: Any: The result from the chat generation method. """ retry_decorator = _create_retry_decorator() - from google.api_core.exceptions import InvalidArgument # type: ignore @retry_decorator def _chat_with_retry(**kwargs: Any) -> Any: try: return generation_method(**kwargs) - except InvalidArgument as e: - # Do not retry for these errors. + # Do not retry for these errors. + except google.api_core.exceptions.FailedPrecondition as exc: + if "location is not supported" in exc.message: + error_msg = ( + "Your location is not supported by google-generativeai " + "at the moment. Try to use ChatVertexAI LLM from " + "langchain_google_vertexai." + ) + raise ValueError(error_msg) + + except google.api_core.exceptions.InvalidArgument as e: raise ChatGoogleGenerativeAIError( f"Invalid argument provided to Gemini: {e}" ) from e diff --git a/libs/partners/google-genai/langchain_google_genai/llms.py b/libs/partners/google-genai/langchain_google_genai/llms.py index ec9ec4d67f0..751ca8afebe 100644 --- a/libs/partners/google-genai/langchain_google_genai/llms.py +++ b/libs/partners/google-genai/langchain_google_genai/llms.py @@ -56,11 +56,19 @@ def _completion_with_retry( prompt: LanguageModelInput, is_gemini: bool, stream: bool, **kwargs: Any ) -> Any: generation_config = kwargs.get("generation_config", {}) - if is_gemini: - return llm.client.generate_content( - contents=prompt, stream=stream, generation_config=generation_config - ) - return llm.client.generate_text(prompt=prompt, **kwargs) + error_msg = ( + "Your location is not supported by google-generativeai at the moment. " + "Try to use VertexAI LLM from langchain_google_vertexai" + ) + try: + if is_gemini: + return llm.client.generate_content( + contents=prompt, stream=stream, generation_config=generation_config + ) + return llm.client.generate_text(prompt=prompt, **kwargs) + except google.api_core.exceptions.FailedPrecondition as exc: + if "location is not supported" in exc.message: + raise ValueError(error_msg) return _completion_with_retry( prompt=prompt, is_gemini=is_gemini, stream=stream, **kwargs