hiyouga/LlamaFactory · error · ValueError
Cannot inject {% generation %} into chat template: no `{% el
Error message
Cannot inject {% generation %} into chat template: no `{% elif message['role'] == 'assistant' %}` block found. What it means
Error "Cannot inject {% generation %} into chat template: no `{% elif message['role'] == 'assistant' %}` block found." thrown in hiyouga/LlamaFactory.
Source
Thrown at src/llamafactory/train/megatron_bridge/dataset_export.py:57
def supports_hf_chat_template() -> bool:
r"""Return whether the installed Megatron Bridge supports HF chat templates."""
try:
from megatron.bridge.data.datasets.sft import GPTSFTChatDataset
return "use_hf_tokenizer_chat_template" in inspect.signature(GPTSFTChatDataset.__init__).parameters
except Exception:
return False
def _inject_generation_block(chat_template: str) -> str:
r"""Wrap assistant content with ``{% generation %}`` when missing."""
if _GENERATION_REGEX.search(chat_template):
return chat_template
match = _ASSISTANT_ELIF_REGEX.search(chat_template)
if match is None:
raise ValueError(
"Cannot inject {% generation %} into chat template: "
"no `{% elif message['role'] == 'assistant' %}` block found."
)
body = match.group(2)
if _END_GENERATION_REGEX.search(body):
return chat_template
patched = (
chat_template[: match.start()]
+ match.group(1)
+ "{% generation %}"
+ body
+ "{% endgeneration %}"
+ match.group(3)
+ chat_template[match.end() :]
)
if not _GENERATION_REGEX.search(patched):View on GitHub (pinned to f28afaf635)
Solutions
- Use a chat template that contains a standard `{% elif message['role'] == 'assistant' %}` block, or manually add `{% generation %}...{% endgeneration %}` around the assistant content in the template.
Example fix
{% elif message['role'] == 'assistant' %}{% generation %}{{ message['content'] }}{% endgeneration %} When it happens
Trigger: Thrown at src/llamafactory/train/megatron_bridge/dataset_export.py:57 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of hiyouga/LlamaFactory@f28afaf635 (2026-08-14).
Data as JSON: /api/errors/e0b7d5565e87f301.
Report an issue: GitHub.