Error handling for chat completion attempts

Hello,

I would really enjoy some clarification in terms of how to error handle a chat.completion.create() wherein the return object is an async iterable of chunks as I am doing response streaming. Given the .create() with streaming enabled returns chunks and not a promise, the standard .catch(error) used in the error handling section of the documentation is unhelpful. Any guidance would be very much appreciated.

Hi!

Are you talking about node or python?

If node, my standard answer would be to use axios instead :sweat_smile:

I’m using Node! How would Axios facilitate the error handling? I am making stream requests via the standard OpenAI.chat.completion.create( … stream = true) interface.

well, you get your typical errors and you just handle them. in this case, we just error the stream subject. If working with chat, it’s typically not worth continuing so you need to retry the whole thing anyways in most cases.

response.data.on('error', (error: Error) => {
    resultSubject.error(error);
    cancelTokenSource.cancel("stream error");
    response.data.destroy();
    return;
});