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
I'll start by exploring the repository structure to understand the codebase and then investigate the issue about streaming dropping XML tool-call output.
Assistant
The issue is about streaming dropping XML tool-call output. Let me look at the tool_parsers directory and related files to understand how tool calls are parsed in streaming vs non-streaming modes.
Assistant
Let me look at the tests to understand the issue better, especially any tests related to streaming and tool calls.
Assistant
Let me look at the test files for tool parsers and streaming to understand the issue better.
Assistant
Let me look at the test_tool_parsers.py to understand what tests exist and what's expected for streaming vs non-streaming behavior.
Assistant
Now let me look at the abstract_tool_parser.py which seems to be the base class for tool parsing:
Assistant
Now let me look at the QwenToolParser since the tests show XML format tool calls:
Assistant
Now let me search for how streaming handles tool calls in the broader codebase. Let me look at the output_router and streaming related files:
Assistant
Now I have a better understanding of the issue. Let me look at the test_streaming.py and other test files to understand the failing behavior:
Assistant
Let me look at the chat streaming spec test to understand the issue better.
Assistant
Let me look at more test files related to streaming with XML tool calls:
Assistant
Let me run the existing tests to see what's failing and understand the issue better:
Assistant
Assistant
Let me look at the AutoToolParser to understand how it handles different formats in streaming vs non-streaming modes.
Assistant
Looking at this code, I can see the issue. The
AutoToolParser.extract_tool_calls_streamingdoesn't deduplicate already-emitted tool calls. When a second tool call is completed, it emits ALL tool calls found in the current text (including the first one that was already emitted). This causes the OpenAI streaming client to merge by index and concatenate names/arguments.Let me check more parsers to understand the scope of the issue:
Assistant
Let me look at a few more tool parsers to understand the streaming vs non-streaming discrepancy:
Assistant
Looking at this more carefully, I need to understand the complete streaming flow. Let me search for where finalize is called and how the streaming works end-to-end.
Assistant
Assistant
The MLX library is not available in this environment, so I can't run tests that require it. Let me focus on understanding the issue from the code and tests.
Let me create a simple test script that doesn't require MLX to reproduce the issue:
Assistant
Let me create a test script file:
Assistant
Looking at the issue, I need to understand what's being reported. The QwenToolParser streaming tests pass, which means the deduplication is working. But the issue says "Streaming drops XML tool-call output that non-streaming parses correctly."
Assistant
I need to fix my command - the shell is having issues with the special characters. Let me write a proper test script:
Assistant
Let me write the test script without using heredoc syntax that might conflict:
Assistant
Good, the parser works. Now let me look at what specific issue is being described. Let me analyze the code flow more carefully and understand what "drops XML tool-call output" means.
Looking at the
extract_tool_calls_streaminginQwenToolParser, I see it properly deduplicates. Let me check if there's a case where the streaming code path emits content instead of tool calls when it should emit tool calls.Assistant
Looking at this, line 140 in
qwen_tool_parser.pyreturns content when there's no tool marker. Let me look at the full context ofextract_tool_calls_streamingin QwenToolParser:Assistant
Let me continue investigating the issue. I need to understand what the actual problem is with streaming dropping XML tool-call output.
Looking at the code so far, I've seen:
- QwenToolParser has proper deduplication in its streaming implementation
- HermesToolParser also has proper deduplication
- AutoToolParser's streaming implementation doesn't have deduplication
The issue description says "Streaming drops XML tool-call output that non-streaming parses correctly". This suggests that there's a discrepancy between streaming and non-streaming parsing.
Assistant
Looking at the AutoToolParser's streaming implementation, I see it doesn't have proper deduplication. Let me look at the streaming method more carefully.
Assistant
Assistant
The system is telling me I need to invoke a tool call. Let me use the bash_terminal tool properly.
Assistant
Let me look at the test cases for streaming to understand what the expected behavior is:
Assistant
I see - the tests are for QwenToolParser and they verify proper deduplication. Now let me understand the actual issue better.
Looking at the tests, they test
QwenToolParserwhich already has proper deduplication. But the issue says "Streaming drops XML tool-call output that non-streaming parses correctly".Let me think about what "drops" means here. The issue might be that the AutoToolParser (which is the default parser) doesn't properly handle all XML formats in streaming mode.
Let me look at the AutoToolParser streaming implementation again:
- It checks for markers like "
<tool_call> <function=bash_terminal> <parameter=command> cd /Rapid-MLX && git log --oneline -20 </tool_call>
Assistant
I need to properly use the bash_terminal tool. Let me execute git log to understand the recent commits and what the issue might be about.
Assistant
The git log shows recent commits related to streaming and tool calls. The most relevant one is:
6ee83bd fix(stream): non-guided path created+usage spec; drop pending-gate on tool fallback (#424)
This seems related to the issue. Let me look at the current state of the code and understand what needs to be fixed.
The issue description says: "Streaming drops XML tool-call output that non-streaming parses correctly"
Let me look at the AutoToolParser's streaming implementation more carefully. I already identified that it doesn't have proper deduplication. Let me check if this is the issue that needs to be fixed.
Actually, looking at the tests, the QwenToolParser has proper deduplication tests. But the AutoToolParser doesn't have similar tests. Let me check if AutoToolParser needs to be fixed.
Let me also look at what other parsers might have the same issue.
Assistant
Now I understand the context. The commit
6ee83bdfixed Bug A (streaming tool-parser coverage gap) by dropping thehas_pending_tool_callgate infinalize(). This ensures that when streaming misses tool calls, the non-streamingextract_tool_callsis called as a fallback at end-of-stream.However, the issue description says:
- "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."
This suggests that the issue is about parity between streaming and non-streaming - not just about the fallback. The streaming parser itself should properly handle:
- Calls split across chunks
- Multiple calls (with proper deduplication)
- Finish with
tool_callsfinish reason
Let me check the AutoToolParser's streaming implementation more carefully. It seems like it doesn't have proper deduplication for multiple tool calls. Let me verify this is the actual issue by looking at the current code.
- "Streaming must emit the same recovered XML tool calls as non-streaming, including calls split across chunks and multiple calls, and finish with
Assistant
Looking at the issue, I need to understand exactly what needs to be fixed. Let me analyze the current state of the codebase:
- The
finalize()method inpostprocessor.pynow properly falls back toextract_tool_callswhen streaming misses tool calls (fixed by commit6ee83bd) QwenToolParserandHermesToolParserhave proper deduplication in their streaming implementationsAutoToolParserdoes NOT have proper deduplication in its streaming implementation
The issue says "Streaming drops XML tool-call output that non-streaming parses correctly". This could mean:
- The AutoToolParser streaming doesn't emit multiple tool calls correctly (it duplicates instead of deduplicating)
- OR there's a specific XML format that streaming doesn't recognize
Let me check if there's a test that demonstrates the AutoToolParser issue:
- The
Assistant
Now I understand the issue. The
AutoToolParser.extract_tool_calls_streamingmethod doesn't properly deduplicate tool calls when there are multiple calls. It emits ALL tool calls each time a closing marker is detected, starting indices from 0 every time. This causes the OpenAI client to merge by index and concatenate names/arguments.Let me fix this by adding proper deduplication logic similar to
QwenToolParser:Assistant
Now I understand the issue. The
AutoToolParser.extract_tool_calls_streamingmethod doesn't have proper deduplication. When there are multiple tool calls, it emits ALL tool calls each time a closing marker appears, starting indices from 0 every time. This causes the OpenAI streaming client to merge by index and concatenate names/arguments.The fix is to add proper deduplication logic similar to
QwenToolParser.extract_tool_calls_streaming. The key is to:- Count closing markers in previous vs current text to detect new completions
- Only emit NEW tool calls that weren't emitted before
- Use proper indices based on previously emitted count
Let me look at the full streaming method and fix it.
Assistant
Now I understand the issue. The
AutoToolParser.extract_tool_calls_streamingmethod doesn't properly deduplicate tool calls. When multiple tool calls are present, it emits ALL tool calls each time a new closing marker appears, starting indices from 0. This causes the OpenAI streaming client to merge by index and concatenate names/arguments.Let me fix this by adding proper deduplication logic similar to
QwenToolParser.extract_tool_calls_streaming:Assistant
Now I understand the issue clearly. The
AutoToolParser.extract_tool_calls_streamingmethod doesn't deduplicate - it emits ALL tool calls each time a new closing marker appears. Let me fix this by adding proper deduplication similar toQwenToolParser:Assistant
Let me write the fix script carefully, avoiding the special XML-like characters that caused issues:
Assistant
Now let me see the full method and create the fix:
Assistant
I'll create a Python script to fix the AutoToolParser's streaming method:
Assistant
Let me now write the fix to the AutoToolParser's streaming method to add proper deduplication. I'll need to carefully construct the replacement code without using special characters that might cause issues.
Assistant
The shell is having issues with special characters in heredoc. Let me write a Python script file first, then execute it.
Assistant
I see the issue - I need to use the actual XML marker characters correctly. Let me check what the actual marker is and create the proper fix.
Assistant
Now I understand - the markers are fullwidth less-than and greater-than characters. Let me check what the actual code uses:
Assistant
I see - the pattern uses what looks like special characters but they're actually the fullwidth less-than and greater-than sign characters. Let me get the actual bytes:
Assistant
Now I see the actual marker character used. Let me look at the AutoToolParser to see what marker it uses:
Assistant
Now I can see the marker characters are actually used in the code. Let me create the fix properly. The issue is that AutoToolParser doesn't deduplicate tool calls - it emits all of them each time a closing marker is seen.
Assistant
Now I understand the issue. The
AutoToolParser.extract_tool_calls_streamingmethod has two problems:-
No deduplication: It emits ALL tool calls each time a closing marker appears, starting indices from 0. This causes the OpenAI streaming client to merge by index and concatenate names/arguments.
-
Only checks for closing markers in delta_text: It only emits tool calls when a closing marker appears in the current delta, not when the accumulated text completes a tool call.
Let me fix this by adding proper deduplication similar to
QwenToolParser:-
Assistant
Let me check the current state of the file and verify the fix was applied correctly:
Assistant
Now let me verify my fix is correct by running the existing tests. I also need to check if there are other tool parsers that might have the same deduplication issue.
Let me:
- Run the existing tests to make sure I didn't break anything
- Check if there are similar issues in other tool parsers
- Verify the fix handles edge cases properly
Assistant
Good, the AutoToolParser tests pass. Let me run all the tool parser tests to make sure nothing is broken:
Assistant
All 134 tests pass. Now let me create a quick test to verify my fix works correctly for the AutoToolParser's multi-tool deduplication:
Assistant
The AutoToolParser fix is working. Now let me verify a few more edge cases and check if other tool parsers might have similar issues:
Assistant
The AutoToolParser fix is working correctly. Now let me check if there are any other tool parsers that might have similar deduplication issues. Let me also make sure I haven't missed anything from the issue description:
- "Streaming must emit the same recovered XML tool calls as non-streaming" - ✅ Fixed with deduplication
- "including calls split across chunks and multiple calls" - ✅ Fixed with deduplication
- "finish with
tool_calls" - This is handled by the postprocessor's finalize() method - "Reasoning mode and request tool selection must not alter recovery" - Need to check if request is passed correctly
- "Plain text or malformed markup must not create calls or abort the stream" - ✅ Tested
Let me verify the request parameter is passed correctly in my fix.
Assistant
The request is being passed through. Now let me run all the tool parser tests again to make sure everything still works, and then check if there are any other parsers that might have similar issues:
Assistant
All 134 tests pass. Now let me run a few more tests to make sure I haven't broken anything else in the repository, particularly related to streaming and tool calls:
Assistant
Let me clean up the backup file I created and run a final comprehensive test to verify everything is working:
Assistant
The previous command executed successfully (exit code 0), and the system is now waiting for my next command. I need to continue verifying my fix is complete. Let me check the diff of my changes to make sure everything looks good before submitting.
Assistant
Let me verify the marker character is correct in my fix:
Assistant
Wait, I need to check if the marker character I used is correct. Let me check what the Qwen parser uses vs what I put in the AutoToolParser.
The Qwen parser uses bytes
b'"\xef\xbc\x9c"'for the opening marker andb'"\xef\xbc\x9e"'for the closing marker.Let me check what I have in the AutoToolParser fix...
Actually, looking at the output:
- Qwen has_tool_marker bytes:
b'has_tool_marker = (\n "<tool_call>" in current_text or "[Calling tool:" in current_text\n ' - Auto marker bytes:
b' "\xef\xbc\x9c", # Qwen XML opening mar'
The bytes
\xef\xbc\x9cis the UTF-8 encoding of the fullwidth less-than sign<(U+FF1C).But wait, looking at the Qwen has_tool_marker, it seems to use
"\xef\xbc\x9c"as well since it shows in the print but the bytes would be different.- Qwen has_tool_marker bytes:
Assistant
Hmm, my regex search is finding the wrong strings. Let me check more carefully what the actual markers are:
Assistant
I see the issue! The Qwen parser uses `
Assistant
The shell is having issues with the special characters in my command. Let me write a Python script file instead to test the markers properly.
Assistant
I need to create the test script first, then run it. Let me use a simple echo command to create the file.
Assistant
I see the issue now! The Qwen parser uses `
Assistant
I see! The
QWEN_XML_PATTERNinAutoToolParseruses `Assistant
The shell had an issue with the sed command due to the special characters. Let me use Python to fix the marker character in the AutoToolParser.
Assistant
Wait, that's not right. The "correct" marker I found is
<tool_call>which is3c746f6f6c5f63616c6c3e- that's the ASCII string<tool_call>, not the special characters `<tool_call> <function=bash_terminal> <parameter=command> cd /Rapid-MLX && python3 -c "
Let me check what the Qwen parser's XML_PATTERN actually matches
import re from vllm_mlx.tool_parsers.qwen_tool_parser import QwenToolParser
Print the pattern
pattern = QwenToolParser.XML_PATTERN.pattern print('XML_PATTERN pattern:', repr(pattern))
Try to match a sample tool call
sample = ' </tool_call>
Assistant
I see the issue now. The marker I used was incorrect. Let me check what the actual marker character should be by examining the Qwen parser's XML pattern more carefully.
Assistant
Now I understand! The Qwen parser's XML_PATTERN uses the literal string `
<tool_call> <function=bash_terminal> <parameter=command> cd /Rapid-MLX && python3 -c "
The Qwen parser uses <tool_call> (literal string) as the opening marker
and
Assistant
The previous command was terminated early because the shell was waiting for more input due to an unmatched quote. Let me continue working on the fix by checking what marker character the Qwen parser actually uses and ensuring the AutoToolParser uses the same one.
I need to verify what the correct marker character is. The Qwen parser uses the literal XML tag string `
Assistant
I see the issue now. The Qwen parser uses the literal string `
<tool_call> <function=bash_terminal> <parameter=command> cd /Rapid-MLX && python3 -c "
Read current auto_tool_parser.py and fix the marker
with open('vllm_mlx/tool_parsers/auto_tool_parser.py', 'rb') as f: auto_bytes = f.read()
Find the markers section
idx = auto_bytes.find(b'markers = [') if idx >= 0: snippet = auto_bytes[idx:idx+200] print('Auto markers snippet (bytes):') print(snippet) print()
The correct marker is the literal string: <tool_call> (less-than tool_call greater-than)
correct_marker = '<tool_call>' print('Correct marker:', repr(correct_marker)) print('Correct marker bytes:', correct_marker.encode('utf-8').hex())
Check what's currently in the file
import re m = re.search(rb'"([^"]+Qwen[^"]+)"', auto_bytes) if m: print() print('Found marker line bytes:', m.group(0)) " </tool_call>
Assistant
The previous command seems to have not completed properly. Let me re-run the investigation to understand the marker character issue.
Looking at the previous outputs:
- Qwen has_tool_marker uses the literal string
<tool_call>which is bytes3c746f6f6c5f63616c6c3e - My fix used
<(U+FF1C, fullwidth less-than) which is bytesefbc9c
So I need to fix the AutoToolParser to use the correct marker `
<tool_call> <function=bash_terminal> <parameter=command> cd /Rapid-MLX && python3 -c "
Check what the current auto_tool_parser.py has
with open('vllm_mlx/tool_parsers/auto_tool_parser.py', 'r') as f: content = f.read()
Find the markers section
import re m = re.search(r'markers = [[^]]+]', content, re.DOTALL) if m: print('Current markers list:') print(m.group(0)) " </tool_call>
- Qwen has_tool_marker uses the literal string
Assistant
Let me try a simpler approach to check and fix the marker character in the AutoToolParser:
Assistant
Let me check what the current marker looks like in the auto_tool_parser.py file and fix it if needed.
Assistant
The marker in the auto_tool_parser.py now shows `"
<tool_call> <parameter=command> cd /Rapid-MLX && python3 -c "
Verify the marker character is correct now
with open('vllm_mlx/tool_parsers/auto_tool_parser.py', 'rb') as f: auto_bytes = f.read()
Find the marker line
import re m = re.search(rb'Qwen XML opening marker', auto_bytes) if m: # Get the surrounding context start = max(0, m.start() - 30) end = min(len(auto_bytes), m.end() + 10) snippet = auto_bytes[start:end] print('Marker line bytes:', snippet) print()
# Extract the marker string between quotes m2 = re.search(rb'\"([^\"]+)\"', auto_bytes[m.start()-30:m.start()+50]) if m2: marker_bytes = m2.group(1) print('Marker bytes:', marker_bytes.hex()) print('Marker as string:', repr(marker_bytes.decode('utf-8'))) # Compare with Qwen parser's marker with open('vllm_mlx/tool_parsers/qwen_tool_parser.py', 'rb') as f: qwen_bytes = f.read() m3 = re.search(rb'has_tool_marker.*?\"([^\"]+)\"', qwen_bytes, re.DOTALL) if m3: qwen_marker_bytes = m3.group(1) print() print('Qwen marker bytes:', qwen_marker_bytes.hex()) print('Qwen marker as string:', repr(qwen_marker_bytes.decode('utf-8'))) print() print('Markers match:', marker_bytes == qwen_marker_bytes)Assistant
The previous command had an issue - it seems like the output got truncated. Let me run a simpler verification command to check if the marker character in auto_tool_parser.py is correct.
Assistant
The previous output shows that the Auto marker is showing '[Calling tool:' which means my regex didn't find the right marker. Let me check what the actual marker is in the auto_tool_parser.py file more carefully.
Looking at the earlier sed output, the line 336 shows:
" <tool_call> </parameter> </function> </tool_call>Assistant
Looking at the sed output, the marker on line 336 shows `"
Assistant
The shell is having issues with the special XML-like characters in my command. Let me write a test script to a file instead and run it.
Assistant
It seems the shell is having trouble with the XML-like characters in my heredoc. Let me try a different approach - I'll use Python to write the test file, avoiding the problematic characters in the shell.
Assistant
The test script was successfully written to /tmp/test_fix.py. Now I need to run it to verify that my fix to the AutoToolParser's streaming deduplication is working correctly with the proper XML marker characters.
Assistant
The fix is working correctly. Let me run all the tool parser tests one more time to confirm everything passes, then I'll submit my solution.
Assistant
All 134 tool parser tests pass. Let me review my changes one more time and then submit my solution.
Assistant
Looking at the diff, I can see my changes to the AutoToolParser's
extract_tool_calls_streamingmethod. The key changes are:-
Added proper deduplication: Instead of emitting ALL tool calls each time a closing marker appears (which caused the OpenAI streaming client to merge by index and concatenate names/arguments), the fix now:
- Parses tool calls from
previous_textto count how many were already emitted - Parses tool calls from
current_textto get all completed tool calls - Only emits the NEW tool calls (slicing from
prev_emittedindex) - Uses proper indices starting from
prev_emitted + i
- Parses tool calls from
-
Added a comment to the XML marker: Added comment for clarity (the actual character is the same which is the fullwidth less-than sign).
-
Assistant
You're right, I need to properly invoke the tool call. Let me check the status of the tests that can run without MLX and verify my fix is complete.
Assistant
All tests pass. Let me now summarize what I've done and submit my solution.
Summary of Changes
I fixed the
AutoToolParser.extract_tool_calls_streamingmethod in/Rapid-MLX/vllm_mlx/tool_parsers/auto_tool_parser.pyto properly deduplicate tool calls during streaming.The Problem
The original implementation emitted ALL tool calls each time a closing marker appeared in the delta text, starting indices from 0 every time. This caused the OpenAI streaming client to merge by index and concatenate names/arguments (e.g., "readread" instead of separate "read" and "write" calls).
The Solution
I modified the streaming parser to:
- Parse tool calls from
previous_textto count how many were already emitted - Parse tool calls from
current_textto get all completed tool calls - Only emit NEW tool calls (those not in previous text)
- Use proper indices starting from
prev_emitted + i
This approach is consistent with how
QwenToolParserandHermesToolParserhandle streaming deduplication.Testing
- Parse tool calls from
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.