Skip to content

fix: emit tool-input-end eagerly for sequential tool calls#494

Merged
threepointone merged 1 commit intomainfrom
fix-488
Apr 15, 2026
Merged

fix: emit tool-input-end eagerly for sequential tool calls#494
threepointone merged 1 commit intomainfrom
fix-488

Conversation

@threepointone
Copy link
Copy Markdown
Collaborator

Summary

Fixes #488.

  • Emit tool-input-end + tool-call eagerly when a new tool call index starts streaming, instead of batching all of them at stream close in flush(). This matches the behavior of other AI SDK providers like @ai-sdk/openai-compatible.
  • Use the null finalization chunk as an explicit close signal. Previously this sentinel was detected and discarded; now it triggers tool-input-end for the active tool call.
  • Guard against double-close via a closedToolCalls set — if both a finalization chunk and a new-index-start would close the same tool, only the first one emits events.

Before

All tool-input-end events emitted simultaneously at stream close:

tool-input-start(0) → deltas → tool-input-start(1) → deltas → [stream ends] → tool-input-end(0), tool-call(0), tool-input-end(1), tool-call(1)

After

Each tool call is closed before the next one starts:

tool-input-start(0) → deltas → tool-input-end(0) → tool-call(0) → tool-input-start(1) → deltas → tool-input-end(1) → tool-call(1)

Test plan

  • 11 new tests covering: sequential tool calls (2 and 3 tools), null finalization chunks, double-close prevention, batch tool calls in a single SSE event, OpenAI format, text + tool call interleaving, single tool call (regression)
  • All 236 existing unit tests pass with no modifications

Made with Cursor

Previously, tool-input-end and tool-call events were batched in flush()
at stream close, making all tool calls appear simultaneously "in
progress." Now each tool call is closed as soon as the next one starts
or a null finalization chunk arrives.

Closes #488

Made-with: Cursor
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 15, 2026

🦋 Changeset detected

Latest commit: ed6112a

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

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 15, 2026

Open in StackBlitz

npx https://pkg.pr.new/cloudflare/ai/ai-gateway-provider@494
npx https://pkg.pr.new/cloudflare/ai/@cloudflare/tanstack-ai@494
npx https://pkg.pr.new/cloudflare/ai/workers-ai-provider@494

commit: ed6112a

@threepointone threepointone enabled auto-merge April 15, 2026 20:56
@threepointone threepointone disabled auto-merge April 15, 2026 20:56
@threepointone threepointone merged commit 60d9b02 into main Apr 15, 2026
3 checks passed
@threepointone threepointone deleted the fix-488 branch April 15, 2026 20:56
@github-actions github-actions bot mentioned this pull request Apr 15, 2026
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 doesn't produce tool-input-end event until entire message is done

1 participant