Skip to content

[Performance]: DeepSeek streaming tool parsers rescan the whole tool call on every increment (30 s of CPU for one 64k-character call) #20989

Open

Description

@LeizhenZhang

System Info

  • TensorRT-LLM main at 687359e (tensorrt_llm/serve/tool_parser/deepseekv3_parser.py, deepseekv31_parser.py, deepseekv32_parser.py, which deepseekv4_parser.py and deepseekv41_parser.py inherit), pure Python. A CPU is enough to reproduce.
  • Python 3.12
  • Serving path: trtllm-serve ... --tool_parser deepseek_v3 (or deepseek_v31, deepseek_v32, deepseek_v4, deepseek_v41), streaming chat completions with tools

Who can help?

@NVIDIA/trt-llm-runtime-devs (CODEOWNERS for tensorrt_llm/serve)

Reproduction

Stream one write_file call whose content argument is ordinary code, four characters per increment:

import json
from tensorrt_llm.serve.openai_protocol import ChatCompletionToolsParam
from tensorrt_llm.serve.tool_parser.deepseekv31_parser import DeepSeekV31Parser

tools = [ChatCompletionToolsParam(function={
    "name": "write_file", "parameters": {"type": "object", "properties": {
        "path": {"type": "string"}, "content": {"type": "string"}}}})]
content = ("if (a > b) { p->x = q; } // ok\n" * 2200)[:65536]
text = ("<|tool▁calls▁begin|><|tool▁call▁begin|>write_file<|tool▁sep|>"
        + json.dumps({"path": "a.c", "content": content})
        + "<|tool▁call▁end|><|tool▁calls▁end|>")
parser = DeepSeekV31Parser()
for i in range(0, len(text), 4):
    parser.parse_streaming_increment(text[i:i + 4], tools)

Total streaming time for one call (main at 687359e, one run, the same shape for each parser's format):

argument length deepseek_v3 deepseek_v31 deepseek_v32 deepseek_v4 deepseek_v41
1k chars 0.02 s 0.04 s 0.03 s 0.03 s 0.05 s
4k chars 0.18 s 0.21 s 0.19 s 0.19 s 0.24 s
16k chars 1.7 s 2.3 s 2.2 s 2.2 s 2.4 s
64k chars 23.4 s 33.1 s 32.1 s 31.8 s 31.7 s

Each fourfold step in length costs 10 to 15 times more. detect_and_parse on the same 64k text takes about 5 ms.

A model that repeats the DSML opener (<|DSML|function_calls><|DSML|invoke name="w"><|DSML|parameter name="c" string="true">) until max_tokens is worse. 16k characters of it take 53 s with deepseek_v32 and 62 s with deepseek_v4.

Expected behavior

Streaming one tool call costs time close to linear in its length, near the detect_and_parse cost.

actual behavior

Time grows with the square of the tool call length. With the default num_postprocess_workers=0 the parser runs inside chat_stream_generator on the server event loop. One 64k-character tool call (a 2000-line file written by a coding agent) spends about 30 s of event loop time, in slices that reach 15 to 23 ms for a single increment near the end of the call (measured for deepseek_v3, deepseek_v31 and deepseek_v32). Every other request on the server waits during those slices, and several such calls at once add up.

additional notes

On every increment each parser runs a regular expression over the whole buffered tool call:

  • deepseekv3_parser.py parse_streaming_increment searches <|tool▁call▁begin|>(.*)<|tool▁sep|>(.*)\n```json\n(.*)\n```.*. The leading greedy group backtracks over the whole buffer on every increment, and the pattern can only match once the closing fence arrives.
  • deepseekv31_parser.py searches <|tool▁call▁begin|>(.*)<|tool▁sep|>(.*?)(<|tool▁call▁end|>|$) and then strips, compares and json.loads the whole argument text.
  • deepseekv32_parser.py searches the invoke pattern with (.*?)(</|DSML|invoke>|$) and runs _parse_parameters_from_xml plus json.dumps on the open invoke, although the result is only used once </|DSML|invoke> arrives.

I have a fix with tests ready that keeps every streamed result the same and will open a PR that references this issue.

Activity

  1. added
    General perf
    on Oct 26, 2026
  2. added 9 commits that reference this issue on Oct 26, 2026
  3. added 9 commits that reference this issue on Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    General perf

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions