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 inspect the repository structure and search for streaming, XML tool-call parsing, and finish-reason logic. Then I’ll identify the non-streaming recovery path and compare it to streaming behavior before building a focused reproduction.
- Assistant
The initial search confirms tool recovery is a deliberate server feature, but the broad output was noisy. I’ll now limit inspection to the application package and tests, locating parser classes and the exact chat completion streaming implementation.
- Assistant
StreamPostProcessoralready contains an end-of-stream fallback intended to match non-stream parsing, so the bug is likely in its gating, buffering, or event ordering. I’ll inspect this class, its callers, parser contracts, and current git state to understand whether the fallback is incomplete. - Assistant
The file output was truncated across unrelated route content. I’ll inspect the postprocessor in focused sections, especially all three processing paths and
finalize(), then read the existing postprocessor/tool-filter tests to derive expected event semantics. - Assistant
The Hermes incremental parser handles only exact streaming formats, while full parsing supports more XML variants. The postprocessor’s fallback should bridge this, but I need the untruncated implementation and precise tests. I’ll retrieve only those narrow ranges.
- Assistant
A key flaw is visible: the final engine chunk emits
finish_reason="stop"beforefinalize()performs full recovery, after which recovery emits a tool-call event. This violates ordering and can cause clients to stop consuming. Also, accumulation currently bypasses the parser whenever a chunk lacks<or[, even after prior markup handling, and reasoning paths may not preserve all model output. I’ll inspect stream event formatting/call order and enumerate recovery tests already present near the bottom oftest_postprocessor.py. - Assistant
The route calls
finalize()after all chunk events, confirming the premature finish concern. I’ll inspect the exact formatter because the cleanest fix may require deferring a terminal event inside the postprocessor and merging it after recovery, while preserving final content/usage metadata. - Assistant
The non-stream path parses
output.textwholesale. Streaming’s fallback instead choosestool_accumulated_textover raw accumulated text, and standard mode does not populate rawaccumulated_textat all. Reasoning can therefore make fallback inspect only a fragment. I’ll compare helper parser-selection semantics and all parser XML formats, then create a temporary reproduction against the real Hermes parser covering split chunks, multiple calls, reasoning mode, malformed XML, and finish ordering. - Assistant
I’ve confirmed the recent fallback fix was intentionally broad but did not defer terminal emission or guarantee a complete raw transcript. Before editing, I’ll run a focused temporary script using actual
HermesToolParserandStreamingPostProcessorto record current failures for alternate XML, split/multiple calls, reasoning, plain text, and malformed markup. - Assistant
The reproduction confirms concrete defects: a split opening tag leaks
<tool_calas content, multiple XML calls yield only one recovered call, malformed markup suppresses the stream without a terminal event, and terminal ordering is inconsistent depending on parser behavior. I’ll inspect Hermes streaming behavior directly and the API-level generic fallback parser to decide whether the fix belongs solely in orchestration or also in parser logic. - Assistant
The direct parser trace identifies the multiple-call root cause: when one call is complete but a second is partial, Hermes suppresses both; on the next chunk it emits only index 1. Since
tool_calls_detectedthen disables fallback, index 0 is lost. I’ll fix orchestration generically by:- Accumulating the complete raw model transcript independently of reasoning/content routing.
- Deferring terminal finish events until full-response recovery has run.
- Reconciling full-parser calls against already streamed calls by function signature and index, so missing calls are emitted without duplicates.
- Buffering ambiguous split XML prefixes and flushing them as content if markup proves malformed/non-tool.
- Catching full-parser errors so malformed output cannot abort finalization.
- Updating the route to format deferred finish/content events from
finalize().
- Assistant
The core patch is in place; the formatter is not installed in this environment, so I’ll keep style manually compliant. Next I’ll extend the route’s finalize loop to serialize recovered content and deferred finishes, then run the reproduction and focused tests to expose any behavioral regressions.
- Assistant
The reproduction now passes recovery, finish, reasoning, request-selection, and malformed-stream requirements. One subtle issue remains: in the multiple-call case, incremental parsing emits index 1 before final recovery emits index 0. Although OpenAI aggregation by index can reconstruct this, it is safer and more compatible to hold any non-contiguous incremental tool delta and let final recovery emit the complete ordered list. I’ll add that guard and rerun the isolated test file (the API spec test cannot import here because the Linux environment lacks
libmlx.so). - Assistant
Focused tests reveal two compatibility expectations: tests may seed
tool_accumulated_textdirectly, and already-streamed contiguous tool calls are expected to finish immediately rather than requiring an explicitfinalize()call. I can preserve both without weakening recovery: use the tool buffer only when no raw transcript exists, and defer terminal emission only when no contiguous tool call has been emitted (the exact condition for fallback/malformed/non-contiguous recovery). Finalization will avoid adding a duplicate finish when no terminal was deferred. - Assistant
The focused processor suite is now fully green, and the reproduction produces ordered multiple calls with
tool_calls, no split-tag leakage, reasoning/request-selection parity, and a normalstopfinish for malformed markup. I’ll now review the complete diff for accidental changes and run adjacent parser/tool/streaming suites that are importable without MLX. I’ll also use the repository’s configured lint tools if available through its declared development dependencies. - Assistant
All 200 adjacent parser/tool/filter tests pass. Diff review shows the changes are localized. I’ll install the declared
ruffdevelopment tool (it is missing from the environment), run format/lint checks on modified files, then rerun focused tests because formatting can expose or alter line-level issues. - Assistant
Formatting and lint are clean, and 274 focused/adjacent tests pass. The remaining API-level spec test failed only because the dynamic linker cannot find
libmlx.so; I’ll locate the installed MLX library and retry with an appropriate temporaryLD_LIBRARY_PATH, without changing repository configuration. - Assistant
No MLX shared library is installed, so API-route collection cannot be repaired via linker configuration. I’ll inspect the existing streaming spec’s fallback assertions to ensure the new deferred route events preserve IDs, finish reasons, and usage behavior, then run the reproduction one final time after formatter changes and inspect repository status.
- Assistant
Reviewing the spec test uncovered one expected invariant I should preserve: plain text with a configured parser must emit its finish during
process_chunk()and makefinalize()a no-op. The current implementation deferred every parser-backed plain response. I’ll narrow deferral to responses containing a plausible tool marker (<,{, or[Calling), which still covers XML recovery and malformed markup while preserving the established plain-text fast path. - Assistant
The plain-text invariant is restored and all focused tests remain green. For the malformed-markup guarantee, one final defensive gap remains: an incremental parser exception currently propagates and aborts SSE even though full-parser exceptions are handled. I’ll catch incremental parse errors and pass buffered text through as content, then run upstream regression tests plus the focused set.
- Assistant
Verification is complete:
- Reproduced the reported error: the original processor leaked split XML prefixes, dropped the first of multiple XML calls, and emitted no terminal event for malformed markup.
- Confirmed the fix: the temporary real-Hermes reproduction now emits single/split/multiple XML calls in order and ends with
tool_calls; reasoning mode andtool_choicedo not change recovery. - Considered edge cases: plain text retains the fast path; malformed or parser-throwing markup is passed through and finishes normally; already-streamed calls are deduplicated by signature; non-contiguous calls are recovered in order.
- Ran affected existing tests: 353 tests passed across postprocessing, tool parsers/calling/filtering, and upstream regressions.
ruffformat/check andgit diff --checkpass. The API spec module could not collect because this Linux environment has nolibmlx.so; its fallback assertions were manually reviewed and preserved.
I’ll do one final concise diff/status check and then submit the two source-file changes.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.