Handling Errors in Streaming Responses

· 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.

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover