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

Đang mở
#23,342 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@chaitanya0305 đang làm issue này rồi.

Từ ngày 3/12/2026.

  • #23343 của @chaitanya0305 — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
backend-api-design

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.
Ngôn ngữ chính
Python
Star
15.2k
Fork
3k
Merge trung bình
2 ngày 21 giờ
Pull request đã merge (30 ngày)
477

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của NVIDIA/TensorRT-LLM

Tất cả issue của NVIDIA/TensorRT-LLM

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.