[Bug]: gemma4 reasoning parser returns reasoning as content on requests without tools; reasoning_content keeps the thought label

未关闭
#23,342 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@chaitanya0305 已经在做这个了。

开始于 2026年12月3日。

  • #23343 来自 @chaitanya0305 —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python

调研方向

Start with Gemma4ReasoningParser and compare its raw-token handling with MuseSparkReasoningParser and KimiK3ReasoningParser. The issue names tests/unittest/llmapi/test_reasoning_parser.py for regression tests; check the linked pull request for work already underway. Done means reasoning is separated from content without the thought label, including when streaming.

由索引模型根据 Issue 内容生成。

描述

System Info

TensorRT-LLM main (3f692ac). This is serving-layer logic only, so it reproduces on CPU with the google/gemma-4-E4B-it tokenizer.

Who can help?

No response

Information
  • The official example scripts
  • My own modified scripts
Reproduction

trtllm-serve with a Gemma 4 model and --reasoning_parser gemma4 (or auto, which maps gemma4 to it). Send a chat request with chat_template_kwargs: {"enable_thinking": true} and no tools.

<|channel> and <channel|> are special tokens in the Gemma 4 tokenizer. The request keeps the default skip_special_tokens=True, and Gemma4ReasoningParser does not set needs_raw_special_tokens. So _configure_parser_special_token_decoding leaves the default in place and the detokenizer removes both markers:

from transformers import AutoTokenizer
from tensorrt_llm.llmapi.reasoning_parser import ReasoningParserFactory

tok = AutoTokenizer.from_pretrained("google/gemma-4-E4B-it")
ids = tok.encode("<|channel>thought\nThe user wants 2+2.<channel|>4", add_special_tokens=False)
text = tok.decode(ids, skip_special_tokens=True)      # what the server decodes
print(repr(text))                                      # 'thought\nThe user wants 2+2.4'
r = ReasoningParserFactory.create_reasoning_parser("gemma4").parse(text)
print(repr(r.content), repr(r.reasoning_content))      # 'thought\nThe user wants 2+2.4' ''
print(ReasoningParserFactory.needs_raw_special_tokens("gemma4"))  # False
Expected behavior

content == "4". reasoning_content holds the reasoning.

actual behavior
  • Without tools: the whole reasoning block, plus its thought label, is returned as content, glued to the answer, and reasoning_content is empty. With tools it works only because the Gemma 4 tool parser sets needs_raw_special_tokens.
  • With tools (raw tokens kept): reasoning_content starts with the channel label ("thought\nThe user wants..."). In a tool-call loop the Gemma 4 chat template replays that turn's reasoning as '<|channel>thought\n' + reasoning_content + '\n<channel|>'. The next prompt therefore contains <|channel>thought\nthought\n..., which the model never produced: an off-distribution prompt and a prefix-cache miss at every tool step. vLLM's Gemma 4 parser strips this label (_strip_thought_label).
additional notes

Fix:

  1. Set needs_raw_special_tokens = True on Gemma4ReasoningParser. MuseSparkReasoningParser and KimiK3ReasoningParser already do this for the same reason.
  2. Drop a leading thought\n from each channel body in parse and parse_delta. While streaming, a partial label split across deltas is held back.

A PR with regression tests in tests/unittest/llmapi/test_reasoning_parser.py follows.

A related gap that the PR does not change: after a tool response with enable_thinking=True, the template's generation prompt ends with <|channel>thought\n. The completion then starts inside the channel, but Gemma4ReasoningParser always starts outside reasoning. That reasoning is still returned as content up to <channel|>. Fixing it needs the prompt-tail resolution (resolves_thinking_from_prompt) that other parsers use, so I left it for a separate change.

Before submitting a new issue...
  • Make sure you already searched for relevant issues, and checked the documentation and examples for answers to frequently asked questions.
主要语言
Python
星标
15.2k
派生
3k
平均合并
2 天 21 小时
30 天内合并 PR
477

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

NVIDIA/TensorRT-LLM 的其他 Issue

查看 NVIDIA/TensorRT-LLM 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。