fix(streaming): handle duplicate tool call index from API gateways #1443
Merged
joshua-mo-143 merged 1 commit into0xPlaygrounds:mainfrom Feb 26, 2026
Merged
Conversation
94bd39f to
717eb25
Compare
…PI gateways Some API gateways (e.g. LiteLLM, OneAPI) do not follow the OpenAI streaming convention of assigning each parallel tool call a unique index. Instead, they emit multiple distinct tool calls all at index 0, distinguishing them only by their `id` field. The previous accumulation logic keyed solely on index, causing later tool calls to overwrite and corrupt earlier ones into a single merged entry. Detect this by comparing the incoming `id` against the existing entry at the same index; when a new id appears, flush the old entry as a completed tool call before starting a fresh accumulation. Also: - Extract shared MockStreamingClient into http_client::mock - Remove unused final_tool_calls accumulator - Fix inconsistent indentation in tool call delta handling Signed-off-by: Jiawei Zhao <Phoenix500526@163.com>
717eb25 to
9357850
Compare
joshua-mo-143
approved these changes
Feb 26, 2026
Contributor
joshua-mo-143
left a comment
There was a problem hiding this comment.
lgtm - thanks for your hard work @Phoenix500526 !
Merged
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
Some API gateways (e.g. LiteLLM, OneAPI) do not follow the OpenAI streaming convention of assigning each parallel tool call a unique
index. Instead, they emit multiple distinct tool calls all at index 0, distinguishing them only by theiridfield.The previous accumulation logic keyed solely on index, causing later tool calls to overwrite and corrupt earlier ones — merging names and arguments into a single broken entry.
Changes
id: when a new non-emptyidarrives at an index that already holds a differentid, flush the old entry as a completed tool call before starting fresh accumulationtest_duplicate_index_different_id_tool_callsreproduces the exact gateway behavior with two tool calls both at index 0MockStreamingClientintohttp_client::mockto deduplicate test boilerplatefinal_tool_callsaccumulator that was pushed to but never readTest plan
cargo test --lib providers::openai::completion::streaming::tests— all 8 tests pass