mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-06 13:18:12 +00:00
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
This commit is contained in:
parent
54dd8e52a8
commit
1113700b09
@ -21,6 +21,8 @@ from typing import (
|
|||||||
)
|
)
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
import google.api_core
|
||||||
|
|
||||||
# TODO: remove ignore once the google package is published with types
|
# TODO: remove ignore once the google package is published with types
|
||||||
import google.generativeai as genai # type: ignore[import]
|
import google.generativeai as genai # type: ignore[import]
|
||||||
import requests
|
import requests
|
||||||
@ -87,8 +89,6 @@ def _create_retry_decorator() -> Callable[[Any], Any]:
|
|||||||
Callable[[Any], Any]: A retry decorator configured for handling specific
|
Callable[[Any], Any]: A retry decorator configured for handling specific
|
||||||
Google API exceptions.
|
Google API exceptions.
|
||||||
"""
|
"""
|
||||||
import google.api_core.exceptions
|
|
||||||
|
|
||||||
multiplier = 2
|
multiplier = 2
|
||||||
min_seconds = 1
|
min_seconds = 1
|
||||||
max_seconds = 60
|
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.
|
Any: The result from the chat generation method.
|
||||||
"""
|
"""
|
||||||
retry_decorator = _create_retry_decorator()
|
retry_decorator = _create_retry_decorator()
|
||||||
from google.api_core.exceptions import InvalidArgument # type: ignore
|
|
||||||
|
|
||||||
@retry_decorator
|
@retry_decorator
|
||||||
def _chat_with_retry(**kwargs: Any) -> Any:
|
def _chat_with_retry(**kwargs: Any) -> Any:
|
||||||
try:
|
try:
|
||||||
return generation_method(**kwargs)
|
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(
|
raise ChatGoogleGenerativeAIError(
|
||||||
f"Invalid argument provided to Gemini: {e}"
|
f"Invalid argument provided to Gemini: {e}"
|
||||||
) from e
|
) from e
|
||||||
|
@ -56,11 +56,19 @@ def _completion_with_retry(
|
|||||||
prompt: LanguageModelInput, is_gemini: bool, stream: bool, **kwargs: Any
|
prompt: LanguageModelInput, is_gemini: bool, stream: bool, **kwargs: Any
|
||||||
) -> Any:
|
) -> Any:
|
||||||
generation_config = kwargs.get("generation_config", {})
|
generation_config = kwargs.get("generation_config", {})
|
||||||
if is_gemini:
|
error_msg = (
|
||||||
return llm.client.generate_content(
|
"Your location is not supported by google-generativeai at the moment. "
|
||||||
contents=prompt, stream=stream, generation_config=generation_config
|
"Try to use VertexAI LLM from langchain_google_vertexai"
|
||||||
)
|
)
|
||||||
return llm.client.generate_text(prompt=prompt, **kwargs)
|
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(
|
return _completion_with_retry(
|
||||||
prompt=prompt, is_gemini=is_gemini, stream=stream, **kwargs
|
prompt=prompt, is_gemini=is_gemini, stream=stream, **kwargs
|
||||||
|
Loading…
Reference in New Issue
Block a user