Skip to content

fix(workers-ai-provider): keep a tool call open on an empty argument fragment - #699

Open
Kinfe123 wants to merge 1 commit into
cloudflare:mainfrom
Kinfe123:fix/tool-call-sentinel-index
Open

Kinfe123 wants to merge 1 commit into
cloudflare:mainfrom
Kinfe123:fix/tool-call-sentinel-index

Conversation

@Kinfe123

@Kinfe123 Kinfe123 commented Dec 1, 2026

Copy link
Copy Markdown

Fixes #698.

Problem

A streamed tool-call delta with an index but no id, no name and empty arguments, such as { index: 0, function: { arguments: "" } }, ends the call. streamText then reports a tool-error for the empty input, and the call's real arguments arrive as tool-input-delta after tool-call.

Root cause

isNullFinalizationChunk in src/streaming.ts didn't check index, so an empty fragment of the current call matched the null-finalization sentinel from #494.

Change

The sentinel now also requires tc.index == null. That's the same rule openAIStreamParser uses in agents/models/ai-sdk, where the comment notes that some vLLM tool parsers send empty argument fragments between real ones.

Safety and compatibility

  • The existing finalization tests still pass. Their sentinel { id: null, type: null, function: { name: null, arguments: "" } } has no index.
  • If a model ever sends the sentinel with an index, the call is closed in flush() at the end of the stream instead of eagerly. That's the behavior from before fix: emit tool-input-end eagerly for sequential tool calls #494, not a lost call.
  • Deltas that arrive for an index that's already closed are still emitted. createAI drops them. I left that out to keep this change to the one rule.

Verification

  • New test: should not treat an empty-arguments continuation delta as the finalization chunk. Before the fix it fails with tool-input-start, tool-input-end, tool-call, tool-error, tool-input-delta, tool-input-delta. After the fix it gets start, delta, delta, end, tool-call with input { location: "London" }.
  • pnpm --filter workers-ai-provider exec vitest --watch=false: 22 files, 455 tests passed.
  • tsc --noEmit in the package: no errors. oxfmt --check passes on the changed files, and oxlint reports no errors (one existing warning at test/stream-text.test.ts:1947, not from this change).
  • The repro script from workers-ai-provider: a tool-call delta with empty arguments ends the call early #698, run against the rebuilt dist/, now prints tool-call input="{\"city\":\"Paris\"}" for the empty-fragment case.

Not verified: a live Workers AI stream. I don't have one that sends an empty fragment.

Documentation and release

Patch changeset for workers-ai-provider.

@changeset-bot

changeset-bot Bot commented Dec 1, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 79e20fe

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
workers-ai-provider Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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.

workers-ai-provider: a tool-call delta with empty arguments ends the call early

1 participant