@@ -267,6 +267,16 @@ def verify_chat_template_generation_prompt_logic(tokenizer_model):
267267 actual_prefix_in_full_turn = full_turn_ids [len (prompt_wo_gen_ids ) : len (prompt_wo_gen_ids ) + len (assistant_prefix )]
268268
269269 if actual_prefix_in_full_turn != assistant_prefix :
270+ # Allow the generation prompt to include a thought channel block (e.g., for Gemma 4).
271+ thought_channel = "<|channel>thought\n <channel|>"
272+ thought_ids = extract_token_ids (tokenizer_model .encode (thought_channel , add_special_tokens = False ))
273+ if len (assistant_prefix ) >= len (thought_ids ) and assistant_prefix [- len (thought_ids ) :] == thought_ids :
274+ true_prefix_ids = assistant_prefix [: - len (thought_ids )]
275+ actual_prefix = full_turn_ids [len (prompt_wo_gen_ids ) : len (prompt_wo_gen_ids ) + len (true_prefix_ids )]
276+ if actual_prefix == true_prefix_ids :
277+ max_logging .info ("Chat template generation prompt mismatch resolved via thought channel bypass." )
278+ return
279+
270280 expected_str = tokenizer_model .decode (assistant_prefix )
271281 actual_str = tokenizer_model .decode (actual_prefix_in_full_turn )
272282 raise ValueError (
@@ -298,6 +308,12 @@ def _get_completion_in_chat_template(tokenizer_model, round_msgs):
298308 prompt_completion_ids = extract_token_ids (prompt_completion_tokens )
299309 prompt_ids = extract_token_ids (prompt_tokens )
300310
311+ # Bypass for Gemma 4's thought channel block which is included in generation prompt but not in normal assistant turns
312+ thought_channel = "<|channel>thought\n <channel|>"
313+ thought_ids = extract_token_ids (tokenizer_model .encode (thought_channel , add_special_tokens = False ))
314+ if len (prompt_ids ) >= len (thought_ids ) and prompt_ids [- len (thought_ids ) :] == thought_ids :
315+ prompt_ids = prompt_ids [: - len (thought_ids )]
316+
301317 completion_tokens = prompt_completion_ids [len (prompt_ids ) :]
302318 completion_in_chat_template = tokenizer_model .decode (completion_tokens , skip_special_tokens = False )
303319 return completion_in_chat_template
0 commit comments