Skip to content

bug: Gemma 4 reasoning channel leaks into CLI output as raw tokens #884

Closed

Description

@inureyes

Summary

When a Gemma 4 checkpoint emits its reasoning channel, the mlxcel run / mlxcel generate CLI path prints the raw channel markers and the entire chain-of-thought to the terminal as visible output. Observed on gemma-4-e4b-it-qat-4bit (also reproduces on plain gemma-4-e4b-it-4bit). The server chat-completions path already routes this reasoning into reasoning_content, so the gap is specifically the CLI output path, plus a minor leftover on the server side.

Reproduction

CLI (leaks raw markers + reasoning):

./target/release/mlxcel run <path>/gemma-4-e4b-it-qat-4bit -t 0 -n 60 \
  -p "Explain briefly why the sky is blue."

Actual output (verbatim):

<|channel>thought
Here's a thinking process to arrive at the suggested answer:
1.  **Analyze the Request:** The user wants a brief explanation of why the sky is blue.
2.  **Identify the Core Scientific Concept:** The phenomenon is called Rayleigh scattering.
3.  ...

The literal <|channel>thought marker and the full step-by-step reasoning are printed as if they were the answer. On prompts where the model answers directly (e.g. "What is 2+2? Answer briefly." returns 4) there is no leak, so the trigger is: the model chooses to open a thought channel.

Server, same model and prompt, for contrast (/v1/chat/completions, non-stream): the reasoning is routed to reasoning_content and content holds the final answer. But reasoning_content retains a leading thought\n (the channel argument), e.g. reasoning_content = "thought\nHere's a thinking process ...".

Expected

  • CLI: the reasoning channel should be hidden from the visible answer by default (raw <|channel>thought / <channel|> markers must never print), matching how the server hides it from content. A toggle (consistent with the existing preserve_thinking / enable_thinking kwargs, e.g. a --show-reasoning flag) may optionally surface the reasoning, ideally dimmed.
  • Server: reasoning_content should not retain the thought channel-argument prefix.

Root cause

mlxcel already recognizes the Gemma 4 channel markers. The tokenizer sets up ThinkingMarkers { think_start: "<|channel>thought", think_end: "<channel|>" } when both <|channel> and <channel|> are present as token IDs (src/tokenizer/mod.rs:250; the e4b-qat tokenizer defines them as added-token IDs 100 and 101 in tokenizer.json). The server reasoning splitters use this to separate reasoning from content.

The CLI generation path does not use these markers on the output side:

  • src/commands/chat.rs streaming callback (around lines 602-608) calls decode_state.on_token(...) then print!("{text}") for every generated piece, with no thinking-channel filtering. So the raw <|channel>thought block streams straight to the terminal.
  • The CLI's only thinking-awareness is src/commands/generate.rs:707 (infer_thinking_markers().has_thinking()), which governs the prompt-side enable_thinking default only, not output rendering.
  • Note the final transcript decode uses tokenizer.decode(&ids, true) (skip special tokens) so the marker is not re-fed into history, but that path does not run on the live-streamed visible output and does not strip the thought argument or the CoT body.

Additionally, the prompt-side thinking-OFF default from issue #686 (the closed <|channel>thought\n<channel|> priming scaffold, src/commands/generate.rs:700-711, src/server/chat_template.rs:2043-2058) does not prevent the model from opening a thought channel on many prompts, so relying on the prompt default is insufficient. The output path must handle the channel whenever it appears.

The server-side thought\n leftover comes from think_start = "<|channel>thought" where the <|channel> token is consumed but the thought argument text is not trimmed from the captured reasoning.

Proposed fix

  1. Apply the tokenizer's ThinkingMarkers (think_start / think_end) in the CLI streaming output path (src/commands/chat.rs stream callback and the mlxcel generate one-shot output), so the reasoning channel is suppressed from visible terminal output by default and the raw <|channel>thought / <channel|> markers never print.
  2. Buffer across the streaming boundary: the markers can split across token pieces, so hold output until the channel open/close state is resolved (a small stream-state filter, mirroring the server splitters).
  3. Trim the think_start argument (thought) so neither the CLI reasoning display nor the server reasoning_content keeps a leftover prefix.
  4. Add an opt-in CLI flag to surface reasoning (dimmed), consistent with preserve_thinking / enable_thinking.
  5. Tests (CLI + server): thought-then-answer, answer-only (no channel), thought cut off with no <channel|> close, and streamed token-by-token, all asserting the raw markers never appear in visible output and reasoning is captured without the thought prefix.

Acceptance criteria

  • mlxcel run / mlxcel generate on a Gemma 4 checkpoint that opens a thought channel prints only the final answer by default; no <|channel> / <channel|> / thought text appears in the visible output.
  • The optional reasoning-display flag surfaces the reasoning without raw markers.
  • Server reasoning_content no longer carries the leading thought prefix.
  • The listed tests pass; the direct-answer case (no channel) is byte-identical to today.

Environment / notes

Activity

  1. added
    type:bug
    priority:medium
    on Jul 23, 2026
  2. inureyes commented on Jul 23, 2026

    @inureyes
    MemberAuthor

    Clarification: the originally reported "useless tokens" symptom on gemma-4-e4b-it-qat is actually a collapse into reserved <unused...> vocabulary tokens caused by a chunked-prefill sliding-window mask drop on the server. That is now tracked separately in #885 (#885).

    This issue (#884) remains valid as a separate, lower-severity problem: the <|channel>thought reasoning-channel text leaking into CLI output. The two have different root causes.

  3. added
    status:in-progress
    status:review
    and removed
    status:in-progress
    on Jul 23, 2026
  4. added
    status:done
    and removed
    status:review
    on Jul 23, 2026
  5. self-assigned this
    on Aug 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

priority:mediumstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions