Standard retry logic for Gemini Python SDK?

Does anyone know of a repeatable, official, retry logic for the Gemini API? I see 100s of code “cookbook” examples, scant return codes listed in the official API docs, but no robust examples of how to handle the various codes that are returned (429,503, etc.).

This seems to work for 429 responses, but not for 503 responses:

from google.generativeai.types import RequestOptions
from google.api_core import retry

...

response = model.generate_content(user_message,
                                      request_options=RequestOptions(
                                        retry=retry.Retry(
                                            initial=10, 
                                            multiplier=2, 
                                            maximum=60, 
                                            timeout=300
                                        )
                                       )
                                    )
1 Like

Hey @hie,

Use below retry code :

import google.generativeai as genai
from google.api_core import retry

model = genai.GenerativeModel('gemini-2.0-flash')

# For convenience, a simple wrapper to let the SDK handle error retries
def generate_with_retry(model, prompt):
  return model.generate_content(prompt, request_options={'retry':retry.Retry()})

I think the default limit is 5min, but it’s configurable.

1 Like