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):
        try:
            response = self.call_openai_api(
                model="gpt-3.5-turbo",
                messages=[{"role": "user", "content": content}],
                max_tokens=2500,
                n=1,
                stop=None,
                temperature=0.0,
            )
            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}")
            break

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

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

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

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

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

    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