Skip to content

fix(streaming): join tool-call fragments that have no index - #474

Open
pcbeingused333 wants to merge 2 commits into
posit-dev:mainfrom
pcbeingused333:fix/merge-lists-unindexed-tool-calls
Open

pcbeingused333 wants to merge 2 commits into
posit-dev:mainfrom
pcbeingused333:fix/merge-lists-unindexed-tool-calls

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

merge_lists merges streamed list items only when they carry an int index (this is how tool_calls fragments are joined in OpenAICompletionsProvider.stream_merge_chunks, and so in every provider built on it). When a server omits index, each argument fragment was appended as its own tool call, so the turn ended up with calls like {"id": "call_1", "function": {"name": "f", "arguments": ""}} followed by several nameless ones with partial JSON. Separately, if an indexed chunk arrived after an element without index, e_left["index"] raised KeyError.

What changed

  • A tool-call item without index, id and function.name is merged into the previous tool call. An item with an id or a name still starts a new call, so parallel calls stay separate.
  • The index comparison uses .get("index") instead of ["index"].

Related: #436 reorders the merge_dicts branches; this change is independent of it.

Tests. Four cases in tests/test_utils_merge.py: fragments joined into one call, two un-indexed calls with different ids kept separate, the KeyError case, and a fragment arriving when the list is still empty. The first and the KeyError case fail on main; the empty-list case pins the guard on the new branch. No request was made to a live API.

This change was made with Claude Code.

🤖 Generated with Claude Code

pcbeingused333 and others added 2 commits October 21, 2026 01:01
merge_lists only merged tool-call chunks by their int `index`. Servers
that omit it left every argument fragment as its own tool call, and an
indexed chunk after an unindexed element raised KeyError.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
# Conflicts:
#	CHANGELOG.md

This branch has not been deployed

No deployments
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.

1 participant