Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Handle a Stream That Breaks Midway

In this video, you watched the partner script crash when a stream failed partway through Amara’s reply, and you changed the script so it keeps the words she’d already read and marks a reply that never finished as broken.

Key Points

  • A failure can arrive after the first words. The status was 200 before the first word was written, so a failure during generation arrives later, as an error event in the middle of the stream.
  • The failing runs used a stand-in server. You can’t make a provider fail on cue, so this video’s failing runs use a small local stand-in server. It passes on the real reply’s first words, then sends an error event. It’s only for the recording, so you don’t need to build it.
  • The error comes from inside the loop. With the script as the last lesson left it, Amara saw the first few words of the reply, and then the script crashed with Error: Stream error: Server error, thrown by your provider file when it read the error event. The stack’s last line pointed to line 17 of partner.js, the for await loop, not to the streamAnswer() call above it. Nothing after the loop ran, so Amara was left with half a sentence. Your run’s words will differ.
  • streamAnswer() doesn’t retry it. An error event from the provider is never retried, even before any text. And once the first words have gone out, even a retryable failure is thrown on to your loop. That second rule is there because a retry would ask the model for a whole new reply, which would appear after the broken words. Words the member has already read can’t be taken back, so handling the error is the script’s job.
  • Put the whole loop inside the try. A try around the streamAnswer() call alone catches nothing here, because the request starts, and fails, inside the loop.
  • Keep what was shown. The catch block prints the error’s message. The words Amara saw stay on screen, and reply keeps them on purpose.
  • A null stop reason means the reply is broken. Only the closing event sets answer.stopReason, so after the try, the script sets broken to answer.stopReason === null, and a marker tells Amara that the reply broke off.
  • cutOff can’t tell you how a broken reply ended. The stop reason and usage stay null, because the closing event never came. So the script can’t count this call’s tokens, though the provider still processed them. cutOff stays false only because that’s its starting value.
  • A cut-off reply ends with no error. At the output limit, the closing event does come, so the stop reason is set and cutOff turns true, the same flag ask() sets. One if statement after the loop adds a second marker.

Remember that a stream can break after the member has read part of it. Put the whole loop inside the try, and in the catch, keep what was shown. After it, a null stop reason means broken, and cutOff means cut off.

The Code

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your partner script prints Amara’s reply from streamAnswer() as the words arrive. In this video, you’ll see what happens when the provider fails partway through that reply, and handle it in the script.

00:12 By then, the request has already succeeded. The status was 200 before the first word was written. So a failure during generation arrives later, as an error event in the middle of the stream.

00:25 You can’t make a provider fail on cue, so this run uses a small local stand-in server. It passes on the real reply’s first words, then sends an error event.

00:35 It’s only for this recording.

00:38 Here’s the script’s loop, as the last lesson left it. Each piece is written to the terminal as it arrives. After the loop, four lines print the results. What does Amara see when the stream fails after the first few words?

00:52 Your turn: pause the video and predict.

01:05 Amara sees the first few words of the reply. Then the script crashes.

01:11 Your run’s words will differ.

01:14 The error is the stand-in’s server error, which your provider file threw when it read the error event. The stack’s last line points to line 17 of the script, the for await loop.

01:25 So the error doesn’t come from the streamAnswer() call above the loop. It comes from inside the loop, while the loop reads the stream.

01:34 Nothing after the loop runs. The four result lines never print, and Amara is left with half a sentence.

01:42 streamAnswer() retries some failures, so why not this one? An error event from the provider is never retried, even before any text. And here, the first words had already gone out, so even a retryable failure would be thrown on to your loop.

01:59 The second rule is there because a retry would ask the model for a whole new reply. Here’s what Amara would see then: the broken words, and then a different reply after them.

02:10 Words she’s already read can’t be taken back. So the error goes to your script, and handling it is the script’s job.

02:20 So the whole loop goes inside a try block. Watch out: a try around the streamAnswer() call alone catches nothing here, because the request starts, and fails, inside the loop.

02:33 The catch block prints the error’s message. The words Amara saw stay on screen, and reply keeps them, on purpose. After the try, the script checks the stop reason.

02:45 Only the closing event sets it, so if it’s still null, the reply is broken, and a marker says so. Run it against the stand-in again. The first words print, then the error and the marker, and the script finishes.

03:00 The stop reason and usage are null, because the closing event never came. Cut off is false only because that’s its starting value, so it can’t tell you how the reply ended.

03:10 The null stop reason can.

03:14 A reply can also stop at the output limit. Then the closing event does come, so the stop reason is set, and cutOff turns true. So one if statement after the loop adds a second marker.

03:27 In this run, with the limit lowered to 90 tokens, the reply stopped mid-sentence. The marker says so, and Broken is false.

03:38 So a stream can break after the member has read part of it. Put the whole loop inside the try, and in the catch, keep what was shown. After it, a null stop reason means broken, and cutOff means cut off.

03:52 Next comes Exercise: Stream a Reply to the Terminal, the course’s first coding exercise. You’ll print a club member’s reply as it arrives, and mark one that doesn’t finish.

04:03 It runs in your browser on a stand-in model, so it needs no key.

Become a Member to join the conversation.