Skip to content

Commit 90c2da5

Browse files
RobertCraigiejacobzim-stl
authored andcommitted
fix(examples): handle usage chunk in tool call streaming (#1068)
--------- Co-authored-by: Jacob Zimmerman <[email protected]>
1 parent 58eb50c commit 90c2da5

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

examples/tool-calls-stream.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,13 @@ function messageReducer(previous: ChatCompletionMessage, item: ChatCompletionChu
184184
}
185185
return acc;
186186
};
187-
return reduce(previous, item.choices[0]!.delta) as ChatCompletionMessage;
187+
188+
const choice = item.choices[0];
189+
if (!choice) {
190+
// chunk contains information about usage and token counts
191+
return previous;
192+
}
193+
return reduce(previous, choice.delta) as ChatCompletionMessage;
188194
}
189195

190196
function lineRewriter() {

0 commit comments

Comments
 (0)