diff --git a/examples/tool-calls-stream.ts b/examples/tool-calls-stream.ts index 924e6b7cf..687ea86fd 100755 --- a/examples/tool-calls-stream.ts +++ b/examples/tool-calls-stream.ts @@ -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() {