From e4188c4ba443a21d1ef94658df5366f80f0e573b Mon Sep 17 00:00:00 2001 From: Robert Craigie Date: Fri, 13 Sep 2024 16:15:28 +0100 Subject: [PATCH] fix(examples): handle usage chunk in tool call streaming (#1068) --------- Co-authored-by: Jacob Zimmerman --- examples/tool-calls-stream.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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() {