{"record":{"id":"aaedde2604512b13","repo":"hiyouga/LlamaFactory","slug":"template-name-already-exists","errorCode":null,"errorMessage":"Template {name} already exists.","messagePattern":"Template (.+?) already exists\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/llamafactory/data/template.py","lineNumber":576,"sourceCode":"    ```\n    <s><user>user prompt here\n    <model>model response here</s>\n    <user>user prompt here\n    <model>model response here</s>\n    ```\n\n    The corresponding code should be:\n    ```\n    register_template(\n        name=\"custom\",\n        format_user=StringFormatter(slots=[\"<user>{{content}}\\n<model>\"]),\n        format_assistant=StringFormatter(slots=[\"{{content}}</s>\\n\"]),\n        format_prefix=EmptyFormatter(\"<s>\"),\n    )\n    ```\n    \"\"\"\n    if name in TEMPLATES:\n        raise ValueError(f\"Template {name} already exists.\")\n\n    default_slots = [\"{{content}}\"] if efficient_eos else [\"{{content}}\", {\"eos_token\"}]\n    default_user_formatter = StringFormatter(slots=[\"{{content}}\"])\n    default_assistant_formatter = StringFormatter(slots=default_slots)\n    if format_assistant is not None:\n        default_function_formatter = FunctionFormatter(slots=format_assistant.slots, tool_format=\"default\")\n    else:\n        default_function_formatter = FunctionFormatter(slots=default_slots, tool_format=\"default\")\n\n    default_tool_formatter = ToolFormatter(tool_format=\"default\")\n    default_prefix_formatter = EmptyFormatter()\n    TEMPLATES[name] = template_class(\n        format_user=format_user or default_user_formatter,\n        format_assistant=format_assistant or default_assistant_formatter,\n        format_system=format_system or default_user_formatter,\n        format_function=format_function or default_function_formatter,\n        format_observation=format_observation or format_user or default_user_formatter,\n        format_tools=format_tools or default_tool_formatter,","sourceCodeStart":558,"sourceCodeEnd":594,"githubUrl":"https://github.com/hiyouga/LlamaFactory/blob/f28afaf6355af515454dfb16c97d728307c93897/src/llamafactory/data/template.py#L558-L594","documentation":"register_template inserts into a global TEMPLATES dict keyed by name and refuses to overwrite an existing entry. Calling register_template twice with the same name — especially common with 'custom' — raises ValueError immediately, protecting built-in templates from silent shadowing.","triggerScenarios":"Running register_template(name=\"custom\", ...) twice in one process: notebook cell re-execution, a webui session that registers user templates then re-registers on reload, or a plugin + user script both defining the same template name. Also triggers when attempting to override a built-in name like \"llama3\".","commonSituations":"Jupyter workflows where cells run twice; calling get_template_and_fix_tokenizer after manually registering, then registering again in a retry loop; library upgrades where user code now collides with a newly built-in template name.","solutions":["Use a unique template name per registration (e.g. \"my_model_v1\").","In notebooks or re-entrant scripts, guard with: if \"custom\" not in TEMPLATES: register_template(...) or del TEMPLATES[\"custom\"] before re-registering.","Never reuse a built-in template name; pick a prefixed name to avoid collisions with future releases."],"exampleFix":"# before\nregister_template(name=\"custom\", ...)\nregister_template(name=\"custom\", ...)  # ValueError\n\n# after\nfrom llamafactory.data.template import TEMPLATES, register_template\nif \"custom\" not in TEMPLATES:\n    register_template(name=\"custom\", ...)","handlingStrategy":"validation","validationCode":"from llamafactory.data.template import TEMPLATES\n\nname = \"custom\"\nif name not in TEMPLATES:\n    register_template(name=name, ...)  # safe to call","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Guard register_template with a name-in-TEMPLATES check in notebooks and re-entrant scripts.","Prefer unique, namespaced template names over 'custom'."],"tags":["template","custom-template","idempotency"],"backgroundTag":null,"analyzedSha":"f28afaf6355af515454dfb16c97d728307c93897","analyzedAt":"2026-08-14T21:57:28.298Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}