Add generation markers to the chat template
Browse filesThis PR adds `{% generation %}` / `{% endgeneration %}` markers around the assistant output in `chat_template.jinja`, so `return_assistant_tokens_mask=True` returns the assistant tokens. Without them the mask is all zeros, and assistant-only loss (e.g. TRL SFT with `assistant_only_loss=True`) can't work. Rendering is unchanged.
The `<|im_start|>assistant\n` header is now emitted once before the branches, so it stays outside the mask. The mask covers what the model generates, up to and including `<|im_end|>`:
```
<think>\nr\n</think>\n\nHello<|im_end|>\n
```
Checked: the rendered prompt is identical before and after for 40 combinations (system, multi-turn, tool calls and responses, with and without `tools`, `add_generation_prompt`, `enable_thinking`). The mask covers each assistant turn in multi-turn and tool-call conversations, and the `<|im_end|>` stop token is always inside it.
39 unmodified lines404142434445464748495051525315 unmodified lines69707172737439 unmodified lines{%- set content = content.split('</think>')[-1].lstrip('\n') %}{%- endif %}{%- endif %}{%- if loop.index0 > ns.last_query_index %}{%- if loop.last or (not loop.last and reasoning_content) %}{{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}{%- else %}{{- '<|im_start|>' + message.role + '\n' + content }}{%- endif %}{%- else %}{{- '<|im_start|>' + message.role + '\n' + content }}{%- endif %}{%- if message.tool_calls %}{%- for tool_call in message.tool_calls %}15 unmodified lines{%- endfor %}{%- endif %}{{- '<|im_end|>\n' }}{%- elif message.role == "tool" %}{%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}{{- '<|im_start|>user' }}39 unmodified lines4041424344454647484950515253545515 unmodified lines7172737475767739 unmodified lines{%- set content = content.split('</think>')[-1].lstrip('\n') %}{%- endif %}{%- endif %}{{- '<|im_start|>' + message.role + '\n' }}{%- generation %}{%- if loop.index0 > ns.last_query_index %}{%- if loop.last or (not loop.last and reasoning_content) %}{{- '<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}{%- else %}{{- content }}{%- endif %}{%- else %}{{- content }}{%- endif %}{%- if message.tool_calls %}{%- for tool_call in message.tool_calls %}15 unmodified lines{%- endfor %}{%- endif %}{{- '<|im_end|>\n' }}{%- endgeneration %}{%- elif message.role == "tool" %}{%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}{{- '<|im_start|>user' }}