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 to understand the codebase and locate relevant files related to streaming and XML tool-call parsing. Let's first check the overall structure.
- Assistant
Let me explore the repo more broadly first.
- Assistant
Let me look for files related to streaming and XML tool-call parsing.
- Assistant
Let me look at the vllm_mlx directory structure and find streaming-related files.
- Assistant
Let me explore the repository structure more carefully, especially the streaming and tool call related code.
- Assistant
Let me look at the tool parsers directory and the streaming-related code.
- Assistant
Let me look at the test files related to streaming and tool calls to understand the expected behavior.
- Assistant
Let me look at the key test files to understand the issue better.
- Assistant
This is a streaming tool call filter. But the issue is about "Streaming drops XML tool-call output that non-streaming parses correctly". So the issue might be that when streaming, the XML tool calls are being dropped (filtered out) instead of being parsed and emitted as tool_calls in the response.
Let me look at the broader context - how the streaming and non-streaming paths work.
- Assistant
Let me look at the StreamingToolCallFilter class:
- Assistant
Let me read the StreamingToolCallFilter class.
- Assistant
Let me check what
_TOOL_CALL_TAGSare defined: - Assistant
Let me see the full definition of _TOOL_CALL_TAGS
- Assistant
Let me look at how streaming and non-streaming handle tool calls. Let me first check the tool parsers:
- Assistant
Let me look at the server.py to understand how streaming and tool calls are handled:
- Assistant
Let me look at the routes directory to understand how streaming is handled:
- Assistant
Let me search for the streaming logic in the chat route:
- Assistant
Let me look at the postprocessor.py:
- Assistant
Let me look at the test files to understand the expected behavior better:
- Assistant
Let me look at the tests that might be related to the issue:
- Assistant
Let me look at some of the test files to understand what the expected behavior should be:
- Assistant
Let me look at the chat route to understand how streaming works with tool calls:
- Assistant
Let me look at the actual streaming handler to understand how tool calls are processed. Let me search for the
stream_chat_completionfunction. - Assistant
Let me look at the
stream_chat_completionfunction: - Assistant
Now let me look at the tool_calling module to understand the non-streaming path:
- Assistant
Let me look at the Anthropic route to understand how StreamingToolCallFilter is used there:
- Assistant
Let me run the existing tests to understand what's currently working and what's not:
- Assistant
Let me look at the test_postprocessor.py file to understand the issue better:
- Assistant
Let me look at the test_tool_call_e2e.py file:
- Assistant
Let me look for specific failing tests or test files that might be related to this issue:
- Assistant
Let me run the test suites related to tool calling to see if there are any failures:
- Assistant
Let me look at the specific test for streaming XML tool calls to understand what's expected:
- Assistant
Let me search for tests related to the streaming vs non-streaming XML tool-call behavior:
- Assistant
Let me look at the test_chat_streaming_spec.py file:
- Assistant
Let me run the tests from test_chat_streaming_spec.py to see what's currently working:
- Assistant
Let me check the git log to understand the repository history and what changes might be related:
- Assistant
Let me look at the most recent commit to see what changes were made:
- Assistant
Let me look at what the current state of the code is more carefully and check for any failing tests:
- Assistant
Let me run all the relevant test files to see what currently passes/fails:
- Assistant
The streaming_pipeline_integration test requires mlx which is not available. Let me run the other tests separately.
- Assistant
All existing tests pass. Let me understand the issue better. Let me look at how the Anthropic streaming path handles tool calls vs the OpenAI path:
- Assistant
- Assistant
- Assistant
Let me create a proper test script to reproduce the issue:
- Assistant
Interesting! Let me debug this further to understand why finalize doesn't detect the tool calls:
- Assistant
Let me debug further to understand what's happening with the tool call text:' ]
all_events = [] for i, chunk in enumerate(chunks): output = MagicMock() output.new_text = chunk output.finished = (i == len(chunks) - 1) output.channel = None output.finish_reason = "stop" if output.finished else None output.prompt_tokens = 10 output.completion_tokens = i + 1 output.tokens = [] output.logprobs = None
events = processor.process_chunk(output) print(f"\nChunk {i}: {chunk!r:.60}") print(f" accumulated: {processor.tool_accumulated_text!r:.80}") print(f" tool_calls_detected: {processor.tool_calls_detected}") print(f" events: {[(e.type, e.finish_reason) for e in events]}") all_events.extend(events)print(f"\nAfter streaming:") print(f" tool_accumulated_text: {processor.tool_accumulated_text!r:.100}") print(f" tool_calls_detected: {processor.tool_calls_detected}")
finalize_events = processor.finalize() print(f"\nFinalize events: {[(e.type, e.finish_reason) for e in finalize_events]}")
Check final state
finish_events = [e for e in all_events if e.type == "finish"] tool_events_in_stream = [e for e in all_events if e.type == "tool_call"] tool_events_in_finalize = [e for e in finalize_events if e.type == "tool_call"]
- Assistant
Let me create a proper test file:
- Assistant
I see - the XML_TOOL_CALL is empty. Let me fix the test:
- Assistant
Now I can see the bug clearly. Let me also verify the real streaming scenario with XML tool calls split across chunks:
- Assistant
Now I can see the bug clearly! When the
StreamingPostProcessor.finalize()catches tool calls that the streaming parser missed, thestream_chat_completionfunction has already yielded afinish_reason="stop"chunk to the client. Then it yields tool_call chunks withfinish_reason="tool_calls"- creating two conflicting finish reasons.Let me now look at the
stream_chat_completioncode flow more carefully: - Assistant
Now I understand the bug. Let me trace through the exact flow:
- Streaming: When engine finishes,
process_chunkreturns afinishevent withfinish_reason="stop"(because streaming parser didn't detect tool calls) - chat.py immediately yields this finish chunk to the client
- After stream loop:
finalize()runs and detects tool calls, returnstool_callevent - chat.py yields the tool_call chunk with
finish_reason="tool_calls"
The client gets two conflicting finish reasons: first "stop", then "tool_calls".
The fix needs to delay the finish event until after
finalize()runs. Let me implement this: - Streaming: When engine finishes,
- Assistant
Now let me see the rest of the function after finalize:
- Assistant
Now I have a complete understanding of the bug. Let me implement the fix. The key issue is in
stream_chat_completion:process_chunkreturns afinishevent withfinish_reason="stop"(streaming parser missed tool calls)- This is immediately yielded to the client
- Then
finalize()catches the tool call, yieldingtool_callwithfinish_reason="tool_calls"
The client gets two conflicting finish reasons. The fix: delay the finish event until after
finalize()runs. - Assistant
Now I have a clear understanding. Let me implement the fix:
- Assistant
Now I need to update the finalize section and add the deferred finish emission:
- Assistant
Now I need to modify the finalize section to handle the deferred finish event:
- Assistant
Now I need to also check if the guided streaming path has the same bug. Let me also look at the
stream_chat_completion_guidedfunction: - Assistant
Now let me verify my fix works by running the debug test again:
- Assistant
The fix logic works correctly. Now let me also check if there's a related issue with the
stream_chat_completion_guidedpath: - Assistant
Good - the guided streaming path is a different flow (buffered generation). Now let me also look at the
accumulated_textfield. I noticed it was empty in my debug test. Let me check if it should be populated: - Assistant
Let me look at how
accumulated_textis populated in the reasoning path: - Assistant
Good. Now let me also check how
accumulated_textis used in the standard path (no reasoning parser) vs the reasoning path. In the standard path,accumulated_textis never set, buttool_accumulated_textIS set via_detect_tool_calls. Thefinalize()method usesself.tool_accumulated_text or self.accumulated_text, so it correctly falls back totool_accumulated_text.Now let me run the existing tests to verify nothing is broken:
- Assistant
All 225 tests pass. Now let me also check the Anthropic streaming path to see if it has a similar issue:
- Assistant
Good - the Anthropic path correctly delays
stop_reasonuntil after tool call parsing (line 616:stop_reason = "tool_use" if tool_calls else "end_turn"). The Anthropic path doesn't have the same bug.Now let me run a broader set of tests to make sure nothing is broken:
- Assistant
The guided test requires mlx (not available in this env). Let me also verify the guided streaming path:
- Assistant
All 225 tests pass. Now let me check the
stream_chat_completion_guidedfunction for the same issue (it has its own finish event logic): - Assistant
The guided streaming path uses buffered generation (single
GenerationOutput), so it doesn't have the same issue. The fix is only needed instream_chat_completion.Now let me run a comprehensive verification with all edge cases:
- Assistant
The test 5 has an issue with the heredoc mangling the `
- Assistant
All tests pass. Let me run the full suite of tests that don't depend on the mlx native library:
- Assistant
Some tests depend on mlx. Let me run only the tests that work:
- Assistant
All 240 tests pass. Let me also check the
stream_chat_completion_guidedpath since it falls back tostream_chat_completion: - Assistant
Good. Now let me also check whether the
stream_chat_completion_guidedneeds the same fix: - Assistant
Good - the guided path falls back to
stream_chat_completionwhich already has the fix. Now let me verify the diff: - Assistant
The diff looks clean. Let me also check for a potential edge case - what if the
stream_chat_completionis called without any output (empty stream): - Assistant
All edge cases pass. Let me clean up the temporary files and run the final verification:
- Assistant
Let me now run the final verification to make sure all tests pass.
- Assistant
All 240 tests pass. Let me review the final diff one more time:
- Assistant
Let me view the final code to make sure it's correct:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.