Skip to content

Commit c2836d8

Browse files
committed
fix(parsing): don't default to an empty
1 parent 2bce865 commit c2836d8

File tree

6 files changed

+24
-23
lines changed

6 files changed

+24
-23
lines changed

src/lib/parser.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ 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: { ...choice.message, parsed: null, tool_calls: choice.message.tool_calls ?? undefined },
123123
})),
124124
};
125125
}
@@ -144,7 +144,8 @@ export function parseChatCompletion<
144144
...choice,
145145
message: {
146146
...choice.message,
147-
tool_calls: choice.message.tool_calls?.map((toolCall) => parseToolCall(params, toolCall)) ?? [],
147+
tool_calls:
148+
choice.message.tool_calls?.map((toolCall) => parseToolCall(params, toolCall)) ?? undefined,
148149
parsed:
149150
choice.message.content && !choice.message.refusal ?
150151
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
@@ -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> | undefined;
5454
}
5555

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

src/resources/chat/completions/completions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ export interface ChatCompletionMessage {
701701
/**
702702
* The tool calls generated by the model, such as function calls.
703703
*/
704-
tool_calls?: Array<ChatCompletionMessageToolCall>;
704+
tool_calls?: Array<ChatCompletionMessageToolCall> | undefined;
705705
}
706706

707707
export namespace ChatCompletionMessage {

tests/lib/ChatCompletionRunFunctions.test.ts

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('.stream()', () => {
3939
},
4040
"refusal": null,
4141
"role": "assistant",
42-
"tool_calls": [],
42+
"tool_calls": undefined,
4343
},
4444
}
4545
`);
@@ -198,7 +198,7 @@ describe('.stream()', () => {
198198
},
199199
"refusal": null,
200200
"role": "assistant",
201-
"tool_calls": [],
201+
"tool_calls": undefined,
202202
},
203203
}
204204
`);
@@ -386,7 +386,7 @@ describe('.stream()', () => {
386386
"parsed": null,
387387
"refusal": "I'm very sorry, but I can't assist with that request.",
388388
"role": "assistant",
389-
"tool_calls": [],
389+
"tool_calls": undefined,
390390
},
391391
}
392392
`);

tests/lib/parser.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('.parse()', () => {
3939
},
4040
"refusal": null,
4141
"role": "assistant",
42-
"tool_calls": [],
42+
"tool_calls": undefined,
4343
},
4444
}
4545
`);
@@ -154,7 +154,7 @@ describe('.parse()', () => {
154154
},
155155
"refusal": null,
156156
"role": "assistant",
157-
"tool_calls": [],
157+
"tool_calls": undefined,
158158
}
159159
`);
160160

@@ -488,7 +488,7 @@ describe('.parse()', () => {
488488
},
489489
"refusal": null,
490490
"role": "assistant",
491-
"tool_calls": [],
491+
"tool_calls": undefined,
492492
}
493493
`);
494494
});
@@ -787,7 +787,7 @@ describe('.parse()', () => {
787787
},
788788
"refusal": null,
789789
"role": "assistant",
790-
"tool_calls": [],
790+
"tool_calls": undefined,
791791
}
792792
`);
793793
});
@@ -947,7 +947,7 @@ describe('.parse()', () => {
947947
},
948948
"refusal": null,
949949
"role": "assistant",
950-
"tool_calls": [],
950+
"tool_calls": undefined,
951951
}
952952
`);
953953
});
@@ -1061,7 +1061,7 @@ describe('.parse()', () => {
10611061
},
10621062
"refusal": null,
10631063
"role": "assistant",
1064-
"tool_calls": [],
1064+
"tool_calls": undefined,
10651065
}
10661066
`);
10671067
});

0 commit comments

Comments
 (0)