Skip to content

Commit

Permalink
fix(examples): handle usage chunk in tool call streaming (#1068)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Jacob Zimmerman <jacob@stainlessapi.com>
  • Loading branch information
2 people authored and stainless-app[bot] committed Sep 13, 2024
1 parent 58eb50c commit 90c2da5
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion examples/tool-calls-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,13 @@ function messageReducer(previous: ChatCompletionMessage, item: ChatCompletionChu
}
return acc;
};
return reduce(previous, item.choices[0]!.delta) as ChatCompletionMessage;

const choice = item.choices[0];
if (!choice) {
// chunk contains information about usage and token counts
return previous;
}
return reduce(previous, choice.delta) as ChatCompletionMessage;
}

function lineRewriter() {
Expand Down

0 comments on commit 90c2da5

Please sign in to comment.