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 first map the repository and locate the non-streaming versus streaming tool-call parsing paths. Then I’ll reproduce the discrepancy with the existing test/framework conventions, implement a shared or equivalent recovery path, and run focused plus broader tests.
- Assistant
The project is Python and already has substantial tool-streaming coverage. I’ll now trace XML recovery symbols and inspect the focused tests and route implementation to identify where streaming bypasses non-streaming parsing.
- Assistant
A fallback was recently added in
StreamPostProcessor, but its comments and gating suggest it is parser-specific and likely too restrictive. I’ll inspect the complete lifecycle (buffer accumulation, parser creation, finish handling) and relevant parser contracts before constructing a minimal direct reproduction. - Assistant
The route calls
finalize()only after the engine’s own finish event has already been emitted, so recovery can produce a second terminal chunk; that is acceptable only if the recovered call chunk itself carriestool_calls. More importantly, fallback currently calls only the configured native parser and does nothing when no parser is selected, unlike non-streaming’s genericparse_tool_callsfallback. I’ll inspect exact state transitions and existing direct processor tests to verify all failure modes. - Assistant
The code confirms a fundamental parity gap: text is accumulated only when a tool parser exists, and the generic recovery used by non-streaming is never invoked. Also, reasoning mode stores raw text in
accumulated_text, while parser mode preferstool_accumulated_text, potentially losing XML routed through reasoning. I’ll read the latest regression tests and parser result types, then run a small script demonstrating no-parser, split-call, and multiple-call behavior without modifying tests. - Assistant
To guarantee “same as non-streaming,” the safest approach is to retain the exact raw generated text for every stream and invoke the same
_parse_tool_calls_with_parserfunction at finalization when incremental parsing found none. This naturally handles chunk boundaries, multiple calls, configured parsers, generic XML fallback, reasoning mode, and tool selection. I’ll confirm helper dependencies/circular-import safety and use a temporary script to record the current failures first. - Assistant
I’ll reproduce the key bug directly: with default configuration (the same generic XML parser non-streaming uses), feed two calls split across chunks through
StreamingPostProcessor. I expect non-streaming to return two calls while streaming emits none and finishes withstop. - Assistant
The standalone reproduction is blocked by the environment’s absent MLX shared library; the test suite provides MLX stubs in
conftest.py. I’ll run the closest existing postprocessor tests to establish the baseline, then implement raw accumulation plus generic end-of-stream recovery and exception-safe malformed handling. - Assistant
Even pytest cannot load because an installed
mlxpackage points to unavailablelibmlx.so; this is an environment limitation rather than a code failure. I’ll proceed with the localized change, then use explicit module stubs for verification if necessary. The implementation will avoid importing route helpers (and circular dependencies) by calling the same genericparse_tool_callsfallback directly after the configured parser. - Assistant
I’ll now add a raw-output buffer independent of reasoning/tool-parser state, recover at the finishing chunk (so
stopis replaced bytool_calls, not followed by it), retainfinalize()as a safety net for engines that end without a finish chunk, and make parser failures degrade to text instead of terminating the stream. - Assistant
The core change compiles. Before verification, I noticed one compatibility concern: existing fallback tests inject a parser and expect its full-output fallback even when no configured parser exists; the new flow preserves that. I’ll create an external (non-repository) lightweight MLX stub solely to let Linux collect the processor tests, then run those tests and a temporary focused regression.
- Assistant
The first stub successfully bypassed
libmlx.so; collection now only needsmlx_lm.generate. I’ll complete the minimal external stubs and rerun, without changing repository files. - Assistant
One more import-only class is required. I’ll add it externally and rerun the focused suites.
- Assistant
Collection reaches the last MLX-LM cache import. I’ll stub that external module and rerun.
- Assistant
Focused tests now execute and exposed a backward-compatibility case: a test directly seeds the legacy
tool_accumulated_textbeforefinalize(), without callingprocess_chunk. Production uses raw buffering, but preserving this supported test seam is harmless. I’ll make recovery prefer raw text and fall back to the legacy buffers, then rerun. - Assistant
An existing contract deliberately reserves full-output fallback for
finalize(), even when the engine’s last output hasfinished=True; moving recovery earlier breaks that seam. The route still emits the fallback call as its last terminal choice withfinish_reason="tool_calls", satisfying the issue. I’ll keep raw accumulation and recovery semantics but restore finalize-only timing, while retaining a terminal finish for malformed suppressed markup. - Assistant
All 79 focused existing tests pass. I’ll now rerun the original discrepancy reproduction under the external stub, including: two XML calls split mid-tag/mid-JSON, no configured parser, tools requested both true and false, reasoning parser mode, plain text, and malformed markup. This directly validates the issue’s stated dimensions.
- Assistant
The direct reproduction is fixed: streaming now matches non-streaming’s two calls across arbitrary chunk boundaries, independent of whether tools were requested, and the last event is
tool_calls; plain and malformed text finish normally without calls. I’ll additionally verify a real reasoning parser configuration, then inspect formatting/lint and run the broader tool/streaming test set. - Assistant
Reasoning enabled and explicitly disabled both recover the same two calls and finish correctly.
ruffis not installed, butgit diff --checkpasses. I’ll run the broader affected suites under the external MLX stub and use available formatting/static tools afterward. - Assistant
All 305 affected tests pass. The Black command’s nonzero status was hidden by redirected stderr, so I’ll determine whether Black is unavailable or reports formatting changes, and inspect the exact final diff for correctness and unnecessary modifications.
- Assistant
The final diff is localized. I’ll align native fallback ID behavior with non-streaming (generate an ID if a parser omits one), then install/run the repository’s declared linter since it is absent from the environment.
- Assistant
Lint rules pass; the formatter requests a formatting adjustment. I’ll apply Ruff formatting, inspect that it does not create unrelated churn, and rerun affected tests after formatting.
- Assistant
Formatting and all 305 affected tests pass again. I’ll run the route-level tool-call and reasoning streaming suites to catch adapter/SSE regressions, then perform final status/diff checks.
- Assistant
Route-level verification passes (26 passed; six live/model E2E tests skipped by their existing markers). I’ll now review the final changed sections, ensure only the intended source file is modified, and check for whitespace or accidental artifacts before submission.
- Assistant
Final review highlights an important protocol detail: if a missed call is recovered only after a
stopfinish chunk has already been sent, standards-compliant clients may stop reading and never see it. I’ll defer a recoverable terminal call internally untilfinalize()—without exposing it or marking detection during chunk processing—so existing finalize semantics remain intact while the wire stream’s only terminal finish reason istool_calls. Malformed markup will still retain its normalstopfinish because preview recovery finds no call. - Assistant
The finish deferral preserves all 79 focused contracts. I’ll rerun the direct parity script to confirm there is no earlier
stop, then rerun the full 305 affected tests once more because this changes terminal timing. - Assistant
The direct stream now has no premature
stop: valid split XML ends only withtool_calls, while malformed/plain output ends withstopand creates no calls. The full 305-test affected set still passes. I’ll verify every productionStreamingPostProcessorconsumer invokesfinalize()(required by its documented interface), then do final lint/status checks. - Assistant
There is one production consumer and it always invokes
finalize(), so deferred recovery cannot be lost. I’ll perform one final end-to-end SSE request using the existing mock engine/client helper, confirming two structured calls andtool_callsas the sole finish reason at the HTTP interface.
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 →
Qwen/Qwen3.6-27BCompare side by side →