From 40bd8b89174c0878b016e2c67b92e7551f4dac3d Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Tue, 24 Mar 2026 10:01:05 +0000 Subject: [PATCH] fix: preserve chat_template BOS check when test_text is None The has_bos_token_already guard wrapped both test_text.startswith() and bos_token in chat_template with test_text is not None, which disabled the chat_template BOS detection for conversational datasets where test_text is set to None. Split the guard so test_text is not None only applies to the startswith() call, while bos_token in chat_template is always checked. --- unsloth/models/rl_replacements.py | 2 +- unsloth/tokenizer_utils.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/unsloth/models/rl_replacements.py b/unsloth/models/rl_replacements.py index 023b952168..9f555416d4 100755 --- a/unsloth/models/rl_replacements.py +++ b/unsloth/models/rl_replacements.py @@ -164,7 +164,7 @@ def sft_trainer_prepare_dataset(function_name, function): " test_text = None\n" "chat_template = getattr(tokenizer, 'chat_template', None)\n" "chat_template = '' if chat_template is None else chat_template\n" - "has_bos_token_already = (test_text is not None and (test_text.startswith(tokenizer.bos_token) or tokenizer.bos_token in chat_template)) " + "has_bos_token_already = ((test_text is not None and test_text.startswith(tokenizer.bos_token)) or tokenizer.bos_token in chat_template) " "if getattr(tokenizer, 'bos_token', None) is not None else False\n" "if 'add_special_tokens' not in locals() and has_bos_token_already:\n" " from functools import partial\n" diff --git a/unsloth/tokenizer_utils.py b/unsloth/tokenizer_utils.py index 506641ae0b..96c22f62ff 100644 --- a/unsloth/tokenizer_utils.py +++ b/unsloth/tokenizer_utils.py @@ -985,7 +985,7 @@ def patch_sft_trainer_tokenizer(): " test_text = None\n" "chat_template = getattr(tokenizer, 'chat_template', None)\n" "chat_template = '' if chat_template is None else chat_template\n" - "has_bos_token_already = (test_text is not None and (test_text.startswith(tokenizer.bos_token) or tokenizer.bos_token in chat_template)) " + "has_bos_token_already = ((test_text is not None and test_text.startswith(tokenizer.bos_token)) or tokenizer.bos_token in chat_template) " "if getattr(tokenizer, 'bos_token', None) is not None else False\n" "if 'add_special_tokens' not in locals() and has_bos_token_already:\n" " from functools import partial\n"