Gemma 4 supports a 'Thinking' mode for complex reasoning tasks. To enable it, add <|thought|> to the beginning of your system prompt and set enable_thinking=True in the chat template application.
Output Structure:
When thinking is enabled, the model outputs a thought block followed by the final response.
Important Rules:
- In multi-turn conversations, omit the thought blocks from the conversation history. Only pass the
result["response"] back to the model. - For 31B/26B A4B models, if
enable_thinking=False, the model will still output empty tags (thought\n<|\|> and \n<|\|> ) to maintain structure. - E2B/E4B models will skip empty tags entirely when disabled.
messages = [
{
"role": "system",
"content": "<|thought|>You are a math expert."
},
{"role": "user", "content": "Solve: 3x + 7 = 22"}
]
text = processor.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=True
)
# ... generate and parse ...