fix(chat): support custom tool calls in parser and ChatCompletionStream - #2023
fix(chat): support custom tool calls in parser and ChatCompletionStream#2023hsusul wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d2100cf1ee
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if (toolCall.type !== 'function') { | ||
| return toolCall; |
There was a problem hiding this comment.
Permit custom tools through the parse validator
When chat.completions.parse() is given the custom tool needed to produce this call, it invokes validateInputTools(body.tools) before making the request, and that validator still throws for every non-function tool (src/resources/chat/completions/completions.ts:171-185, src/lib/parser.ts:306-312). Consequently this new pass-through branch is unreachable through the advertised .parse() helper; the validator should allow custom tools through without attempting to auto-parse them while retaining strictness checks for function tools.
Useful? React with 👍 / 👎.
| toolCall: ChatCompletionMessageFunctionToolCall, | ||
| ): ParsedFunctionToolCall { | ||
| toolCall: ChatCompletionMessageToolCall, | ||
| ): ParsedFunctionToolCall | ChatCompletionMessageToolCall { |
There was a problem hiding this comment.
Include custom calls in the parsed completion type
This return type now admits a custom tool call, but the public ParsedChatCompletionMessage.tool_calls property remains Array<ParsedFunctionToolCall> in src/resources/chat/completions/completions.ts:286-288. Thus TypeScript consumers of .parse() or stream.finalChatCompletion() cannot narrow to or access .custom, while the declared type incorrectly guarantees that .function exists; the parsed tool-call type needs to be a union that preserves custom calls.
Useful? React with 👍 / 👎.
Problem
When a Chat Completion response contains a
customtool call (tool_call.type === 'custom'), helper functions insrc/lib/parser.tsandsrc/lib/ChatCompletionStream.tsfail with errors such asCurrently only 'function' tool calls are supported; Received 'custom'orTypeError: Cannot read properties of undefined (reading 'name')ormissing choices[0].tool_calls[0].function.name.Affected Scenario
Using
client.chat.completions.parse(),maybeParseChatCompletion(), orChatCompletionStream/.stream()with completions containingcustomtool calls.Root Cause
assertToolCallsAreChatCompletionFunctionToolCallsinsrc/lib/parser.tsstrictly rejected any non-functiontool calls, causingmaybeParseChatCompletionandparseChatCompletionto throw an error when processing completions with custom tool calls.shouldParseToolCalldid not guard against non-functiontool calls before accessingtoolCall.function.name.ChatCompletionStreamassumed all streamed tool call snapshots and deltas have afunctionproperty, throwing errors in#addChunk,#emitToolCallDoneEvent, andfinalizeChatCompletion.Solution
maybeParseChatCompletion,parseChatCompletion, andparseToolCallinsrc/lib/parser.tsto pass through non-function tool calls (such ascustomtool calls) without throwing.toolCall.type === 'function'guard toshouldParseToolCall.ChatCompletionStreamto safely accumulate deltas forcustomtool calls, map them during finalization, and skiptool_calls.function.arguments.*event emission for non-function tool calls.ChatCompletionSnapshot.Choice.Message.ToolCalltype definitions to supportCustomToolCall.Regression Coverage
Added unit tests in:
tests/lib/parser.test.ts: verifyingmaybeParseChatCompletionpreservescustomtool calls without throwing.tests/lib/ChatCompletionStream.test.ts: verifying stream accumulation and finalization of custom tool calls.Validation Commands
pnpm jest tests/lib/parser.test.ts tests/lib/ChatCompletionStream.test.ts(PASS)pnpm build(PASS)pnpm lint(PASS)pnpm format(PASS)pnpm test(PASS)Generated Code Impact
None. Changes are strictly confined to manually maintained files in
src/lib/andtests/lib/.