Skip to content

Commit 8bebccf

Browse files
nushibBesmira Nushi
andauthored
gemini models bug fix - error handling (#79)
- adds more detail in the warning message when the gemini model response has no output in parts but may still have candidate answers generated - fixes the logic around retrying calls for EndpointModels --------- Co-authored-by: Besmira Nushi <[email protected]>
1 parent 3a0835e commit 8bebccf

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

eureka_ml_insights/models/models.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ def generate(self, query_text, *args, **kwargs):
123123
logging.warning(f"Attempt {attempts+1}/{self.num_retries} failed: {e}")
124124
do_return = self.handle_request_error(e)
125125
if do_return:
126-
self.model_output = self.response
127-
self.is_valid = self.is_valid
126+
self.model_output = None
127+
self.is_valid = False
128128
break
129129
attempts += 1
130130
else:
@@ -650,7 +650,7 @@ def handle_request_error(self, e):
650650
e (_type_): Exception occurred during getting a response.
651651
652652
returns:
653-
_type_: response, is_valid, do_return (False if the call should not be attempted again).
653+
_type_: do_return (True if the call should not be attempted again).
654654
"""
655655
# Handling cases where the model explicitly blocks prompts and provides a reason for it.
656656
# In these cases, there is no need to make a new attempt as the model will continue to explicitly block the request, do_return = True.
@@ -659,10 +659,17 @@ def handle_request_error(self, e):
659659
f"Attempt failed due to explicitly blocked input prompt: {e} Block Reason {self.gemini_response.prompt_feedback.block_reason}"
660660
)
661661
return True
662-
# Handling cases where the model implicitly blocks prompts and does not provide a reason for it but rather an empty content.
662+
# Handling cases where the model implicitly blocks prompts and does not provide an explicit block reason for it but rather an empty content.
663663
# In these cases, there is no need to make a new attempt as the model will continue to implicitly block the request, do_return = True.
664+
# Note that, in some cases, the model may still provide a finish reason as shown here https://ai.google.dev/api/generate-content?authuser=2#FinishReason
664665
elif e.__class__.__name__ == "IndexError" and len(self.gemini_response.parts) == 0:
665666
logging.warning(f"Attempt failed due to implicitly blocked input prompt and empty model output: {e}")
667+
# For cases where there are some response candidates do_return is still True because in most cases these candidates are incomplete.
668+
# Trying again may not necessarily help, unless in high temperature regimes.
669+
if len(self.gemini_response.candidates) > 0:
670+
logging.warning(f"The response is not empty and has : {len(self.gemini_response.candidates)} candidates")
671+
logging.warning(f"Finish Reason for the first answer candidate is: {self.gemini_response.candidates[0].finish_reason}")
672+
logging.warning(f"Safety Ratings for the first answer candidate are: {self.gemini_response.candidates[0].safety_ratings}")
666673
return True
667674
# Any other case will be re attempted again, do_return = False.
668675
return False

0 commit comments

Comments
 (0)