Skip to content

genai: Enhance retry mechanism in ChatGoogleGenerativeAI with customizable parameter #915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions libs/genai/langchain_google_genai/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import logging
import mimetypes
import time
import uuid
import warnings
from difflib import get_close_matches
Expand Down Expand Up @@ -139,7 +140,12 @@ class ChatGoogleGenerativeAIError(GoogleGenerativeAIError):
"""


def _create_retry_decorator() -> Callable[[Any], Any]:
def _create_retry_decorator(
max_retries: int = 6,
wait_exponential_multiplier: float = 2.0,
wait_exponential_min: float = 1.0,
wait_exponential_max: float = 60.0,
) -> Callable[[Any], Any]:
"""
Creates and returns a preconfigured tenacity retry decorator.

Expand All @@ -151,15 +157,14 @@ def _create_retry_decorator() -> Callable[[Any], Any]:
Callable[[Any], Any]: A retry decorator configured for handling specific
Google API exceptions.
"""
multiplier = 2
min_seconds = 1
max_seconds = 60
max_retries = 2

return retry(
reraise=True,
stop=stop_after_attempt(max_retries),
wait=wait_exponential(multiplier=multiplier, min=min_seconds, max=max_seconds),
wait=wait_exponential(
multiplier=wait_exponential_multiplier,
min=wait_exponential_min,
max=wait_exponential_max,
),
retry=(
retry_if_exception_type(google.api_core.exceptions.ResourceExhausted)
| retry_if_exception_type(google.api_core.exceptions.ServiceUnavailable)
Expand All @@ -184,13 +189,17 @@ def _chat_with_retry(generation_method: Callable, **kwargs: Any) -> Any:
Returns:
Any: The result from the chat generation method.
"""
retry_decorator = _create_retry_decorator()
retry_decorator = _create_retry_decorator(
max_retries=kwargs.get("max_retries", 6),
wait_exponential_multiplier=kwargs.get("wait_exponential_multiplier", 2.0),
wait_exponential_min=kwargs.get("wait_exponential_min", 1.0),
wait_exponential_max=kwargs.get("wait_exponential_max", 60.0),
)

@retry_decorator
def _chat_with_retry(**kwargs: Any) -> Any:
try:
return generation_method(**kwargs)
# Do not retry for these errors.
except google.api_core.exceptions.FailedPrecondition as exc:
if "location is not supported" in exc.message:
error_msg = (
Expand All @@ -204,6 +213,13 @@ def _chat_with_retry(**kwargs: Any) -> Any:
raise ChatGoogleGenerativeAIError(
f"Invalid argument provided to Gemini: {e}"
) from e
except google.api_core.exceptions.ResourceExhausted as e:
# Handle quota-exceeded error with recommended retry delay
if hasattr(e, "retry_after") and e.retry_after < kwargs.get(
"wait_exponential_max", 60.0
):
time.sleep(e.retry_after)
raise e
except Exception as e:
raise e

Expand Down
21 changes: 21 additions & 0 deletions libs/genai/tests/unit_tests/test_chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
GenerateContentResponse,
Part,
)
from google.api_core.exceptions import ResourceExhausted
from langchain_core.load import dumps, loads
from langchain_core.messages import (
AIMessage,
Expand All @@ -30,6 +31,7 @@

from langchain_google_genai.chat_models import (
ChatGoogleGenerativeAI,
_chat_with_retry,
_convert_tool_message_to_part,
_parse_chat_history,
_parse_response_candidate,
Expand Down Expand Up @@ -771,3 +773,22 @@ def test_model_kwargs() -> None:
assert llm.model == "models/my-model"
assert llm.convert_system_message_to_human is True
assert llm.model_kwargs == {"foo": "bar"}


def test_retry_decorator_with_custom_parameters() -> None:
# Mock the generation method
mock_generation_method = Mock()
mock_generation_method.side_effect = ResourceExhausted("Quota exceeded")

# Call the function with custom retry parameters
with pytest.raises(ResourceExhausted):
_chat_with_retry(
generation_method=mock_generation_method,
max_retries=3,
wait_exponential_multiplier=1.5,
wait_exponential_min=2.0,
wait_exponential_max=30.0,
)

# Verify that the retry mechanism used the custom parameters
assert mock_generation_method.call_count == 3
Loading