Qwen3.5-9B: Mismatch between tokenized prompt and the start of tokenized prompt+completion
Reproduction
I tried to SFT Qwen3.5-9B, but it reports "Mismatch between tokenized prompt and the start of tokenized prompt+completion...". I wonder how can I fix it.
from trl import SFTTrainer, SFTConfig
from datasets import Dataset
dataset = Dataset.from_dict(
{
"prompt": [
[{"role": "user", "content": "This is my prompt."}],
[{"role": "user", "content": "This is my second prompt."}],
],
"completion": [
[{"role": "assistant", "content": "This is my response."}],
[{"role": "assistant", "content": "This is my second response."}],
],
}
)
trainer = SFTTrainer(model="Qwen/Qwen3.5-9B", train_dataset=dataset)
outputs:
Mismatch between tokenized prompt and the start of tokenized prompt+completion. This may be due to unexpected tokenizer behavior, whitespace issues, or special token handling. Verify that the tokenizer is processing text consistently.
Mismatch between tokenized prompt and the start of tokenized prompt+completion. This may be due to unexpected tokenizer behavior, whitespace issues, or special token handling. Verify that the tokenizer is processing text consistently.
System Info
transformers==5.2.0 trl==0.29.0
Checklist
- [x] I have checked that my issue isn't already filed (see open issues)
- [x] I have included my system information
- [x] Any code provided is minimal, complete, and reproducible (more on MREs)
- [x] Any code provided is properly formatted in code blocks, (no screenshot, more on code blocks)
- [x] Any traceback provided is complete
Thanks for reporting!
TL;DR
You'll get this warning for Qwen3.5 4B and larger. This warning is safe to ignore. It’s caused by how thinking mode and newline tokens are handled internally.
However, we recommend adding a chat_template_kwargs column with enable_thinking=False to your dataset to avoid the warning and ensure clean prompt/completion prefix matching.
dataset = dataset.add_column(
"chat_template_kwargs",
[{"enable_thinking": False} for _ in range(len(dataset))]
)
What’s happening
For Qwen3.5 4B+, the chat template enables thinking mode by default. This automatically inserts a <think>\n block at the beginning of the assistant turn.
So when applying the template at the string level:
>>> processing_class.apply_chat_template(prompt, tokenize=False, add_generation_prompt=True)
'<|im_start|>user\nThis is my prompt.<|im_end|>\n<|im_start|>assistant\n<think>\n'
>>> processing_class.apply_chat_template(prompt + completion, tokenize=False)
'<|im_start|>user\nThis is my prompt.<|im_end|>\n<|im_start|>assistant\n<think>\n\n</think>\n\nThis is my response.<|im_end|>\n'
At first glance, it looks correct:
template(prompt + completion) appears to start with template(prompt, add_generation_prompt=True).
This is true in string space. But the prefix check is performed in token space, not string space. Qwen’s tokenizer has a single token representing \n\n (double newline). Because the thinking template inserts multiple newlines inside the <think> block, tokenization slightly changes how the boundary is represented.
Example:
>>> processing_class.apply_chat_template(prompt, tokenize=True, add_generation_prompt=True)
[[..., 248068, 198]] # ...<think>\n
>>> processing_class.apply_chat_template(prompt + completion, tokenize=True)
[[..., 248068, 271, 248069, 271, ...]] # ...<think>\n\n</think>\n\n...
So even though the strings look prefix-compatible, the token sequences are not strictly prefix-aligned due to how newline tokens are merged. That mismatch is what triggers the warning.
Is it a real problem?
For Qwen3.5, this is expected and harmless: the warning comes from a check performed while the SFTTrainer is inferring the completion mask (i.e., which tokens belong to the prompt vs. the completion). And in this case, the SFTTrainer detects that you’re "cutting in the middle of a token." (\n\n)
However, in this specific case the inferred completion mask is still correct. (luckily!)
Recommended fix (disable thinking mode)
To remove the warning and restore strict prefix matching, add a chat_template_kwargs column that disables thinking mode:
from trl import SFTTrainer
from datasets import Dataset
dataset = Dataset.from_dict(
{
"prompt": [
[{"role": "user", "content": "This is my prompt."}],
[{"role": "user", "content": "This is my second prompt."}],
],
"completion": [
[{"role": "assistant", "content": "This is my response."}],
[{"role": "assistant", "content": "This is my second response."}],
],
}
)
# Add this!
dataset = dataset.add_column(
"chat_template_kwargs",
[{"enable_thinking": False} for _ in range(len(dataset))]
)
trainer = SFTTrainer(model="Qwen/Qwen3.5-9B", train_dataset=dataset)
This will include the whole <think>\n\n</think> in the prompt (instead of just <think>\n) and avoid the token-level prefix mismatch entirely.
~~this worked for me, thanks!~~ wait...
I used enable_thinking but it still has the same warning
Doesn't the enable_thinking=False workaround still create a train/inference distribution shift for users who want to preserve and train on thinking blocks? Agentic traces, in particular, typically contain empty thinking blocks for e.g. tool calls, even when thinking is enabled.