Handling Errors in Streaming Responses

Handling Errors in Streaming Responses

Jul 14, 2024 · 1 min read · 148 Words · -Views -Comments

AI APIs often return server-sent streams. When the server reports an error, the payload still arrives as a stream, so error handling differs from typical JSON responses.

Example

Using Axios as an example:

axios.post('http://api.com/v1/chat/completions', data, {
  headers: {
    Authorization: `Bearer xxxx`,
    'Content-Type': 'application/json',
  },
  responseType: 'stream',
})
  .then(res => res.data)
  .catch(async (e) => {
    let error;
    if (e.isAxiosError) {
      let streamString = '';
      await new Promise(resolve => {
        e.response.data
          .on('data', (utf8Chunk) => {
            streamString += utf8Chunk;
          })
          .on('end', () => {
            error = JSON.parse(streamString);
            resolve();
          });
      });
    } else {
      error = e;
    }
    throw error;
  });

In streaming mode you must consume the error payload through the same evented interface as normal data. Wrapping the listener with a Promise ensures you capture the full string before parsing and rethrowing.

Final Thoughts

When dealing with streaming APIs, remember that error paths are still streams—structure your error handling accordingly.

Alan H
Authors
Developer, Tech Enthusiast, Open Source Advocate