-
-
Notifications
You must be signed in to change notification settings - Fork 167
fix(ai, ai-openai): normalize null tool input to empty object #430
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
Open
AlemTuzlak
wants to merge
3
commits into
main
Choose a base branch
from
fix/null-tool-input-normalization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+175
−5
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| '@tanstack/ai': patch | ||
| '@tanstack/ai-openai': patch | ||
| --- | ||
|
|
||
| fix(ai, ai-openai): normalize null tool input to empty object | ||
|
|
||
| When a model produces a `tool_use` block with no input, `JSON.parse('null')` returns `null` which fails Zod schema validation and silently kills the agent loop. Normalize null/non-object parsed tool input to `{}` in `executeToolCalls`, `ToolCallManager.completeToolCall`, `ToolCallManager.executeTools`, and the OpenAI adapter's `TOOL_CALL_END` emission. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
packages/typescript/ai/tests/tool-calls-null-input.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { | ||
| ToolCallManager, | ||
| executeToolCalls, | ||
| } from '../src/activities/chat/tools/tool-calls' | ||
| import type { Tool, ToolCall } from '../src/types' | ||
|
|
||
| /** | ||
| * Drain an async generator and return its final return value. | ||
| */ | ||
| async function drainGenerator<TChunk, TResult>( | ||
| gen: AsyncGenerator<TChunk, TResult, void>, | ||
| ): Promise<TResult> { | ||
| while (true) { | ||
| const next = await gen.next() | ||
| if (next.done) return next.value | ||
| } | ||
| } | ||
|
|
||
| describe('null tool input normalization', () => { | ||
| describe('executeToolCalls', () => { | ||
| it('should normalize "null" arguments to empty object', async () => { | ||
| const receivedInput = vi.fn() | ||
|
|
||
| const tool: Tool = { | ||
| name: 'test_tool', | ||
| description: 'test', | ||
| execute: async (input: unknown) => { | ||
| receivedInput(input) | ||
| return { ok: true } | ||
| }, | ||
| } | ||
|
|
||
| const toolCalls: Array<ToolCall> = [ | ||
| { | ||
| id: 'tc-1', | ||
| type: 'function', | ||
| function: { name: 'test_tool', arguments: 'null' }, | ||
| }, | ||
| ] | ||
|
|
||
| const result = await drainGenerator(executeToolCalls(toolCalls, [tool])) | ||
| expect(receivedInput).toHaveBeenCalledWith({}) | ||
| expect(result.results).toHaveLength(1) | ||
| expect(result.results[0]!.state).toBeUndefined() | ||
| }) | ||
|
|
||
| it('should normalize empty arguments to empty object', async () => { | ||
| const receivedInput = vi.fn() | ||
|
|
||
| const tool: Tool = { | ||
| name: 'test_tool', | ||
| description: 'test', | ||
| execute: async (input: unknown) => { | ||
| receivedInput(input) | ||
| return { ok: true } | ||
| }, | ||
| } | ||
|
|
||
| const toolCalls: Array<ToolCall> = [ | ||
| { | ||
| id: 'tc-1', | ||
| type: 'function', | ||
| function: { name: 'test_tool', arguments: '' }, | ||
| }, | ||
| ] | ||
|
|
||
| await drainGenerator(executeToolCalls(toolCalls, [tool])) | ||
| expect(receivedInput).toHaveBeenCalledWith({}) | ||
| }) | ||
|
|
||
| it('should pass through valid object arguments unchanged', async () => { | ||
| const receivedInput = vi.fn() | ||
|
|
||
| const tool: Tool = { | ||
| name: 'test_tool', | ||
| description: 'test', | ||
| execute: async (input: unknown) => { | ||
| receivedInput(input) | ||
| return { ok: true } | ||
| }, | ||
| } | ||
|
|
||
| const toolCalls: Array<ToolCall> = [ | ||
| { | ||
| id: 'tc-1', | ||
| type: 'function', | ||
| function: { | ||
| name: 'test_tool', | ||
| arguments: '{"location":"NYC"}', | ||
| }, | ||
| }, | ||
| ] | ||
|
|
||
| await drainGenerator(executeToolCalls(toolCalls, [tool])) | ||
| expect(receivedInput).toHaveBeenCalledWith({ location: 'NYC' }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('ToolCallManager.completeToolCall', () => { | ||
| it('should normalize null input to empty object', () => { | ||
| const manager = new ToolCallManager([]) | ||
|
|
||
| // Register a tool call | ||
| manager.addToolCallStartEvent({ | ||
| type: 'TOOL_CALL_START', | ||
| toolCallId: 'tc-1', | ||
| toolName: 'test_tool', | ||
| model: 'test', | ||
| timestamp: Date.now(), | ||
| index: 0, | ||
| }) | ||
|
|
||
| // Complete with null input (simulating Anthropic empty tool_use) | ||
| manager.completeToolCall({ | ||
| type: 'TOOL_CALL_END', | ||
| toolCallId: 'tc-1', | ||
| toolName: 'test_tool', | ||
| model: 'test', | ||
| timestamp: Date.now(), | ||
| input: null as unknown, | ||
| }) | ||
|
|
||
| const toolCalls = manager.getToolCalls() | ||
| expect(toolCalls).toHaveLength(1) | ||
| // Should be "{}" not "null" | ||
| expect(toolCalls[0]!.function.arguments).toBe('{}') | ||
| }) | ||
|
|
||
| it('should preserve valid object input', () => { | ||
| const manager = new ToolCallManager([]) | ||
|
|
||
| manager.addToolCallStartEvent({ | ||
| type: 'TOOL_CALL_START', | ||
| toolCallId: 'tc-1', | ||
| toolName: 'test_tool', | ||
| model: 'test', | ||
| timestamp: Date.now(), | ||
| index: 0, | ||
| }) | ||
|
|
||
| manager.completeToolCall({ | ||
| type: 'TOOL_CALL_END', | ||
| toolCallId: 'tc-1', | ||
| toolName: 'test_tool', | ||
| model: 'test', | ||
| timestamp: Date.now(), | ||
| input: { location: 'NYC' }, | ||
| }) | ||
|
|
||
| const toolCalls = manager.getToolCalls() | ||
| expect(toolCalls[0]!.function.arguments).toBe('{"location":"NYC"}') | ||
| }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: TanStack/ai
Length of output: 17852
🏁 Script executed:
Repository: TanStack/ai
Length of output: 5661
Constructor signature mismatch — tests will not compile.
ToolCallManagerconstructor accepts only one parameter (tools: ReadonlyArray<Tool>), but the tests at lines 110 and 139 pass a second argument (mockFinishedEvent). This causes TypeScript compilation errors.🐛 Proposed fix
describe('ToolCallManager.completeToolCall', () => { it('should normalize null input to empty object', () => { - const manager = new ToolCallManager([], mockFinishedEvent) + const manager = new ToolCallManager([]) // Register a tool call manager.addToolCallStartEvent({it('should preserve valid object input', () => { - const manager = new ToolCallManager([], mockFinishedEvent) + const manager = new ToolCallManager([]) manager.addToolCallStartEvent({📝 Committable suggestion
🤖 Prompt for AI Agents