{"record":{"id":"e0b7d5565e87f301","repo":"hiyouga/LlamaFactory","slug":"cannot-inject-generation-into-chat-template","errorCode":null,"errorMessage":"Cannot inject {% generation %} into chat template: no `{% elif message['role'] == 'assistant' %}` block found.","messagePattern":"Cannot inject (.+?) into chat template: no `(.+?)` block found\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/llamafactory/train/megatron_bridge/dataset_export.py","lineNumber":57,"sourceCode":"\ndef supports_hf_chat_template() -> bool:\n    r\"\"\"Return whether the installed Megatron Bridge supports HF chat templates.\"\"\"\n    try:\n        from megatron.bridge.data.datasets.sft import GPTSFTChatDataset\n\n        return \"use_hf_tokenizer_chat_template\" in inspect.signature(GPTSFTChatDataset.__init__).parameters\n    except Exception:\n        return False\n\n\ndef _inject_generation_block(chat_template: str) -> str:\n    r\"\"\"Wrap assistant content with ``{% generation %}`` when missing.\"\"\"\n    if _GENERATION_REGEX.search(chat_template):\n        return chat_template\n\n    match = _ASSISTANT_ELIF_REGEX.search(chat_template)\n    if match is None:\n        raise ValueError(\n            \"Cannot inject {% generation %} into chat template: \"\n            \"no `{% elif message['role'] == 'assistant' %}` block found.\"\n        )\n\n    body = match.group(2)\n    if _END_GENERATION_REGEX.search(body):\n        return chat_template\n\n    patched = (\n        chat_template[: match.start()]\n        + match.group(1)\n        + \"{% generation %}\"\n        + body\n        + \"{% endgeneration %}\"\n        + match.group(3)\n        + chat_template[match.end() :]\n    )\n    if not _GENERATION_REGEX.search(patched):","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/hiyouga/LlamaFactory/blob/f28afaf6355af515454dfb16c97d728307c93897/src/llamafactory/train/megatron_bridge/dataset_export.py#L39-L75","documentation":"Error \"Cannot inject {% generation %} into chat template: no `{% elif message['role'] == 'assistant' %}` block found.\" thrown in hiyouga/LlamaFactory.","triggerScenarios":"Thrown at src/llamafactory/train/megatron_bridge/dataset_export.py:57 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","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."],"exampleFix":"{% elif message['role'] == 'assistant' %}{% generation %}{{ message['content'] }}{% endgeneration %}","handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"f28afaf6355af515454dfb16c97d728307c93897","analyzedAt":"2026-08-14T21:57:28.298Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}