Update chat_template.jinja

#20
by dluthyc - opened

Title: Fix partial assistant prefills when continue_final_message=True

Summary:
The current chat_template.jinja always appends <|im_end|> after assistant turns.
When callers disable add_generation_prompt and use a final assistant message as a prefill, this closes the assistant message before generation starts, so the model tends to emit an empty continuation or start a new turn incorrectly.

This patch makes the final assistant turn honor Hugging Face's continue_final_message contract by skipping the closing <|im_end|> only when:

  • the current message is the last message
  • continue_final_message is defined
  • continue_final_message is true

All other rendering paths are unchanged.

Minimal repro:

messages = [
    {"role": "user", "content": "Reply in JSON."},
    {"role": "assistant", "content": '{"name": "'},
]

tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=False,
    continue_final_message=True,
)

Before:

<|im_start|>user
Reply in JSON.<|im_end|>
<|im_start|>assistant
{"name": "<|im_end|>

After:

<|im_start|>user
Reply in JSON.<|im_end|>
<|im_start|>assistant
{"name": "
Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment