Skip to content

fix(chat): support custom tool calls in parser and ChatCompletionStream - #2023

Open
hsusul wants to merge 1 commit into
openai:mainfrom
hsusul:fix/chat-completion-custom-tool-calls
Open

fix(chat): support custom tool calls in parser and ChatCompletionStream#2023
hsusul wants to merge 1 commit into
openai:mainfrom
hsusul:fix/chat-completion-custom-tool-calls

Conversation

@hsusul

@hsusul hsusul commented Jul 27, 2026

Copy link
Copy Markdown

Problem

When a Chat Completion response contains a custom tool call (tool_call.type === 'custom'), helper functions in src/lib/parser.ts and src/lib/ChatCompletionStream.ts fail with errors such as Currently only 'function' tool calls are supported; Received 'custom' or TypeError: Cannot read properties of undefined (reading 'name') or missing choices[0].tool_calls[0].function.name.

Affected Scenario

Using client.chat.completions.parse(), maybeParseChatCompletion(), or ChatCompletionStream / .stream() with completions containing custom tool calls.

Root Cause

  1. assertToolCallsAreChatCompletionFunctionToolCalls in src/lib/parser.ts strictly rejected any non-function tool calls, causing maybeParseChatCompletion and parseChatCompletion to throw an error when processing completions with custom tool calls.
  2. shouldParseToolCall did not guard against non-function tool calls before accessing toolCall.function.name.
  3. ChatCompletionStream assumed all streamed tool call snapshots and deltas have a function property, throwing errors in #addChunk, #emitToolCallDoneEvent, and finalizeChatCompletion.

Solution

  1. Updated maybeParseChatCompletion, parseChatCompletion, and parseToolCall in src/lib/parser.ts to pass through non-function tool calls (such as custom tool calls) without throwing.
  2. Added a toolCall.type === 'function' guard to shouldParseToolCall.
  3. Updated ChatCompletionStream to safely accumulate deltas for custom tool calls, map them during finalization, and skip tool_calls.function.arguments.* event emission for non-function tool calls.
  4. Expanded ChatCompletionSnapshot.Choice.Message.ToolCall type definitions to support CustomToolCall.

Regression Coverage

Added unit tests in:

  • tests/lib/parser.test.ts: verifying maybeParseChatCompletion preserves custom tool 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/ and tests/lib/.

@hsusul
hsusul requested a review from a team as a code owner July 27, 2026 13:35

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread src/lib/parser.ts
Comment on lines +244 to +245
if (toolCall.type !== 'function') {
return toolCall;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge 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 👍 / 👎.

Comment thread src/lib/parser.ts
toolCall: ChatCompletionMessageFunctionToolCall,
): ParsedFunctionToolCall {
toolCall: ChatCompletionMessageToolCall,
): ParsedFunctionToolCall | ChatCompletionMessageToolCall {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Git)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants