How to Implement Retry Logic in the New Python SDK?

Hi everyone,
I’m currently updating my existing code to work with the new Gemini Python SDK, which has gone through a structural change. Previously, I was using the built-in Retry logic provided within the module and simply passed it as an option. However, I couldn’t find a clear way to do this with the new SDK.

Could anyone share how to implement retry logic with the updated SDK?

Thanks in advance!

1 Like

You should be able to manually implementing backoff and retry mechanisms with error handling, similar to the previous approach. Let me know if you encounter any errors.

from google.api_core import retry, exceptions

@retry.Retry(
    predicate=retry.if_transient_error,
    initial=1.0,
    maximum=60.0,
    multiplier=2.0,
    timeout=120.0,
)
def summarize_content(description,system_instruction):
    response = client.models.generate_content(
        model="gemini-2.0-flash",
        config=types.GenerateContentConfig(
            system_instruction=system_instruction
        ),
        contents=description
    )