Skip to content

Commit 40e8dd2

Browse files
committed
fix(parsing): remove tool_calls default empty array (#1341)
1 parent ee34833 commit 40e8dd2

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

src/lib/parser.ts

Lines changed: 15 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export interface ParsedFunctionToolCall extends ChatCompletionMessageToolCall {
4747

4848
export interface ParsedChatCompletionMessage<ParsedT> extends ChatCompletionMessage {
4949
parsed: ParsedT | null;
50-
tool_calls: Array<ParsedFunctionToolCall>;
50+
tool_calls?: Array<ParsedFunctionToolCall>;
5151
}
5252

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

tests/lib/ChatCompletionRunFunctions.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ describe('resource completions', () => {
627627
content: "it's raining",
628628
parsed: null,
629629
refusal: null,
630-
tool_calls: [],
630+
tool_calls: undefined,
631631
},
632632
]);
633633
expect(listener.functionCallResults).toEqual([`it's raining`]);
@@ -875,7 +875,7 @@ describe('resource completions', () => {
875875
content: 'there are 3 properties in {"a": 1, "b": 2, "c": 3}',
876876
parsed: null,
877877
refusal: null,
878-
tool_calls: [],
878+
tool_calls: undefined,
879879
},
880880
]);
881881
expect(listener.functionCallResults).toEqual(['3']);
@@ -1124,7 +1124,7 @@ describe('resource completions', () => {
11241124
content: 'there are 3 properties in {"a": 1, "b": 2, "c": 3}',
11251125
parsed: null,
11261126
refusal: null,
1127-
tool_calls: [],
1127+
tool_calls: undefined,
11281128
},
11291129
]);
11301130
expect(listener.functionCallResults).toEqual([`must be an object`, '3']);
@@ -1442,7 +1442,7 @@ describe('resource completions', () => {
14421442
content: "it's raining",
14431443
parsed: null,
14441444
refusal: null,
1445-
tool_calls: [],
1445+
tool_calls: undefined,
14461446
},
14471447
]);
14481448
expect(listener.functionCallResults).toEqual([
@@ -1571,7 +1571,7 @@ describe('resource completions', () => {
15711571
content: "it's raining",
15721572
parsed: null,
15731573
refusal: null,
1574-
tool_calls: [],
1574+
tool_calls: undefined,
15751575
},
15761576
]);
15771577
expect(listener.eventFunctionCallResults).toEqual([`it's raining`]);
@@ -1794,7 +1794,7 @@ describe('resource completions', () => {
17941794
content: 'there are 3 properties in {"a": 1, "b": 2, "c": 3}',
17951795
parsed: null,
17961796
refusal: null,
1797-
tool_calls: [],
1797+
tool_calls: undefined,
17981798
},
17991799
]);
18001800
expect(listener.eventFunctionCallResults).toEqual(['3']);
@@ -1996,7 +1996,7 @@ describe('resource completions', () => {
19961996
content: 'there are 3 properties in {"a": 1, "b": 2, "c": 3}',
19971997
parsed: null,
19981998
refusal: null,
1999-
tool_calls: [],
1999+
tool_calls: undefined,
20002000
},
20012001
]);
20022002
expect(listener.eventFunctionCallResults).toEqual([`must be an object`, '3']);
@@ -2300,7 +2300,7 @@ describe('resource completions', () => {
23002300
content: "it's raining",
23012301
parsed: null,
23022302
refusal: null,
2303-
tool_calls: [],
2303+
tool_calls: undefined,
23042304
},
23052305
]);
23062306
expect(listener.eventFunctionCallResults).toEqual([
@@ -2346,7 +2346,7 @@ describe('resource completions', () => {
23462346
content: 'The weather is great today!',
23472347
parsed: null,
23482348
refusal: null,
2349-
tool_calls: [],
2349+
tool_calls: undefined,
23502350
});
23512351
await listener.sanityCheck();
23522352
});
@@ -2385,7 +2385,7 @@ describe('resource completions', () => {
23852385
content: 'The weather is great today!',
23862386
parsed: null,
23872387
refusal: null,
2388-
tool_calls: [],
2388+
tool_calls: undefined,
23892389
});
23902390
await listener.sanityCheck();
23912391
});

tests/lib/ChatCompletionStream.test.ts

Lines changed: 0 additions & 3 deletions
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

Lines changed: 0 additions & 6 deletions
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)