Skip to content

Commit 2672160

Browse files
fix(parsing): remove tool_calls default empty array (#1341)
1 parent 2bce865 commit 2672160

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

src/lib/parser.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,15 @@ export function maybeParseChatCompletion<
119119
...completion,
120120
choices: completion.choices.map((choice) => ({
121121
...choice,
122-
message: { ...choice.message, parsed: null, tool_calls: choice.message.tool_calls ?? [] },
122+
message: {
123+
...choice.message,
124+
parsed: null,
125+
...(choice.message.tool_calls ?
126+
{
127+
tool_calls: choice.message.tool_calls,
128+
}
129+
: undefined),
130+
},
123131
})),
124132
};
125133
}
@@ -144,7 +152,12 @@ export function parseChatCompletion<
144152
...choice,
145153
message: {
146154
...choice.message,
147-
tool_calls: choice.message.tool_calls?.map((toolCall) => parseToolCall(params, toolCall)) ?? [],
155+
...(choice.message.tool_calls ?
156+
{
157+
tool_calls:
158+
choice.message.tool_calls?.map((toolCall) => parseToolCall(params, toolCall)) ?? undefined,
159+
}
160+
: undefined),
148161
parsed:
149162
choice.message.content && !choice.message.refusal ?
150163
parseResponseFormat(params, choice.message.content)

src/resources/beta/chat/completions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export interface ParsedFunctionToolCall extends ChatCompletionMessageToolCall {
5050

5151
export interface ParsedChatCompletionMessage<ParsedT> extends ChatCompletionMessage {
5252
parsed: ParsedT | null;
53-
tool_calls: Array<ParsedFunctionToolCall>;
53+
tool_calls?: Array<ParsedFunctionToolCall>;
5454
}
5555

5656
export interface ParsedChoice<ParsedT> extends ChatCompletion.Choice {

tests/lib/ChatCompletionRunFunctions.test.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ describe('resource completions', () => {
628628
content: "it's raining",
629629
parsed: null,
630630
refusal: null,
631-
tool_calls: [],
631+
tool_calls: undefined,
632632
},
633633
]);
634634
expect(listener.functionCallResults).toEqual([`it's raining`]);
@@ -876,7 +876,7 @@ describe('resource completions', () => {
876876
content: 'there are 3 properties in {"a": 1, "b": 2, "c": 3}',
877877
parsed: null,
878878
refusal: null,
879-
tool_calls: [],
879+
tool_calls: undefined,
880880
},
881881
]);
882882
expect(listener.functionCallResults).toEqual(['3']);
@@ -1125,7 +1125,7 @@ describe('resource completions', () => {
11251125
content: 'there are 3 properties in {"a": 1, "b": 2, "c": 3}',
11261126
parsed: null,
11271127
refusal: null,
1128-
tool_calls: [],
1128+
tool_calls: undefined,
11291129
},
11301130
]);
11311131
expect(listener.functionCallResults).toEqual([`must be an object`, '3']);
@@ -1443,7 +1443,7 @@ describe('resource completions', () => {
14431443
content: "it's raining",
14441444
parsed: null,
14451445
refusal: null,
1446-
tool_calls: [],
1446+
tool_calls: undefined,
14471447
},
14481448
]);
14491449
expect(listener.functionCallResults).toEqual([
@@ -1572,7 +1572,7 @@ describe('resource completions', () => {
15721572
content: "it's raining",
15731573
parsed: null,
15741574
refusal: null,
1575-
tool_calls: [],
1575+
tool_calls: undefined,
15761576
},
15771577
]);
15781578
expect(listener.eventFunctionCallResults).toEqual([`it's raining`]);
@@ -1795,7 +1795,7 @@ describe('resource completions', () => {
17951795
content: 'there are 3 properties in {"a": 1, "b": 2, "c": 3}',
17961796
parsed: null,
17971797
refusal: null,
1798-
tool_calls: [],
1798+
tool_calls: undefined,
17991799
},
18001800
]);
18011801
expect(listener.eventFunctionCallResults).toEqual(['3']);
@@ -1997,7 +1997,7 @@ describe('resource completions', () => {
19971997
content: 'there are 3 properties in {"a": 1, "b": 2, "c": 3}',
19981998
parsed: null,
19991999
refusal: null,
2000-
tool_calls: [],
2000+
tool_calls: undefined,
20012001
},
20022002
]);
20032003
expect(listener.eventFunctionCallResults).toEqual([`must be an object`, '3']);
@@ -2301,7 +2301,7 @@ describe('resource completions', () => {
23012301
content: "it's raining",
23022302
parsed: null,
23032303
refusal: null,
2304-
tool_calls: [],
2304+
tool_calls: undefined,
23052305
},
23062306
]);
23072307
expect(listener.eventFunctionCallResults).toEqual([
@@ -2347,7 +2347,7 @@ describe('resource completions', () => {
23472347
content: 'The weather is great today!',
23482348
parsed: null,
23492349
refusal: null,
2350-
tool_calls: [],
2350+
tool_calls: undefined,
23512351
});
23522352
await listener.sanityCheck();
23532353
});
@@ -2386,7 +2386,7 @@ describe('resource completions', () => {
23862386
content: 'The weather is great today!',
23872387
parsed: null,
23882388
refusal: null,
2389-
tool_calls: [],
2389+
tool_calls: undefined,
23902390
});
23912391
await listener.sanityCheck();
23922392
});

tests/lib/ChatCompletionStream.test.ts

-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ describe('.stream()', () => {
3939
},
4040
"refusal": null,
4141
"role": "assistant",
42-
"tool_calls": [],
4342
},
4443
}
4544
`);
@@ -198,7 +197,6 @@ describe('.stream()', () => {
198197
},
199198
"refusal": null,
200199
"role": "assistant",
201-
"tool_calls": [],
202200
},
203201
}
204202
`);
@@ -386,7 +384,6 @@ describe('.stream()', () => {
386384
"parsed": null,
387385
"refusal": "I'm very sorry, but I can't assist with that request.",
388386
"role": "assistant",
389-
"tool_calls": [],
390387
},
391388
}
392389
`);

tests/lib/parser.test.ts

-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ describe('.parse()', () => {
3939
},
4040
"refusal": null,
4141
"role": "assistant",
42-
"tool_calls": [],
4342
},
4443
}
4544
`);
@@ -154,7 +153,6 @@ describe('.parse()', () => {
154153
},
155154
"refusal": null,
156155
"role": "assistant",
157-
"tool_calls": [],
158156
}
159157
`);
160158

@@ -488,7 +486,6 @@ describe('.parse()', () => {
488486
},
489487
"refusal": null,
490488
"role": "assistant",
491-
"tool_calls": [],
492489
}
493490
`);
494491
});
@@ -787,7 +784,6 @@ describe('.parse()', () => {
787784
},
788785
"refusal": null,
789786
"role": "assistant",
790-
"tool_calls": [],
791787
}
792788
`);
793789
});
@@ -947,7 +943,6 @@ describe('.parse()', () => {
947943
},
948944
"refusal": null,
949945
"role": "assistant",
950-
"tool_calls": [],
951946
}
952947
`);
953948
});
@@ -1061,7 +1056,6 @@ describe('.parse()', () => {
10611056
},
10621057
"refusal": null,
10631058
"role": "assistant",
1064-
"tool_calls": [],
10651059
}
10661060
`);
10671061
});

0 commit comments

Comments
 (0)