How can I handle OpenAI API errors when I'm using the streaming mode?

G Cocci 211 Reputation points Microsoft Employee
2023-07-13T09:14:48.9933333+00:00

Hi,

I am writing a Python application that uses the OpenAI API to create a chat.

I am having problems with error handling when using streaming mode, in that although I have entered a number of excepts, none of them intercept the error.

Here an example of the code that I'm using:

try:
    response = openai.ChatCompletion.create(
                model="<model-name>",
                messages = messages,
                stream=true
    )
except openai.error.APIError as e:
    print(f"OpenAI API returned an API Error: {e}")
    pass
except openai.error.APIConnectionError as e:
    print(f"Failed to connect to OpenAI API: {e}")
    pass
except openai.error.RateLimitError as e:
    print(f"OpenAI API request exceeded rate limit: {e}")
    pass
except openai.error.Timeout as e:
    print(f"OpenAI API request timed out: {e}")
    pass
except openai.error.InvalidRequestError as e:
    print(f"Invalid request to OpenAI API: {e}")
    pass
except openai.error.AuthenticationError as e:
    print(f"Authentication error with OpenAI API: {e}")
    pass
except openai.error.ServiceUnavailableError as e:
    print(f"OpenAI API service unavailable: {e}")
    pass

The error I am getting is as follows:

"openai.error.APIError: The server had an error while processing your request. Sorry about that! (Error occurred while streaming.)"

Can anyone help me?

Azure OpenAI Service
Azure OpenAI Service
An Azure service that provides access to OpenAI’s GPT-3 models with enterprise capabilities.
1,841 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ramr-msft 17,441 Reputation points
    2023-07-19T08:38:23.0266667+00:00
    0 comments No comments