# attach it to your call
except (RateLimitError, ConnectionError, Timeout, RemoteDisconnected, json.decoder.JSONDecodeError, APIError) as e:
handle_errors(e)
# then handle the errors
def handle_errors(e):
if isinstance(e, RateLimitError):
# Handle RateLimitError
print("Rate limit exceeded, retrying in 2 minutes...")
time.sleep(120)
elif isinstance(e, ConnectionError):
# Handle ConnectionError
print("API Connection Error, retrying in 10 minutes...")
time.sleep(600)
elif isinstance(e, Timeout):
# Handle Timeout
print("Timeout Error, retrying in 5 minutes...")
time.sleep(300)
elif isinstance(e, RemoteDisconnected):
# Handle RemoteDisconnected
print("RemoteDisconnected Error, retrying in 5 minutes...")
time.sleep(300)
elif isinstance(e, json.decoder.JSONDecodeError):
# Handle JSONDecodeError
print("JSON Decode Error, retrying in 2 minutes...")
time.sleep(120)
elif isinstance(e, APIError):
# Handle APIError
if "gateway" in str(e):
print("Bad gateway error, waiting for 5 minutes before retrying...")
time.sleep(300)
elif "overloaded" in str(e):
print("Model is overloaded with requests, waiting for 10 minutes before retrying...")
time.sleep(600)
else:
print(f"General API error: {e}, retrying in 10 minutes...")
time.sleep(600)