Skip to content

Commit 1113700

Browse files
authored
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
1 parent 54dd8e5 commit 1113700

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

libs/partners/google-genai/langchain_google_genai/chat_models.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
)
2222
from urllib.parse import urlparse
2323

24+
import google.api_core
25+
2426
# TODO: remove ignore once the google package is published with types
2527
import google.generativeai as genai # type: ignore[import]
2628
import requests
@@ -87,8 +89,6 @@ def _create_retry_decorator() -> Callable[[Any], Any]:
8789
Callable[[Any], Any]: A retry decorator configured for handling specific
8890
Google API exceptions.
8991
"""
90-
import google.api_core.exceptions
91-
9292
multiplier = 2
9393
min_seconds = 1
9494
max_seconds = 60
@@ -123,14 +123,22 @@ def _chat_with_retry(generation_method: Callable, **kwargs: Any) -> Any:
123123
Any: The result from the chat generation method.
124124
"""
125125
retry_decorator = _create_retry_decorator()
126-
from google.api_core.exceptions import InvalidArgument # type: ignore
127126

128127
@retry_decorator
129128
def _chat_with_retry(**kwargs: Any) -> Any:
130129
try:
131130
return generation_method(**kwargs)
132-
except InvalidArgument as e:
133-
# Do not retry for these errors.
131+
# Do not retry for these errors.
132+
except google.api_core.exceptions.FailedPrecondition as exc:
133+
if "location is not supported" in exc.message:
134+
error_msg = (
135+
"Your location is not supported by google-generativeai "
136+
"at the moment. Try to use ChatVertexAI LLM from "
137+
"langchain_google_vertexai."
138+
)
139+
raise ValueError(error_msg)
140+
141+
except google.api_core.exceptions.InvalidArgument as e:
134142
raise ChatGoogleGenerativeAIError(
135143
f"Invalid argument provided to Gemini: {e}"
136144
) from e

libs/partners/google-genai/langchain_google_genai/llms.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,19 @@ def _completion_with_retry(
5656
prompt: LanguageModelInput, is_gemini: bool, stream: bool, **kwargs: Any
5757
) -> Any:
5858
generation_config = kwargs.get("generation_config", {})
59-
if is_gemini:
60-
return llm.client.generate_content(
61-
contents=prompt, stream=stream, generation_config=generation_config
62-
)
63-
return llm.client.generate_text(prompt=prompt, **kwargs)
59+
error_msg = (
60+
"Your location is not supported by google-generativeai at the moment. "
61+
"Try to use VertexAI LLM from langchain_google_vertexai"
62+
)
63+
try:
64+
if is_gemini:
65+
return llm.client.generate_content(
66+
contents=prompt, stream=stream, generation_config=generation_config
67+
)
68+
return llm.client.generate_text(prompt=prompt, **kwargs)
69+
except google.api_core.exceptions.FailedPrecondition as exc:
70+
if "location is not supported" in exc.message:
71+
raise ValueError(error_msg)
6472

6573
return _completion_with_retry(
6674
prompt=prompt, is_gemini=is_gemini, stream=stream, **kwargs

0 commit comments

Comments
 (0)