Fix ChatReqLLM streaming tool-call merge for OpenAI-style chunks#551
Merged
Conversation
- tool call bug issue
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.
Problem
Streaming tool calls through
ChatReqLLMagainst OpenAI-compatible backends (direct OpenAI, LiteLLM proxy, and similar) fail at finalize with:The error surfaces only when the model invokes a tool while streaming. Non-streaming calls and Anthropic streaming were unaffected.
The root cause is an index mismatch during
MessageDeltamerging. ReqLLM's default OpenAI decoder emits two kinds of chunks for a single tool call:%StreamChunk{type: :tool_call, name: ..., arguments: %{}, metadata: %{id: ..., index: ...}}chunk.%StreamChunk{type: :meta, metadata: %{tool_call_args: %{index: ..., fragment: "..."}}}argument-fragment chunks.ChatReqLLM.translate_stream_chunk/1was readingidfrom the initial chunk's metadata but droppingindex, and was emitting the call as:completewitharguments: %{}. Fragment chunks then arrived withindex: 0and nocall_id/name. BecauseMessageDelta.merge_tool_calls/2keys lookups byindex, the fragments did not merge into the initial tool call — they landed in a separate slot. At finalize,Message.newranToolCall.complete/1on every entry, and the orphan fragment ToolCall failedvalidate_required([:call_id, :name]).Solution
Added a new
process_stream_chunk/2clause that handles OpenAI-shaped initial:tool_callchunks (those whose metadata carries:index). The clause::indexthrough onto theToolCall, so subsequent fragment chunks merge into the same slot.argumentsis an empty map (placeholder; fragments will follow), emits theToolCallas:incompletewitharguments: nil.ToolCall.append_arguments/2then string-concatenates the incoming JSON fragments, andToolCall.complete/1JSON-decodes the accumulated string at finalize.argumentsis already a non-empty map (single-shot delivery — no fragments will follow), emits theToolCallas:completewith the args inline.The existing Anthropic
%{start: true}clause still matches first, so Anthropic streaming is unchanged. Chunks without:indexin metadata still fall through to the originaltranslate_stream_chunk/1, preserving the previous single-shot behaviour for providers that deliver the whole call in one chunk.Changes
:tool_callchunk clause toprocess_stream_chunk/2; preserves:indexfrom metadata and chooses:incomplete/:completebased on whether args fragments are expected to follow.index+id, followed by twotool_call_argsfragments, then a terminal:meta. Asserts the assembledMessagehas a singleToolCallwith the correctcall_id,name, and JSON-decodedarguments.ToolCallis:completewith the inline args.Testing
mix test test/chat_models/chat_req_llm_test.exs— 85 tests, 0 failures (7 live-tagged tests excluded).mix test— full suite passes: 31 doctests, 1767 tests, 0 failures.StreamChunksequence ReqLLM emits, which is whatChatReqLLMactually consumes.