apply_chat_template TemplateError: "Conversation roles must alternate user/assistant/user/assistant/..."
Symptom: tokenizer.apply_chat_template raises jinja2 TemplateError: Conversation roles must alternate user/assistant/user/assistant/... with a llama-2 style template (hf-internal-testing/llama-tokenizer). Triggers verified: two consecutive user messages; a system message followed directly by an assistant message; a lone tool-role message. A system message first followed by a user message is fine (folded into the first [INST] as <<SYS>>...<</SYS>>). Fix: merge consecutive same-role messages, put system first followed by a user turn, and never insert a system message mid-conversation. Templates that do not validate (ChatML such as SmolLM2) render the same bad input silently, so the error appears only when you swap models. Outputs: [user,user] -> TemplateError; [system,assistant] -> TemplateError; [tool] -> TemplateError; [system,user] -> <s>[INST] <<SYS>> s <</SYS>> b [/INST]. Versions: transformers 5.23.0, Python 3.12.