'502 Bad Gateway Error' when looping chat completions

I’m using a chat completion function that receives a list of items, and I need the function to run once for each item in the list. The list I need it to iterate through is 12 items long.

Here is the code:

class RoundupGenerator(BaseGenerator):
    def generate_roundup(self):
        completions = []
        for type in self.type_notes:
            try:     
                if all(messages) == True:
                    completion = openai.ChatCompletion.create(
                        model="gpt-4",
                            messages=[{"role": "system", "content": f''' Briefly introduce yourself  '''}]
                    )
                    completions.append(completion.choices[0].message.content)
            except openai.error.APIError as err:
                completions.append('API Error')
            except openai.error.APIConnectionError as err:
                completions.append('API Connection Error')
            except openai.error.RateLimitError as err:
                completions.append('Rate Limit Error')
        return '\n'.join(completions)

When I run this function, it takes 10-20 minutes before returning a ‘502 Bad Gateway’ error.

Debugging and fixes tried:

  1. Introducing a 0.5 second time.sleep delay in the for loop
  2. Instantiating the class/function with only 1 item in the list
  3. Adding the error handling seen at the end of my code as recommended from OpenAI’s documentation here
  4. Removing the for loop and instantiating the class/function with 1 item in the list to ensure the function itself works
  5. Asking it nicely

I found an older forum thread here about this issue, but my step 3 seemed to fix it for them. Any ideas?

Hi,

a 502 error is not from OpenAI, that is a 3rd party server somewhere in the chain falling over. Typically a 502 is the result of edge servers not communicating with hubs correctly.

Now, could this be an edge device hosted by cloudflare failing? Sure, and that could even be part of their offering to OpenAI, but it’s not an OpenAI error.

What I would do is track the system status at every state in minute detail, have log statements before and after every step in your code.

Log the return codes, the current loop iteration, all states, the messages being sent, those that get received, the time taken for each step by including a time stamp, that way you can know where it is in the sequence that this 502 occurs. Is it always in the same place? is it random? does it sometime not happen, etc, etc. You need data, you currently have 1 piece you need lots.

3 Likes

Awesome, I’ll give that a try and see if I can find it, thank you