Spaces:
Runtime error
Runtime error
update retry logic to retry after retr_delay sepcified during error:429
Browse files
app.py
CHANGED
|
@@ -66,7 +66,7 @@ class BasicAgent:
|
|
| 66 |
return isinstance(e, genai.errors.APIError) and getattr(e, "code", None) in {429, 503}
|
| 67 |
|
| 68 |
def invoke_with_retry(self,question: str, max_retries: int = 5, initial_delay: float = 10.0) -> str:
|
| 69 |
-
|
| 70 |
for attempt in range(max_retries):
|
| 71 |
try:
|
| 72 |
result = self.agent_obj.invoke(
|
|
@@ -76,12 +76,22 @@ class BasicAgent:
|
|
| 76 |
return result['output']
|
| 77 |
except Exception as e:
|
| 78 |
if self.is_retriable(e):
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
else:
|
|
|
|
| 83 |
raise
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
def __call__(self, question: str) -> str:
|
| 87 |
"""
|
|
|
|
| 66 |
return isinstance(e, genai.errors.APIError) and getattr(e, "code", None) in {429, 503}
|
| 67 |
|
| 68 |
def invoke_with_retry(self,question: str, max_retries: int = 5, initial_delay: float = 10.0) -> str:
|
| 69 |
+
current_delay = initial_delay
|
| 70 |
for attempt in range(max_retries):
|
| 71 |
try:
|
| 72 |
result = self.agent_obj.invoke(
|
|
|
|
| 76 |
return result['output']
|
| 77 |
except Exception as e:
|
| 78 |
if self.is_retriable(e):
|
| 79 |
+
# Check if the error object provides a specific retry_delay
|
| 80 |
+
if hasattr(e, 'retry_delay') and hasattr(e.retry_delay, 'seconds'):
|
| 81 |
+
# Use the specific retry_delay provided by the API
|
| 82 |
+
current_delay = float(e.retry_delay.seconds)
|
| 83 |
+
print(f"Quota error (attempt {attempt+1}/{max_retries}). API suggested retry after {current_delay} seconds.", flush=True)
|
| 84 |
+
else:
|
| 85 |
+
# Fallback to exponential backoff if no specific delay is provided
|
| 86 |
+
print(f"Quota error (attempt {attempt+1}/{max_retries}). Retrying in {current_delay} seconds with exponential backoff.", flush=True)
|
| 87 |
+
current_delay *= 2 # Exponential backoff
|
| 88 |
+
|
| 89 |
+
time.sleep(current_delay)
|
| 90 |
else:
|
| 91 |
+
# If it's not a retriable error, re-raise it
|
| 92 |
raise
|
| 93 |
+
# If all retries fail, raise a RuntimeError
|
| 94 |
+
raise RuntimeError(f"Max retries ({max_retries}) exceeded due to persistent quota errors or other retriable issues.")
|
| 95 |
|
| 96 |
def __call__(self, question: str) -> str:
|
| 97 |
"""
|