fix(openai): stop streaming tool-call double-emission when autoparser is active#10055
Merged
mudler merged 1 commit intoMay 29, 2026
Merged
Conversation
… is active Streaming /v1/chat/completions could emit the same logical tool call at multiple `index` values. In processStreamWithTools the Go-side iterative parser (ParseXMLIterative / ParseJSONIterative) runs on every token and emits tool-call deltas, while the C++ chat-template autoparser delivers its own tool calls via ChatDeltas that are flushed at end-of-stream by ToolCallsFromChatDeltas -> buildDeferredToolCallChunks. With both paths active the same call is emitted twice at different indices, so OpenAI clients that accumulate tool calls by `index` dispatch the tool N times. Skip the Go-side iterative parser once the autoparser is producing tool calls (hasChatDeltaToolCalls). The deferred flush stays guarded by lastEmittedCount, so the race where the Go parser emitted before the flag flipped also remains single-emission. Backends without an autoparser (e.g. vLLM) keep hasChatDeltaToolCalls=false and are unaffected. Refs mudler#9722 Co-authored-by: Cursor <cursoragent@cursor.com> Signed-off-by: bozhouDev <259759010+bozhouDev@users.noreply.github.com> Co-authored-by: Cursor <cursoragent@cursor.com>
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.
Description
Addresses #9722.
Streaming
/v1/chat/completionscould emit the same logical tool call at multipleindexvalues. InprocessStreamWithTools(core/http/endpoints/openai/chat_stream_workers.go) the Go-side iterative parser (ParseXMLIterative/ParseJSONIterative) runs on every token and emits tool-call deltas, while the C++ chat-template autoparser delivers its own tool calls viaChatDeltas, which are flushed at end-of-stream byToolCallsFromChatDeltas→buildDeferredToolCallChunks. With both paths active the same call is emitted twice at different indices, so OpenAI clients that accumulate tool calls byindexend up dispatching the tool N times.This skips the Go-side iterative parser once the autoparser is producing tool calls (
hasChatDeltaToolCalls). The deferred flush stays guarded bylastEmittedCount, so the race where the Go parser emitted before the flag flipped also remains single-emission. Backends without an autoparser (e.g. vLLM) keephasChatDeltaToolCalls=falseand are unaffected.Notes for Reviewers
/v1/chat/completionsemits the sametool_callat multipleindexvalues #9722 (the llama.cpp chat-template autoparser, which is what the reporter ran). The issue also mentions adisable_peg_parser: truevariant where the Go iterative parser alone produces multiple indices — that path keepshasChatDeltaToolCalls=false, so it is intentionally left untouched here and would need a separate change.go build ./core/http/endpoints/openai/,go vet,gofmt -l, and the existingcore/http/endpoints/openaitest suite all pass. I was not able to reproduce against a live llama.cpp model in my environment, so a maintainer sanity-check with a tool-calling GGUF (the repro in Streaming/v1/chat/completionsemits the sametool_callat multipleindexvalues #9722) would be appreciated. The change is intentionally minimal and mirrors the existingpreferAutoparserphilosophy already used in this file, so non-autoparser backends are unaffected.lastEmittedCount, or a(name, arguments)dedupe net) if you prefer one of those.Signed commits