Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parsing): remove tool_calls default empty array #1341

Merged
merged 1 commit into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/lib/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,15 @@ export function maybeParseChatCompletion<
...completion,
choices: completion.choices.map((choice) => ({
...choice,
message: { ...choice.message, parsed: null, tool_calls: choice.message.tool_calls ?? [] },
message: {
...choice.message,
parsed: null,
...(choice.message.tool_calls ?
{
tool_calls: choice.message.tool_calls,
}
: undefined),
},
})),
};
}
Expand All @@ -144,7 +152,12 @@ export function parseChatCompletion<
...choice,
message: {
...choice.message,
tool_calls: choice.message.tool_calls?.map((toolCall) => parseToolCall(params, toolCall)) ?? [],
...(choice.message.tool_calls ?
{
tool_calls:
choice.message.tool_calls?.map((toolCall) => parseToolCall(params, toolCall)) ?? undefined,
}
: undefined),
parsed:
choice.message.content && !choice.message.refusal ?
parseResponseFormat(params, choice.message.content)
Expand Down
2 changes: 1 addition & 1 deletion src/resources/beta/chat/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export interface ParsedFunctionToolCall extends ChatCompletionMessageToolCall {

export interface ParsedChatCompletionMessage<ParsedT> extends ChatCompletionMessage {
parsed: ParsedT | null;
tool_calls: Array<ParsedFunctionToolCall>;
tool_calls?: Array<ParsedFunctionToolCall>;
}

export interface ParsedChoice<ParsedT> extends ChatCompletion.Choice {
Expand Down
20 changes: 10 additions & 10 deletions tests/lib/ChatCompletionRunFunctions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ describe('resource completions', () => {
content: "it's raining",
parsed: null,
refusal: null,
tool_calls: [],
tool_calls: undefined,
},
]);
expect(listener.functionCallResults).toEqual([`it's raining`]);
Expand Down Expand Up @@ -876,7 +876,7 @@ describe('resource completions', () => {
content: 'there are 3 properties in {"a": 1, "b": 2, "c": 3}',
parsed: null,
refusal: null,
tool_calls: [],
tool_calls: undefined,
},
]);
expect(listener.functionCallResults).toEqual(['3']);
Expand Down Expand Up @@ -1125,7 +1125,7 @@ describe('resource completions', () => {
content: 'there are 3 properties in {"a": 1, "b": 2, "c": 3}',
parsed: null,
refusal: null,
tool_calls: [],
tool_calls: undefined,
},
]);
expect(listener.functionCallResults).toEqual([`must be an object`, '3']);
Expand Down Expand Up @@ -1443,7 +1443,7 @@ describe('resource completions', () => {
content: "it's raining",
parsed: null,
refusal: null,
tool_calls: [],
tool_calls: undefined,
},
]);
expect(listener.functionCallResults).toEqual([
Expand Down Expand Up @@ -1572,7 +1572,7 @@ describe('resource completions', () => {
content: "it's raining",
parsed: null,
refusal: null,
tool_calls: [],
tool_calls: undefined,
},
]);
expect(listener.eventFunctionCallResults).toEqual([`it's raining`]);
Expand Down Expand Up @@ -1795,7 +1795,7 @@ describe('resource completions', () => {
content: 'there are 3 properties in {"a": 1, "b": 2, "c": 3}',
parsed: null,
refusal: null,
tool_calls: [],
tool_calls: undefined,
},
]);
expect(listener.eventFunctionCallResults).toEqual(['3']);
Expand Down Expand Up @@ -1997,7 +1997,7 @@ describe('resource completions', () => {
content: 'there are 3 properties in {"a": 1, "b": 2, "c": 3}',
parsed: null,
refusal: null,
tool_calls: [],
tool_calls: undefined,
},
]);
expect(listener.eventFunctionCallResults).toEqual([`must be an object`, '3']);
Expand Down Expand Up @@ -2301,7 +2301,7 @@ describe('resource completions', () => {
content: "it's raining",
parsed: null,
refusal: null,
tool_calls: [],
tool_calls: undefined,
},
]);
expect(listener.eventFunctionCallResults).toEqual([
Expand Down Expand Up @@ -2347,7 +2347,7 @@ describe('resource completions', () => {
content: 'The weather is great today!',
parsed: null,
refusal: null,
tool_calls: [],
tool_calls: undefined,
});
await listener.sanityCheck();
});
Expand Down Expand Up @@ -2386,7 +2386,7 @@ describe('resource completions', () => {
content: 'The weather is great today!',
parsed: null,
refusal: null,
tool_calls: [],
tool_calls: undefined,
});
await listener.sanityCheck();
});
Expand Down
3 changes: 0 additions & 3 deletions tests/lib/ChatCompletionStream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ describe('.stream()', () => {
},
"refusal": null,
"role": "assistant",
"tool_calls": [],
},
}
`);
Expand Down Expand Up @@ -198,7 +197,6 @@ describe('.stream()', () => {
},
"refusal": null,
"role": "assistant",
"tool_calls": [],
},
}
`);
Expand Down Expand Up @@ -386,7 +384,6 @@ describe('.stream()', () => {
"parsed": null,
"refusal": "I'm very sorry, but I can't assist with that request.",
"role": "assistant",
"tool_calls": [],
},
}
`);
Expand Down
6 changes: 0 additions & 6 deletions tests/lib/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ describe('.parse()', () => {
},
"refusal": null,
"role": "assistant",
"tool_calls": [],
},
}
`);
Expand Down Expand Up @@ -154,7 +153,6 @@ describe('.parse()', () => {
},
"refusal": null,
"role": "assistant",
"tool_calls": [],
}
`);

Expand Down Expand Up @@ -488,7 +486,6 @@ describe('.parse()', () => {
},
"refusal": null,
"role": "assistant",
"tool_calls": [],
}
`);
});
Expand Down Expand Up @@ -787,7 +784,6 @@ describe('.parse()', () => {
},
"refusal": null,
"role": "assistant",
"tool_calls": [],
}
`);
});
Expand Down Expand Up @@ -947,7 +943,6 @@ describe('.parse()', () => {
},
"refusal": null,
"role": "assistant",
"tool_calls": [],
}
`);
});
Expand Down Expand Up @@ -1061,7 +1056,6 @@ describe('.parse()', () => {
},
"refusal": null,
"role": "assistant",
"tool_calls": [],
}
`);
});
Expand Down