Skip to content

Commit 78b9aa7

Browse files
committed
fix: initialize streaming tool call arguments to empty string
When the first streaming chunk from an OpenAI-compatible provider includes a tool call with function.name but no arguments field, function.arguments is undefined. Subsequent chunks concatenate to it via +=, producing "undefined{...}" which fails JSON parsing and results in empty fn_args. Initializing to "" on the first chunk prevents this.
1 parent ac17487 commit 78b9aa7

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

src/providers/groq.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ export class GroqProvider extends Provider {
127127

128128
if (!streamedToolCalls[index]) {
129129
streamedToolCalls[index] = toolCall;
130+
if (streamedToolCalls[index].function && streamedToolCalls[index].function.arguments == null) {
131+
streamedToolCalls[index].function.arguments = '';
132+
}
130133
} else {
131134
if (streamedToolCalls[index].function) {
132135
streamedToolCalls[index].function.arguments +=

src/providers/openai.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ export class OpenAIProvider extends Provider {
118118

119119
if (!streamedToolCalls[index]) {
120120
streamedToolCalls[index] = toolCall;
121+
if (streamedToolCalls[index].function && streamedToolCalls[index].function.arguments == null) {
122+
streamedToolCalls[index].function.arguments = '';
123+
}
121124
} else {
122125
if (streamedToolCalls[index].function) {
123126
streamedToolCalls[index].function.arguments +=

0 commit comments

Comments
 (0)