|
21 | 21 | ) |
22 | 22 | from urllib.parse import urlparse |
23 | 23 |
|
| 24 | +import google.api_core |
| 25 | + |
24 | 26 | # TODO: remove ignore once the google package is published with types |
25 | 27 | import google.generativeai as genai # type: ignore[import] |
26 | 28 | import requests |
@@ -87,8 +89,6 @@ def _create_retry_decorator() -> Callable[[Any], Any]: |
87 | 89 | Callable[[Any], Any]: A retry decorator configured for handling specific |
88 | 90 | Google API exceptions. |
89 | 91 | """ |
90 | | - import google.api_core.exceptions |
91 | | - |
92 | 92 | multiplier = 2 |
93 | 93 | min_seconds = 1 |
94 | 94 | max_seconds = 60 |
@@ -123,14 +123,22 @@ def _chat_with_retry(generation_method: Callable, **kwargs: Any) -> Any: |
123 | 123 | Any: The result from the chat generation method. |
124 | 124 | """ |
125 | 125 | retry_decorator = _create_retry_decorator() |
126 | | - from google.api_core.exceptions import InvalidArgument # type: ignore |
127 | 126 |
|
128 | 127 | @retry_decorator |
129 | 128 | def _chat_with_retry(**kwargs: Any) -> Any: |
130 | 129 | try: |
131 | 130 | 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: |
134 | 142 | raise ChatGoogleGenerativeAIError( |
135 | 143 | f"Invalid argument provided to Gemini: {e}" |
136 | 144 | ) from e |
|
0 commit comments