The Gemini 2.5 Flash Image Preview API returns no response or internal server error randomly

I have observed that the Gemini 2.5 Flash Image Preview API randomly produces internal server errors and also responses with no image data. I was handling this using a retry mechanism wherein I retry a request if Internal Server Error occurs or there is no data in the response. I retry 5 times before showing an error. This has reduced the number of errors happening to very less but still a few times I have seen the API fail requests even after 5 retries. Is there a fix for this? I am currently using the gemini API through Google AI studio. Should I be using the API through Vertex AI while running in production to avoid such errors?

This is kind of super frustating. These are very random errors and tonight while I was running the API, the api has been taking forever to produce responses. My server request times out because of the delay. Please help here. I have a product launch soon and I need to figure out how to implement the Gemini 2.5 Flash Image Preview API implementation in a proper manner so that the errors are negligible or impossible.

1 Like

Hi @Lakshya_Gupta Apologies for late response
I tried running the code provided in the docs https://ai.google.dev/gemini-api/docs/image-generation#image_generation_text-to-image, but it is working as expected on my end.
To help me reproduce the error, could you share a snippet of your code?

It would also be helpful if you could try the gemini-3-pro-image-preview model to see if the issue occurs there too.
Thanks