Optimize XML tool parsers with incremental streaming and fast-path buffering#4664
Draft
lvhan028 wants to merge 1 commit into
Draft
Optimize XML tool parsers with incremental streaming and fast-path buffering#4664lvhan028 wants to merge 1 commit into
lvhan028 wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes the XML-like tool parsers used during streamed decoding by adding incremental parsing state, buffering plain value text to avoid repeated full rescans, and caching schema coercion so only newly completed parameters are coerced.
Changes:
- Added shared incremental streaming infrastructure to
XmlToolParser(payload part buffering, “in-progress value” fast-path, and coerced-args caching). - Reworked
Glm47ToolParserandQwen3CoderToolParserto maintain incremental parse state and only finalize parameters when closing tags arrive. - Added a benchmark script to compare legacy vs optimized implementations under long-value and tokenized streaming scenarios.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| lmdeploy/serve/parsers/tool_parser/xml_tool_parser.py | Adds shared buffering / incremental parsing hooks and coercion caching for streamed XML tool parsing. |
| lmdeploy/serve/parsers/tool_parser/glm47_tool_parser.py | Implements incremental state tracking for GLM-4.7 XML tool payloads. |
| lmdeploy/serve/parsers/tool_parser/qwen3coder_tool_parser.py | Implements incremental state tracking and outer-tag stripping changes for Qwen3Coder XML tool payloads. |
| benchmark/benchmark_xml_tool_parser.py | Adds a local benchmark harness comparing legacy vs optimized streaming behavior and performance. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+59
to
+63
| def _payload_text(self) -> str: | ||
| if not self._payload_parts: | ||
| return self._tool_payload | ||
| return ''.join(self._payload_parts) | ||
|
|
Comment on lines
+105
to
+106
| search_idx = 0 | ||
| while True: |
Comment on lines
+112
to
+113
| search_idx = 0 | ||
| while True: |
Comment on lines
+61
to
+68
| def _strip_outer_open_tag_once(self, payload: str) -> str: | ||
| if self._qwen_open_tag_stripped: | ||
| return payload | ||
| open_tag = self.get_tool_open_tag() | ||
| if open_tag and payload.startswith(open_tag): | ||
| self._qwen_open_tag_stripped = True | ||
| return payload[len(open_tag):] | ||
| return payload |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
XML tool parsers (GLM47, Qwen3Coder) run on the API server after each streamed decode step. The previous implementation rescanned the entire accumulated payload on every chunk and re-coerced all parameters, giving O(n²) CPU cost as tool argument values grow or arrive token-by-token.
In real streaming workloads—especially long parameter values split across hundreds or thousands of small chunks—this added unnecessary CPU overhead on the API server hot path. The goal is to keep parsing semantics identical while reducing per-chunk work to roughly O(n) and skipping work entirely while plain value text is still being buffered.
Modification
xml_tool_parser.py: Added incremental streaming infrastructure:_payload_partswith deferredjoin<,>,/)_coerced_args) so only newly completed parameters are coercedjson.loadsunless the value looks like a JSON stringglm47_tool_parser.py/qwen3coder_tool_parser.py:<tool_call>tag once instead of on every chunkbenchmark/benchmark_xml_tool_parser.py: Added benchmark comparing legacy (main-branch) vs optimized parsers on long-value and tokenized streaming scenarios (~3.5x average speedup on 2048-char values).