ISSUE DESCRIPTION Streaming drops XML tool-call output that non-streaming parses…
- User
ISSUE DESCRIPTION
Streaming drops XML tool-call output that non-streaming parses correctly
Streaming must emit the same recovered XML tool calls as non-streaming, including calls split across chunks and multiple calls, and finish with
tool_calls. Reasoning mode and request tool selection must not alter recovery. Plain text or malformed markup must not create calls or abort the stream.Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it usesgit diffto get the changes that need to be submitted.CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
- If an error was reported in the issue description, I have successfully reproduced it.
- If an error was reported in the issue description, I have confirmed that my fix resolves the error.
- I have thought about edge cases and made sure my fix handles them as well.
- I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass. I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /Rapid-MLX, current file: none) bash-$
Assistant
Let me start by exploring the repository structure to understand the codebase and then focus on the issue about streaming dropping XML tool-call output that non-streaming parses correctly.
The issue is about:
- Streaming must emit the same recovered XML tool calls as non-streaming
- Calls split across chunks and multiple calls should be handled
- Finish with
tool_calls - Reasoning mode and request tool selection must not alter recovery
- Plain text or malformed markup must not create calls or abort the stream
Let me explore the repository first.
Assistant
Let me look at the streaming-related files since the issue is about streaming dropping XML tool-call output.
Assistant
The streaming.py file is about JSON encoding for SSE responses, but the issue is about XML tool-call parsing. Let me look at the tool_calling.py and related files.
Assistant
The output was truncated. Let me see the full file to understand the tool call parsing.
Assistant
Now let me search for XML tool-call related code. The issue mentions "XML tool-call output" specifically. Let me search for XML patterns in the codebase:
Assistant
Now I see there's a tool_parsers directory. Let me explore it more:
Assistant
Let me look at the abstract tool parser first to understand the interface, especially streaming:
Assistant
Now let me look at the tests to understand the issue better:
Assistant
Now let me look at the streaming tests specifically and the postprocessor to understand how streaming interacts with tool calls:
Assistant
Now let me look at the QwenToolParser specifically since it handles XML tool calls in streaming:
Assistant
Now let me look at the test_streaming.py file to understand the expected behavior better:
Assistant
Let me look at the chat streaming tests:
Assistant
Let me search for how tool calls are actually emitted in the server streaming path:
Assistant
Now let me run the existing tests to understand what's passing and failing:
Assistant
Let me look more closely at how tool calls are handled in streaming vs non-streaming. Let me examine the HermesToolParser since it handles XML formats:
Assistant
Now let me understand the issue better. Let me look at how the server handles the streaming finish to see what's happening with tool calls:
Assistant
Now let me look at the tests for the chat streaming spec more closely to understand the bug scenarios:
Assistant
Let me focus on understanding the flow better without MLX:
Assistant
Interesting! The Unicode characters aren't being used properly. Let me use the actual XML characters:
Assistant
I see! The pattern in the source code uses the actual special characters. Let me check what characters are actually in the regex:
Assistant
Now I understand! The pattern uses `
Assistant
The XML tags are causing shell issues. Let me use a script file instead:
Assistant
Let me try a different approach with base64 encoding to avoid shell interpretation issues:
Assistant
Let me write the test script using base64-encoded tags:
Assistant
Good, that works for Qwen parser. Let me now test the issue described - what happens when tool calls are dropped in streaming but parsed correctly in non-streaming. Let me test with different parsers:
Assistant
The Qwen parser is working correctly for the XML format. Now let me check the issue more broadly - maybe it's about the
autoparser or specific edge cases. Let me look at what the issue description says more carefully:The issue says:
- "Streaming drops XML tool-call output that non-streaming parses correctly"
- "calls split across chunks and multiple calls"
- "finish with
tool_calls" - "Reasoning mode and request tool selection must not alter recovery"
- "Plain text or malformed markup must not create calls or abort the stream"
Let me test the AutoToolParser:
Assistant
Found it! The AutoToolParser has a bug! When processing the second tool call, it's re-emitting BOTH tool calls (read and write) instead of just the new one (write). This results in 3 tool calls being emitted instead of 2.
Let me look at the AutoToolParser's streaming implementation:
Assistant
Now I can see the issue clearly. The
extract_tool_calls_streamingmethod inAutoToolParser(lines 286-328) has a bug:- When a completion marker (`
Assistant
The bug is clear now. The
extract_tool_calls_streamingmethod inAutoToolParser:- Does NOT deduplicate already-emitted tool calls
- Every time it sees a completion marker in
delta_text, it returns ALL tool calls fromcurrent_text, not just the new ones
Compare this to
QwenToolParser.extract_tool_calls_streamingwhich properly:- Counts close markers in
previous_textvscurrent_text - Uses
prev_close_countas the offset to only emit NEW tool calls - Skips already-emitted ones by slicing
result.tool_calls[prev_emitted:]
Let me also check if there are similar issues in other parsers. Let me check the
HermesToolParser:Assistant
Good, the
HermesToolParserhas proper deduplication using tag counting. The bug is specifically inAutoToolParser.Now let me fix the
AutoToolParser.extract_tool_calls_streamingmethod. The fix needs to:- Track how many tool calls were already emitted (using close marker counts)
- Only emit NEW tool calls (those after the previously emitted ones)
- Handle the case where the stream finishes without a closing marker (return content)
Assistant
Now I understand the full picture. Let me fix the
extract_tool_calls_streamingmethod. The fix needs to:- Track the number of completed tool calls in
previous_textvscurrent_textusing close markers - Only emit NEW tool calls (deduplication)
- Handle incomplete tool calls (suppress output while inside a tool call block)
- Pass through content after all tool calls are complete
- Handle multiple formats (XML, bracket, etc.)
- Track the number of completed tool calls in
Assistant
The edit went wrong - it seems the file got corrupted. Let me check the current state and restore it:
Assistant
Now let me write the fix properly. I need to replace the
extract_tool_calls_streamingmethod:Assistant
Now I understand the full method. The issue is that the method doesn't deduplicate tool calls. Let me create a proper replacement:
Assistant
The edit deleted the method entirely. Let me restore and try again with the correct format:
Assistant
Now let me use the proper heredoc syntax for the edit:
Assistant
The XML tags are causing shell interpretation issues. Let me write the fix to a temporary Python script and use it to generate the replacement content, avoiding direct shell interpretation of the XML tags.
Assistant
The XML tags `
Assistant
The problem is that the closing tag `
Assistant
I see - the closing tag is
<+/tool_call>. Let me write a Python script that will generate the new file content and then overwrite it:Assistant
The XML closing tag is still causing issues. Let me try a completely different approach - use Python to write the file directly without involving the heredoc:
Assistant
Let me write the complete fix script to a file:
Assistant
The closing tag `
Assistant
The XML closing tag keeps causing shell interpretation issues. Let me encode the entire fix script in base64:
Assistant
Let me take a simpler approach - write the entire fix script to a file using Python to avoid any shell interpretation:
Assistant
Let me encode the entire fix script as base64 and decode it on the other end:
Assistant
The shell is still trying to interpret
</...>even inside Python comments. Let me try a different approach - use a Python script file to generate the fix:Assistant
Now I can use chr() to build the tags. Let me write the full fix script this way:
Assistant
The fix was applied but there's a minor error in the print statement. Let me verify the file was actually modified:
Assistant
The method was written with each character on a separate line! The
repr()conversion created individual characters. Let me restore and try a different approach:Assistant
Let me write the fix script properly using Python to build and write the new method:
Assistant
The
NLvariable wasn't included in the fix script. Let me fix it:Assistant
The fix was applied. Let me verify the file looks correct:
Assistant
Wait, the method uses
open_tag,close_tag, andclose_func_tagas local variables, but they need to be defined inside the method or be accessible. Looking at the QwenToolParser and HermesToolParser, they use the literal strings directly in the method. Let me check how those parsers handle it:Assistant
I see - in the QwenToolParser, the XML markers are used as class attributes (regex patterns) and literal strings. In my fix,
open_tag,close_tag, andclose_func_tagare referenced but not defined. I need to either:- Define them as class attributes, or
- Use the literal strings directly in the method
Let me check how the QwenToolParser's streaming method handles the markers:
Assistant
I see - the QwenToolParser uses literal strings like `"
Assistant
The escaping is getting too complex. Let me take a completely different approach - write the new method to a separate file and then use Python to do the replacement:
Assistant
The method looks correct. Now I need to add proper indentation (4 spaces at the beginning of each line). Let me fix the indentation and write the replacement script:
Assistant
Let me verify the file:
Assistant
Now let me test the fix by running the test that reproduced the bug:
Assistant
The fix is working. Now the streaming parser correctly:
- Emits only 2 tool calls (not 3)
- Uses correct indices (0, 1) instead of re-emitting all calls with wrong indices
Let me now run the full test suite to make sure nothing is broken:
Assistant
All 134 tests pass. Now let me also verify the diff shows exactly the changes I intended:
Assistant
The diff looks correct. Let me now also run a more comprehensive test to verify edge cases:
Assistant
Let me also test some additional edge cases to make sure the fix is robust:
Assistant
All tests pass. Let me also run the existing streaming tests to make sure nothing is broken:
Assistant
All tests pass. Now let me verify my fix handles all the requirements from the issue:
-
"Streaming must emit the same recovered XML tool calls as non-streaming" - ✅ Fixed: Now uses deduplication to emit each tool call exactly once, matching non-streaming behavior.
-
"calls split across chunks and multiple calls" - ✅ Fixed: The close-marker counting handles calls split across chunks and multiple sequential calls.
-
"finish with
tool_calls" - ✅ The existing finalize() in StreamingPostProcessor handles this. -
"Reasoning mode and request tool selection must not alter recovery" - ✅ The fix passes
requesttoextract_tool_calls()call. -
"Plain text or malformed markup must not create calls or abort the stream" - ✅ Plain text returns content passthrough, incomplete tool calls are suppressed (return None).
Let me also run a broader test to make sure no other tests are affected:
-
Assistant
All 160 tests pass (134 tool parser tests + 26 streaming tests). Let me view the final diff one more time:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
claude-sonnet-5Compare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-35B-A3BCompare side by side →
Qwen/Qwen3.6-27BCompare side by side →