How to handle error 502 Bad Gateway


I am recently getting error 502 Bad Gateway quite often. I am processing a file and batch calling ChatCompletion.create repeatedly. This works most of the time but since a few days, I have been getting the 502 error a lot.

I am looking for suggestion to handle this.
The Error codes page (OpenAI API) does not mention 502 so I thought to ask here what’s the best way to approach this.

ps. I will appreciate if someone can share example Python code to handle this to retry a few times when this error occurs.



Update: I was able to resolve this by using the try/except sample code provided on the error codes page. Now successfully processing batches with 1000+ api calls.


Thanks for coming back to let us know. Hopefully your post helps someone in the future.

Welcome to the forum, by the way!

1 Like

I’m getting error 502 too. What is it and why it is not referenced in the documentation?

This is an ongoing issue for us, too.
Also, since the completion does execute on the server, we are getting billed quite a lot for things that never arrive.
It’s really not great.


Yup, constant intermittent 502s. Any ETA on fixing that? At least don’t bill us for them…


So are there some news about this error ? Same as you guys, I make big requests that cost me a lot then it’s billed but it fails and I cannot get the result of my request …

See my update on April 12. I explained how was able to get past this error.

Can you describe more exactly how you handle those errors ? Are you retrying in case of failure ? If yes, are you billed twice ?

1 Like

So you just send the same request again? And are those 502 requests affect the usage?

1 Like

Same problem here. Constantly retrying, but sometimes getting a lot of 502 errors.

Same here. Really don’t want to get charged for these!

1 Like

One way to mitigate this is to increase the wait time between retries.


I am also getting this a lot!!

As I am processing a lot of data, it is kind of a pain. I leave the computer to let the stuff process for some hours, to find out that it ran into some GatewayError after 10 minutes.

Just had a 502 and found this thread. Seems like people might be looking for code so wanted to share that I found some examples for exponential backoff in the docs under Rate limit increase.

1 Like

I am getting this alot too, sometimes it works fine but most of the time it gives 502 error, did any one find a solution?

Have you implemented the try/except sample code?

What language are you using?

Yess i have implemented try/except but unable to fix it. I am using Python with FastApi framework.

i’m not sure if they charge us when we get 502 error.
but try/catch loop should loop-check OpenAI Status - API if it’s available so at least we are sure we are not wasting our tokens until it’s fixed

502 almost certainly means the request was dropped before it got anywhere near being handled by OpenAI, so they’ll have no record of it and thus no way to charge you. Also not much we can do but sit and wait… you’d think scaling up the fastest growing application in history would be nothing but smooth sailing but :person_shrugging: