-
Notifications
You must be signed in to change notification settings - Fork 139
fix: emit tool-input-end before tool-call in multi-chunk and flush paths #415
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
robert-j-y
wants to merge
2
commits into
main
Choose a base branch
from
devin/1770920678-fix-tool-input-end-streaming
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.
+197
−4
Open
Changes from 1 commit
Commits
Show all changes
2 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,9 @@ | ||
| --- | ||
| "@openrouter/ai-sdk-provider": patch | ||
| --- | ||
|
|
||
| Fix missing `tool-input-end` event in multi-chunk and flush tool call streaming paths | ||
|
|
||
| The multi-chunk tool call merge path and the flush path for unsent tool calls were missing the `tool-input-end` event before emitting `tool-call`. This diverged from the stream event protocol used by `@ai-sdk/openai`, which consistently emits `tool-input-start → tool-input-delta → tool-input-end → tool-call`. | ||
|
|
||
| The flush path for unsent tool calls also now emits the full `tool-input-start → tool-input-delta → tool-input-end` sequence before `tool-call`, matching the reference implementation. |
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,95 @@ | ||
| /** | ||
| * Regression test for GitHub issue #413 | ||
| * https://github.com/OpenRouterTeam/ai-sdk-provider/issues/413 | ||
| * | ||
| * Issue: "Tool call events are buffered until stream ends (flush) causing | ||
| * perceived streaming delay" | ||
| * | ||
| * The reporter observed that streaming tool calls with @openrouter/ai-sdk-provider | ||
| * were missing `tool-input-end` events in the multi-chunk tool call path, diverging | ||
| * from the protocol used by @ai-sdk/openai. The single-chunk path correctly emitted | ||
| * tool-input-start -> tool-input-delta -> tool-input-end -> tool-call, but the | ||
| * multi-chunk merge path skipped tool-input-end before tool-call. | ||
| * | ||
| * This test verifies that streamText with tool calls emits the complete event | ||
| * sequence including tool-input-end before tool-call. | ||
| */ | ||
| import { streamText, tool } from 'ai'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
| import { z } from 'zod/v4'; | ||
| import { createOpenRouter } from '@/src'; | ||
|
|
||
| vi.setConfig({ | ||
| testTimeout: 60_000, | ||
| }); | ||
|
|
||
| describe('Issue #413: Tool call streaming should emit tool-input-end before tool-call', () => { | ||
| const openrouter = createOpenRouter({ | ||
| apiKey: process.env.OPENROUTER_API_KEY, | ||
| baseUrl: `${process.env.OPENROUTER_API_BASE}/api/v1`, | ||
| }); | ||
|
|
||
| it('should emit tool-input-end before tool-call with openai/gpt-4.1-nano', async () => { | ||
| const getWeather = tool({ | ||
| description: 'Gets the current weather for a given city', | ||
| inputSchema: z.object({ | ||
| city: z.string().describe('The city to get weather for'), | ||
| }), | ||
| execute: async ({ city }) => { | ||
| return { city, temperature: 72, condition: 'sunny' }; | ||
| }, | ||
| }); | ||
|
|
||
| const response = streamText({ | ||
| model: openrouter('openai/gpt-4.1-nano'), | ||
| prompt: 'What is the weather in San Francisco? Use the getWeather tool.', | ||
| tools: { getWeather }, | ||
| toolChoice: 'required', | ||
| }); | ||
|
|
||
| const events: string[] = []; | ||
| for await (const event of response.fullStream) { | ||
| events.push(event.type); | ||
| } | ||
|
|
||
| expect(events).toContain('tool-input-start'); | ||
| expect(events).toContain('tool-input-end'); | ||
| expect(events).toContain('tool-call'); | ||
|
|
||
| const toolInputEndIndex = events.indexOf('tool-input-end'); | ||
| const toolCallIndex = events.indexOf('tool-call'); | ||
| expect(toolInputEndIndex).toBeLessThan(toolCallIndex); | ||
| }); | ||
|
|
||
| it('should emit tool-input-end before tool-call with openai/gpt-4.1-mini', async () => { | ||
| const getWeather = tool({ | ||
| description: 'Gets the current weather for a given city', | ||
| inputSchema: z.object({ | ||
| city: z.string().describe('The city to get weather for'), | ||
| }), | ||
| execute: async ({ city }) => { | ||
| return { city, temperature: 72, condition: 'sunny' }; | ||
| }, | ||
| }); | ||
|
|
||
| const response = streamText({ | ||
| model: openrouter('openai/gpt-4.1-mini'), | ||
| prompt: 'What is the weather in San Francisco? Use the getWeather tool.', | ||
| tools: { getWeather }, | ||
| toolChoice: 'required', | ||
| }); | ||
|
|
||
| const events: string[] = []; | ||
| for await (const event of response.fullStream) { | ||
| events.push(event.type); | ||
| } | ||
|
|
||
| expect(events).toContain('tool-input-start'); | ||
| expect(events).toContain('tool-input-end'); | ||
| expect(events).toContain('tool-call'); | ||
|
|
||
| const toolInputEndIndex = events.indexOf('tool-input-end'); | ||
| const toolCallIndex = events.indexOf('tool-call'); | ||
| expect(toolInputEndIndex).toBeLessThan(toolCallIndex); | ||
| }); | ||
| }); |
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
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.
🚩 Pre-existing: initial chunk arguments are not emitted as a delta in the multi-chunk path
When the first chunk of a tool call includes non-empty but non-parsable-JSON arguments (e.g.,
{"val), those arguments are stored intoolCall.function.argumentsat line 889 but never emitted as atool-input-delta. Thetool-input-startand firsttool-input-deltaare only emitted on the second chunk (merge path, lines 968-987), which only includes the merge chunk's delta, not the initial arguments. In the existing test (src/chat/index.test.ts:1476), the initial chunk hasarguments: ""(empty string), so this isn't visible. This is a pre-existing issue not introduced by this PR, but worth noting for correctness if providers ever send non-empty partial arguments in the first tool call chunk.(Refers to lines 884-893)
Was this helpful? React with 👍 or 👎 to provide feedback.