Authentication error: <empty message>

I am encountering a very strange behaviour using the API. It used to work perfectly fine but since a few days the API returns an empty message. I included a rstrip to remove any potential white spaces or \n at the end of the prompt.

Please help!!
This is the part of the script:
Remove any trailing whitespace or newline characters, this sometimes produces error for openai api

    content = content.rstrip()

    retry_limit = 5
    response = None

    for retry_count in range(retry_limit):
            response = self.call_openai_api(
                messages=[{"role": "user", "content": content}],
            break  # If the call was successful, break the loop

        except RateLimitError as e:
            print(f"OpenAI API request exceeded rate limit: {e}. Attempt {retry_count + 1} of {retry_limit}")
            time.sleep(10)  # sleep for a while before retrying

        except APIError as e:
            print(f"OpenAI API returned an API Error: {e}")

        except APIConnectionError as e:
            print(f"Failed to connect to OpenAI API: {e}")

        except InvalidRequestError as e:
            print(f"Invalid request: {e}")

        except AuthenticationError as e:
            print(f"Authentication error: {e}")

        except ServiceUnavailableError as e:
            print(f"Service is currently unavailable: {e}")

        except Exception as e:
            print(f"An unexpected error occurred: {e}")

    else:  # This part will execute if the for loop completed all iterations without a break statement.
        print("Retry limit exceeded.") 

And the response: Authentication error:
Using playground works perfectly fine. My API key is correct and working (when using a simple test prompt).

Welcome to the OpenAI community @cp1

Do you get this issue when using the openai python library?

Thank you @sps :slight_smile:

Yes, I am using the openai lib, next to the error handling packages:

import openai
from openai.error import APIError, APIConnectionError, RateLimitError, InvalidRequestError, AuthenticationError, ServiceUnavailableError

Interesting. I’m curious why aren’t you using the boilerplate code for calling the chat completion api?

I’m asking because the definition of call_openai_api() isn’t included in the code you shared, and the function looks just what the openai.chatCompletion.create() should be.

I defined prior a method to handle different parts of the API, but the call is basically the same as the standard call.

@retry(retry_on_exception=lambda e: isinstance(e, RateLimitError), stop_max_attempt_number=10, wait_exponential_multiplier=1000, wait_exponential_max=10000)
def call_openai_api(*args, **kwargs):
    return openai.ChatCompletion.create(*args, **kwargs)

Please use the boilerplate code with a newly generated api key. Let me know if your still run into the error.

That seemed to solve the issue - not sure exactly why though. The API key was not part of the issue.
Thanks for the help!!

1 Like