Skip to content

fix(ai, ai-openai): normalize null tool input to empty object#430

Open
AlemTuzlak wants to merge 3 commits intomainfrom
fix/null-tool-input-normalization
Open

fix(ai, ai-openai): normalize null tool input to empty object#430
AlemTuzlak wants to merge 3 commits intomainfrom
fix/null-tool-input-normalization

Conversation

@AlemTuzlak
Copy link
Copy Markdown
Contributor

@AlemTuzlak AlemTuzlak commented Apr 8, 2026

Summary

  • When a model produces a tool_use block with no input (or literal null), 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 four locations:
    • executeToolCalls(): after JSON.parse of arguments string
    • ToolCallManager.completeToolCall(): before JSON.stringify of event input
    • ToolCallManager.executeTools(): replace fragile argsString === 'null' string comparison with robust type check
    • OpenAI adapter: in TOOL_CALL_END emission (matching existing Anthropic adapter fix)
  • The Anthropic adapter already had this fix; this PR extends it to all downstream code paths and the OpenAI adapter

Fixes #265

Test plan

  • New test file with 5 cases covering null, empty, and valid arguments for both executeToolCalls and ToolCallManager
  • All 636 @tanstack/ai tests pass
  • All 127 @tanstack/ai-openai tests pass

Summary by CodeRabbit

  • Bug Fixes

    • Normalize tool call inputs so null or non-object values become empty objects; validate parsed function-call arguments are objects before use.
  • Tests

    • Added tests covering null, empty-string, and valid-object tool argument parsing and execution.
  • Chores

    • Added patch release note documenting the null→empty-object normalization fix.

When a model produces a tool_use block with no input (or literal null),
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 four locations:
- executeToolCalls(): after JSON.parse of arguments string
- ToolCallManager.completeToolCall(): before JSON.stringify of event input
- ToolCallManager.executeTools(): replace fragile string comparison
- OpenAI adapter: in TOOL_CALL_END emission (matching existing Anthropic fix)

Fixes #265
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 8, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a80cdb1c-75e1-4a3c-9aad-3a8b6e2e648f

📥 Commits

Reviewing files that changed from the base of the PR and between 4188203 and 80fb196.

📒 Files selected for processing (1)
  • packages/typescript/ai/tests/tool-calls-null-input.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/typescript/ai/tests/tool-calls-null-input.test.ts

📝 Walkthrough

Walkthrough

Normalize non-object or null tool call arguments to {} across the OpenAI adapter and core tool execution paths; add tests and a changeset to ensure null, '', and valid JSON object arguments are handled consistently.

Changes

Cohort / File(s) Summary
OpenAI Adapter
packages/typescript/ai-openai/src/adapters/text.ts
When parsing chunk.arguments for response.function_call_arguments.done, only accept parsed JSON if it's a non-null object; otherwise default to {}.
Core Tool Handling
packages/typescript/ai/src/activities/chat/tools/tool-calls.ts
Normalize tool inputs so null or non-object parsed values become {} before stringifying/parsing and prior to schema validation in both ToolCallManager.executeTools() and top-level executeToolCalls() flows.
Tests
packages/typescript/ai/tests/tool-calls-null-input.test.ts
Added tests verifying normalization of 'null', empty string, and valid JSON object arguments through executeToolCalls and ToolCallManager.completeToolCall.
Release Notes
.changeset/fix-null-tool-input-normalization.md
Added changeset entry describing the fix: normalize null tool input to empty object for a patch release.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 I nibbled at a puzzling null,
Turned empty bites to tidy hull—
Now inputs nest in curly cheer,
No stalled loops to cause a tear.
Hopping on, the agents run anew! 🥕✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description comprehensively covers what changed and why, includes test plan with specific test counts, but does not follow the provided template structure with sections like 'Changes' and 'Checklist'. Restructure the description to match the template: use '## 🎯 Changes' section header, include the '✅ Checklist' section with the specified items, and verify the '🚀 Release Impact' checkbox for changesets.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: normalizing null tool input to empty object across ai and ai-openai packages.
Linked Issues check ✅ Passed All code changes directly address issue #265 requirements: normalize null/non-object tool input to {} in executeToolCalls, ToolCallManager methods, and OpenAI adapter, with comprehensive test coverage.
Out of Scope Changes check ✅ Passed All changes are scoped to the stated objectives: tool argument normalization in four specific locations, new test file for null input handling, and changeset entry.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/null-tool-input-normalization

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 8, 2026

🚀 Changeset Version Preview

2 package(s) bumped directly, 25 bumped as dependents.

🟩 Patch bumps

Package Version Reason
@tanstack/ai 0.10.0 → 0.10.1 Changeset
@tanstack/ai-openai 0.7.3 → 0.7.4 Changeset
@tanstack/ai-client 0.7.7 → 0.7.8 Dependent
@tanstack/ai-code-mode 0.1.0 → 0.1.1 Dependent
@tanstack/ai-code-mode-models-eval 0.0.1 → 0.0.2 Dependent
@tanstack/ai-code-mode-skills 0.1.0 → 0.1.1 Dependent
@tanstack/ai-devtools-core 0.3.17 → 0.3.18 Dependent
@tanstack/ai-event-client 0.2.0 → 0.2.1 Dependent
@tanstack/ai-fal 0.6.10 → 0.6.11 Dependent
@tanstack/ai-isolate-cloudflare 0.1.0 → 0.1.1 Dependent
@tanstack/ai-isolate-node 0.1.0 → 0.1.1 Dependent
@tanstack/ai-isolate-quickjs 0.1.0 → 0.1.1 Dependent
@tanstack/ai-openrouter 0.7.1 → 0.7.2 Dependent
@tanstack/ai-preact 0.6.12 → 0.6.13 Dependent
@tanstack/ai-react 0.7.8 → 0.7.9 Dependent
@tanstack/ai-solid 0.6.12 → 0.6.13 Dependent
@tanstack/ai-svelte 0.6.12 → 0.6.13 Dependent
@tanstack/ai-vue 0.6.12 → 0.6.13 Dependent
@tanstack/ai-vue-ui 0.1.23 → 0.1.24 Dependent
@tanstack/preact-ai-devtools 0.1.21 → 0.1.22 Dependent
@tanstack/react-ai-devtools 0.2.21 → 0.2.22 Dependent
@tanstack/smoke-tests-e2e 0.0.33 → 0.0.34 Dependent
@tanstack/solid-ai-devtools 0.2.21 → 0.2.22 Dependent
@tanstack/tests-adapters 0.1.26 → 0.1.27 Dependent
ts-svelte-chat 0.1.27 → 0.1.28 Dependent
ts-vue-chat 0.1.27 → 0.1.28 Dependent
vanilla-chat 0.0.27 → 0.0.28 Dependent

@nx-cloud
Copy link
Copy Markdown

nx-cloud bot commented Apr 8, 2026

View your CI Pipeline Execution ↗ for commit 80fb196

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 14s View ↗
nx run-many --targets=build --exclude=examples/** ✅ Succeeded 1s View ↗

☁️ Nx Cloud last updated this comment at 2026-04-08 11:52:48 UTC

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 8, 2026

Open in StackBlitz

@tanstack/ai

npm i https://pkg.pr.new/@tanstack/ai@430

@tanstack/ai-anthropic

npm i https://pkg.pr.new/@tanstack/ai-anthropic@430

@tanstack/ai-client

npm i https://pkg.pr.new/@tanstack/ai-client@430

@tanstack/ai-code-mode

npm i https://pkg.pr.new/@tanstack/ai-code-mode@430

@tanstack/ai-code-mode-skills

npm i https://pkg.pr.new/@tanstack/ai-code-mode-skills@430

@tanstack/ai-devtools-core

npm i https://pkg.pr.new/@tanstack/ai-devtools-core@430

@tanstack/ai-elevenlabs

npm i https://pkg.pr.new/@tanstack/ai-elevenlabs@430

@tanstack/ai-event-client

npm i https://pkg.pr.new/@tanstack/ai-event-client@430

@tanstack/ai-fal

npm i https://pkg.pr.new/@tanstack/ai-fal@430

@tanstack/ai-gemini

npm i https://pkg.pr.new/@tanstack/ai-gemini@430

@tanstack/ai-grok

npm i https://pkg.pr.new/@tanstack/ai-grok@430

@tanstack/ai-groq

npm i https://pkg.pr.new/@tanstack/ai-groq@430

@tanstack/ai-isolate-cloudflare

npm i https://pkg.pr.new/@tanstack/ai-isolate-cloudflare@430

@tanstack/ai-isolate-node

npm i https://pkg.pr.new/@tanstack/ai-isolate-node@430

@tanstack/ai-isolate-quickjs

npm i https://pkg.pr.new/@tanstack/ai-isolate-quickjs@430

@tanstack/ai-ollama

npm i https://pkg.pr.new/@tanstack/ai-ollama@430

@tanstack/ai-openai

npm i https://pkg.pr.new/@tanstack/ai-openai@430

@tanstack/ai-openrouter

npm i https://pkg.pr.new/@tanstack/ai-openrouter@430

@tanstack/ai-preact

npm i https://pkg.pr.new/@tanstack/ai-preact@430

@tanstack/ai-react

npm i https://pkg.pr.new/@tanstack/ai-react@430

@tanstack/ai-react-ui

npm i https://pkg.pr.new/@tanstack/ai-react-ui@430

@tanstack/ai-solid

npm i https://pkg.pr.new/@tanstack/ai-solid@430

@tanstack/ai-solid-ui

npm i https://pkg.pr.new/@tanstack/ai-solid-ui@430

@tanstack/ai-svelte

npm i https://pkg.pr.new/@tanstack/ai-svelte@430

@tanstack/ai-vue

npm i https://pkg.pr.new/@tanstack/ai-vue@430

@tanstack/ai-vue-ui

npm i https://pkg.pr.new/@tanstack/ai-vue-ui@430

@tanstack/preact-ai-devtools

npm i https://pkg.pr.new/@tanstack/preact-ai-devtools@430

@tanstack/react-ai-devtools

npm i https://pkg.pr.new/@tanstack/react-ai-devtools@430

@tanstack/solid-ai-devtools

npm i https://pkg.pr.new/@tanstack/solid-ai-devtools@430

commit: 80fb196

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/typescript/ai/tests/tool-calls-null-input.test.ts`:
- Around line 108-136: The tests fail to compile because the ToolCallManager
constructor currently accepts only one parameter (tools: ReadonlyArray<Tool>)
but tests call new ToolCallManager(..., mockFinishedEvent); update the
ToolCallManager constructor signature to accept an optional second parameter
(e.g., finishedEvent or initialFinishedEvent) with the same type as
mockFinishedEvent, adjust the constructor implementation to handle when that
second argument is provided (store or process it the same way tests expect), and
ensure all usages and exports of ToolCallManager are updated to the new
signature so the tests compiling calls to ToolCallManager(...,
mockFinishedEvent) succeed.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: be0d9dc8-a6e1-48a3-81fe-d6e79d0c38d0

📥 Commits

Reviewing files that changed from the base of the PR and between 4afaa76 and 4c4cfe8.

📒 Files selected for processing (3)
  • packages/typescript/ai-openai/src/adapters/text.ts
  • packages/typescript/ai/src/activities/chat/tools/tool-calls.ts
  • packages/typescript/ai/tests/tool-calls-null-input.test.ts

Comment on lines +108 to +136
describe('ToolCallManager.completeToolCall', () => {
it('should normalize null input to empty object', () => {
const manager = new ToolCallManager([], mockFinishedEvent)

// 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('{}')
})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify ToolCallManager constructor signature
ast-grep --pattern 'class ToolCallManager {
  $$$
  constructor($PARAMS) {
    $$$
  }
  $$$
}'

Repository: TanStack/ai

Length of output: 17852


🏁 Script executed:

cat -n packages/typescript/ai/tests/tool-calls-null-input.test.ts

Repository: TanStack/ai

Length of output: 5661


Constructor signature mismatch — tests will not compile.

ToolCallManager constructor 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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
describe('ToolCallManager.completeToolCall', () => {
it('should normalize null input to empty object', () => {
const manager = new ToolCallManager([], mockFinishedEvent)
// 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('{}')
})
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('{}')
})
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/typescript/ai/tests/tool-calls-null-input.test.ts` around lines 108
- 136, The tests fail to compile because the ToolCallManager constructor
currently accepts only one parameter (tools: ReadonlyArray<Tool>) but tests call
new ToolCallManager(..., mockFinishedEvent); update the ToolCallManager
constructor signature to accept an optional second parameter (e.g.,
finishedEvent or initialFinishedEvent) with the same type as mockFinishedEvent,
adjust the constructor implementation to handle when that second argument is
provided (store or process it the same way tests expect), and ensure all usages
and exports of ToolCallManager are updated to the new signature so the tests
compiling calls to ToolCallManager(..., mockFinishedEvent) succeed.

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.

Anthropic adapter passes null tool input when model produces empty tool_use block, causing agent loop to stall

1 participant