@G Cocci Thanks for the question, Here is the sample streaming with the endpoints.
How can I handle OpenAI API errors when I'm using the streaming mode?
G Cocci
216
Reputation points Microsoft Employee
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?