Skip to content

Commit ae597a5

Browse files
authored
refactor(genai): remove direct import of google.api_core and use specific exceptions (#1176)
Remove direct import of `google.api_core` and replace it with specific exception.
1 parent ad1f8d6 commit ae597a5

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

libs/genai/langchain_google_genai/chat_models.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
)
2828

2929
import filetype # type: ignore[import-untyped]
30-
import google.api_core
3130
import proto # type: ignore[import-untyped]
3231
from google.ai.generativelanguage_v1beta import (
3332
GenerativeServiceAsyncClient as v1betaGenerativeServiceAsyncClient,
@@ -52,6 +51,13 @@
5251
VideoMetadata,
5352
)
5453
from google.ai.generativelanguage_v1beta.types import Tool as GoogleTool
54+
from google.api_core.exceptions import (
55+
FailedPrecondition,
56+
GoogleAPIError,
57+
InvalidArgument,
58+
ResourceExhausted,
59+
ServiceUnavailable,
60+
)
5561
from langchain_core.callbacks.manager import (
5662
AsyncCallbackManagerForLLMRun,
5763
CallbackManagerForLLMRun,
@@ -167,9 +173,9 @@ def _create_retry_decorator(
167173
max=wait_exponential_max,
168174
),
169175
retry=(
170-
retry_if_exception_type(google.api_core.exceptions.ResourceExhausted)
171-
| retry_if_exception_type(google.api_core.exceptions.ServiceUnavailable)
172-
| retry_if_exception_type(google.api_core.exceptions.GoogleAPIError)
176+
retry_if_exception_type(ResourceExhausted)
177+
| retry_if_exception_type(ServiceUnavailable)
178+
| retry_if_exception_type(GoogleAPIError)
173179
),
174180
before_sleep=before_sleep_log(logger, logging.WARNING),
175181
)
@@ -200,7 +206,7 @@ def _chat_with_retry(generation_method: Callable, **kwargs: Any) -> Any:
200206
def _chat_with_retry(**kwargs: Any) -> Any:
201207
try:
202208
return generation_method(**kwargs)
203-
except google.api_core.exceptions.FailedPrecondition as exc:
209+
except FailedPrecondition as exc:
204210
if "location is not supported" in exc.message:
205211
error_msg = (
206212
"Your location is not supported by google-generativeai "
@@ -209,10 +215,10 @@ def _chat_with_retry(**kwargs: Any) -> Any:
209215
)
210216
raise ValueError(error_msg)
211217

212-
except google.api_core.exceptions.InvalidArgument as e:
218+
except InvalidArgument as e:
213219
msg = f"Invalid argument provided to Gemini: {e}"
214220
raise ChatGoogleGenerativeAIError(msg) from e
215-
except google.api_core.exceptions.ResourceExhausted as e:
221+
except ResourceExhausted as e:
216222
# Handle quota-exceeded error with recommended retry delay
217223
if hasattr(e, "retry_after") and e.retry_after < kwargs.get(
218224
"wait_exponential_max", 60.0
@@ -247,7 +253,6 @@ async def _achat_with_retry(generation_method: Callable, **kwargs: Any) -> Any:
247253
Any: The result from the chat generation method.
248254
"""
249255
retry_decorator = _create_retry_decorator()
250-
from google.api_core.exceptions import InvalidArgument
251256

252257
@retry_decorator
253258
async def _achat_with_retry(**kwargs: Any) -> Any:

0 commit comments

Comments
 (0)