Skip to content

Commit 86d03b2

Browse files
feat: more robust gemini retry logic
1 parent f5dd74a commit 86d03b2

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

balrog/client.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -306,24 +306,30 @@ def extract_completion(self, response):
306306
307307
Returns:
308308
str: The extracted completion text.
309+
310+
Raises:
311+
Exception: If response is None or missing expected fields.
309312
"""
310313
if not response:
311-
logger.error("Response is None, cannot extract completion.")
312-
return ""
314+
raise Exception("Response is None, cannot extract completion.")
313315

314316
candidates = getattr(response, "candidates", [])
315317
if not candidates:
316-
logger.error("No candidates found in the response.")
317-
return ""
318+
raise Exception("No candidates found in the response.")
318319

319320
candidate = candidates[0]
320321
content = getattr(candidate, "content", None)
322+
if not content:
323+
raise Exception("No content found in the candidate.")
324+
321325
content_parts = getattr(content, "parts", [])
322326
if not content_parts:
323-
logger.error("No content parts found in the candidate.")
324-
return ""
327+
raise Exception("No content parts found in the candidate.")
325328

326-
text = getattr(content_parts[0], "text", "")
329+
text = getattr(content_parts[0], "text", None)
330+
if text is None:
331+
raise Exception("No text found in the content parts.")
332+
327333
return text.strip()
328334

329335
def generate(self, messages):
@@ -346,8 +352,11 @@ def api_call():
346352
)
347353

348354
response = self.execute_with_retries(api_call)
349-
350-
completion = self.extract_completion(response)
355+
356+
def extract_completion_call():
357+
return self.extract_completion(response)
358+
359+
completion = self.execute_with_retries(extract_completion_call)
351360

352361
return LLMResponse(
353362
model_id=self.model_id,

0 commit comments

Comments
 (0)