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 what we're working with. Let's first get an overview.
- Assistant
Let me get a broader view of the repository structure.
- Assistant
This looks like a Python project related to MLX (likely a machine learning framework). Let me explore the vllm_mlx directory since that seems to be the main package.
- Assistant
The issue is about "Streaming drops XML tool-call output that non-streaming parses correctly". This is about tool call parsing in streaming mode. Let me look at the relevant directories - particularly
tool_parsersandrouteswhich likely handle streaming and tool parsing. - Assistant
The issue is about streaming XML tool-call output that non-streaming parses correctly. Let me understand what this means. The issue 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."Let me first understand the architecture - this seems to be about a vLLM-like system for MLX. Let me look at the tool parsers, particularly the XML-based ones and see how streaming vs non-streaming tool call parsing works.
The issue mentions "XML tool-call output" so I need to find which parsers handle XML tool calls.
- Assistant
Let me search for relevant test files to understand what's expected:
- Assistant
Let me look at the relevant test files to understand the issue better. The issue mentions "Streaming drops XML tool-call output that non-streaming parses correctly". This suggests there's a problem where streaming mode doesn't properly parse XML tool calls that non-streaming mode handles correctly.
Let me first look at the test files to understand the expected behavior:
- Assistant
Let me look at the streaming-related tests and the main issue focus:
- Assistant
Let me continue reading this file and also look at the streaming postprocessor.
- Assistant
Let me search for the key areas - the streaming postprocessor and tool call recovery logic.
- Assistant
Let me look at the Qwen tool parser to understand how XML tool calls are handled:
- Assistant
Let me look at the abstract tool parser to understand the base class and the
has_pending_tool_callmethod: - Assistant
Let me understand the issue better. Let me look at the existing tests and try to run them:
- Assistant
Let me continue looking at the test results and see what the issue is about. Let me also look at the test_chat_streaming_spec.py more carefully.
- Assistant
Let me look at how the non-streaming path handles XML tool calls to understand the difference. Let me also look at the chat route code:
- Assistant
Let me look at the streaming tool call test cases to understand what's expected:
- Assistant
Let me search for relevant code about streaming and XML tool calls:
- Assistant
Let me look at the test_chat_streaming_spec.py for the streaming tool parser tests:
- Assistant
Let me look at that test more carefully:
- Assistant
Let me look at what the streaming path does with XML tool calls. Let me check the postprocessor test and related streaming tests:
- Assistant
Let me look at the specific test in test_chat_streaming_spec.py about streaming tool fallback:
- Assistant
Let me look at the
_process_standardmethod in the postprocessor more carefully. I think I see the issue: - Assistant
Let me look at the full
_process_standardmethod more carefully: - Assistant
Let me run the existing tests to see what's failing:
- Assistant
We need to install the mlx library. Let me check if there's a way to install it, or if we can mock it.
- Assistant
Let me look at the streaming tests more carefully to understand what's missing:
- Assistant
Let me look at the chat streaming test more carefully:
- Assistant
Let me check the streaming tool filter test and look for tests related to XML tool calls:
- Assistant
Let me look at more of the test_chat_streaming_spec.py file to understand the issue better:
- Assistant
Let me look at the
_process_with_reasoningmethod to understand the difference: - Assistant
Let me check the git history for recent changes to understand the issue:
- Assistant
Let me look at the recent fix about streaming:
- Assistant
Let me look at the current git diff to understand what's been changed:
- Assistant
Let me run the existing tests to see what's failing:
- Assistant
Let me run only the tests that work without MLX:
- Assistant
- Assistant
Let me create a test script to reproduce the issue:' result = parser.extract_tool_calls(text) print(f" tools_called={result.tools_called}") print(f" tool_calls={result.tool_calls}") print(f" content={result.content}") assert result.tools_called assert len(result.tool_calls) == 1 assert result.tool_calls[0]["name"] == "get_weather" print(" ✓ Non-streaming works")
Now test streaming
print("\n" + "=" * 60) print("TEST: Streaming XML tool call extraction") cfg = _make_cfg() parser2 = QwenToolParser() cfg.tool_parser_instance = parser2
pp = StreamingPostProcessor(cfg, tools_requested=True) pp.reset()
Simulate streaming in small chunks
chunks = [ "", ]
all_events = [] accumulated = "" for i, chunk in enumerate(chunks): accumulated += chunk is_last = i == len(chunks) - 1 output = _make_output(text=chunk, finished=is_last) events = pp.process_chunk(output) all_events.extend(events) print(f" Chunk {i}: delta='{chunk}', events={[(e.type, getattr(e, 'content', None)) for e in events]}")
- Assistant
I apologize for the formatting issue. Let me use the bash_terminal tool properly.' result = parser.extract_tool_calls(text) print(f" tools_called={result.tools_called}") print(f" tool_calls={result.tool_calls}") assert result.tools_called assert len(result.tool_calls) == 1 print(" PASSED")
print("\n" + "=" * 60) print("TEST 2: Streaming XML tool call (single chunk)") cfg = _make_cfg() parser2 = QwenToolParser() cfg.tool_parser_instance = parser2 pp = StreamingPostProcessor(cfg, tools_requested=True) pp.reset()
- Assistant
- Assistant
I need to use the bash_terminal tool correctly. Let me try with a simpler command first.
- Assistant
- Assistant
- Assistant
The XML tags in my commands are being interpreted as tool calls. Let me write the script in a way that constructs the XML strings programmatically.
- Assistant
Now I can see the issue. Multiple XML tool calls fail. Let me investigate what format the tests are using and what the non-streaming parser supports for the Qwen3 XML format:
- Assistant
I need to avoid XML-like tags in my responses. Let me use the
createcommand to write the test file. - Assistant
Good - the standard Qwen XML format works fine. Let me check what format is specifically failing. Let me look at the Qwen3Coder parser:
- Assistant
Let me look at the hermes tool parser:
- Assistant
Let me understand the issue better. Let me test the HermesToolParser and see what happens:
- Assistant
I need to stop generating XML-like content in my commands. Let me use the
createcommand to write a test file. - Assistant
All basic tests pass. Let me now test the Qwen3Coder parser which uses a different XML format (with
<function=name>tags), and also test token-by-token streaming which is a critical edge case: - Assistant
Found a bug! The Qwen3Coder parser fails when multiple tool calls are sent as separate chunks. Let me debug:
- Assistant
The Qwen3Coder parser loses the second tool call. Let me look at the exact state flow more closely:
- Assistant
I found the bug! The Qwen3CoderToolParser's streaming method never advances
current_tool_indexafter a tool call completes. Let me look at the code: - Assistant
Let me see the full streaming method to understand the flow:
- Assistant
Now I understand the bug! When the Qwen3Coder parser completes the first tool call and then encounters the second tool call as the next chunk:
- The "advance check" block at line 264 detects that
json_closed=Trueandin_function=False - It advances
current_tool_indexto 1 and resets state - But it returns None immediately, skipping processing of the next tool call
- The second tool call never gets emitted
Additionally, the
finalize()in the postprocessor skips the fallback becausetool_calls_detected=True(from the first call).Let me also check the Hermes bare function case and the Qwen tool parser for similar issues:
- The "advance check" block at line 264 detects that
- Assistant
Now I can see the issue clearly. The Qwen3CoderToolParser's streaming method has an "advance check" block that returns
Nonewhen it advances to the next tool call. This causes the second tool call to be dropped because:- The advance block fires, increments
current_tool_index, resets state, returnsNone - There's no next delta to process the second tool call
finalize()skips its fallback becausetool_calls_detected=Truefrom the first call
Let me now look at the existing tests to see what tests pass/fail:
- The advance block fires, increments
- Assistant
Let me run only the tool parser tests and postprocessor tests:
- Assistant
Now I understand the full picture. There are two issues:
- Qwen3CoderToolParser: The streaming method's "advance check" block returns
Nonewhen advancing to the next tool call, dropping subsequent tool calls - PostProcessor.finalize(): The
not self.tool_calls_detectedcondition prevents the fallback from recovering missed calls when some calls were already detected
Let me fix both issues. First the Qwen3CoderToolParser:
- Qwen3CoderToolParser: The streaming method's "advance check" block returns
- Assistant
Let me see the exact code around line 277:
- Assistant
Now I can see the exact issue. Line 277 has
return Nonein the advance block. After fixing the Qwen3Coder parser, I also need to fix the PostProcessor's finalize to handle partial streaming detection. Let me implement the fix: - Assistant
Let me verify the Qwen3Coder fix:
- Assistant
The Qwen3Coder fix works. Now I need to also fix the PostProcessor's
finalize()method to handle cases where the streaming parser catches some but not all tool calls. Let me look at the finalize code more carefully and implement the fix: - Assistant
Let me see the full finalize method:
- Assistant
Now I need to fix the PostProcessor's
finalize()method to also catch tool calls missed during streaming even when some were already detected. I'll track the count of emitted tool calls: - Assistant
Now I need to update
_detect_tool_callsto track emitted count, and fixfinalizeto handle partial detection: - Assistant
Let me check what's happening:
- Assistant
I see a duplicate. Let me fix that and also update the
_detect_tool_callsmethod: - Assistant
Now let me fix the
finalizemethod to run the fallback even when some tool calls were detected, but only emit the additional ones: - Assistant
Let me verify all the fixes work:
- Assistant
All Qwen3Coder tests pass now. Let me also run the first test suite:
- Assistant
All custom tests pass. Now let me run the existing test suite to make sure nothing is broken:
- Assistant
All 225 existing tests pass. Let me also run the streaming spec tests:
- Assistant
That test requires the MLX native library. Let me also run other relevant tests:
- Assistant
All tests pass. Let me now write a comprehensive edge-case test to validate the fix more thoroughly:
- Assistant
All comprehensive tests pass. Let me review the changes I've made:
- Assistant
Let me see the actual diff:
- Assistant
The diff looks clean and focused. Let me run all the tests one final time to make sure everything passes:
- Assistant
All 432 tests pass. Let me clean up temporary files and do a final review:
- Assistant
Let me do a final review of the changes:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.