fix(openai): emit incremental tool-call events during streaming - #7
Open
xisongtang wants to merge 1 commit into
Open
fix(openai): emit incremental tool-call events during streaming#7xisongtang wants to merge 1 commit into
xisongtang wants to merge 1 commit into
Conversation
The OpenAI-compatible provider only yielded a tool_call event once the stream finished (on finish_reason), unlike the Google, Anthropic and Ollama providers which surface tool-call progress as it arrives. Emit the accumulated tool calls on every chunk that carries tool-call deltas so consumers can render live tool-argument progress (e.g. streaming previews).
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The OpenAI-compatible provider only emitted a
tool_callevent once the stream finished (whenfinish_reasonwastool_calls/stop). The Google, Anthropic, and Ollama providers all surface tool-call progress as it arrives during streaming, so OpenAI was the inconsistent outlier.This emits the accumulated tool calls on every chunk that carries tool-call deltas, so consumers can render live tool-argument progress (e.g. streaming UI previews of structured tool output) instead of waiting for the entire response to finish.
Before / After
// Accumulate streamed tool calls if (delta.tool_calls) { for (const tc of delta.tool_calls) { /* accumulate into toolCallAccum */ } + // Incremental tool-call progress: emit accumulated arguments as they + // stream in, matching the Google/Anthropic/Ollama providers. + if (toolCallAccum.size > 0) { + const calls = Array.from(toolCallAccum.values()) + .map((tc) => this.normalizeToolCall(tc)); + yield { type: 'tool_call', calls }; + } if (loopGuard.detection) break; }The existing finish-gated emit (
Emit tool calls when stream finishes) is unchanged, so consumers relying on a single final event are unaffected.Why
Discovered while building live streaming previews: the OpenAI provider gave no intermediate
tool_callevents, so partial tool arguments (e.g. a large JSON payload being generated) could not be previewed until completion — unlike the other three providers.Notes
dist/is gitignored; only the source change is included.bun testin my environment (bun not installed locally). Please run the suite; happy to add a unit test (fetch/SSE mock) if you'd like coverage for this path.