hiyouga/LlamaFactory · error · ValueError

Template {name} already exists.

Error message

Template {name} already exists.

What it means

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.

Source

Thrown at src/llamafactory/data/template.py:576

    ```
    <s><user>user prompt here
    <model>model response here</s>
    <user>user prompt here
    <model>model response here</s>
    ```

    The corresponding code should be:
    ```
    register_template(
        name="custom",
        format_user=StringFormatter(slots=["<user>{{content}}\n<model>"]),
        format_assistant=StringFormatter(slots=["{{content}}</s>\n"]),
        format_prefix=EmptyFormatter("<s>"),
    )
    ```
    """
    if name in TEMPLATES:
        raise ValueError(f"Template {name} already exists.")

    default_slots = ["{{content}}"] if efficient_eos else ["{{content}}", {"eos_token"}]
    default_user_formatter = StringFormatter(slots=["{{content}}"])
    default_assistant_formatter = StringFormatter(slots=default_slots)
    if format_assistant is not None:
        default_function_formatter = FunctionFormatter(slots=format_assistant.slots, tool_format="default")
    else:
        default_function_formatter = FunctionFormatter(slots=default_slots, tool_format="default")

    default_tool_formatter = ToolFormatter(tool_format="default")
    default_prefix_formatter = EmptyFormatter()
    TEMPLATES[name] = template_class(
        format_user=format_user or default_user_formatter,
        format_assistant=format_assistant or default_assistant_formatter,
        format_system=format_system or default_user_formatter,
        format_function=format_function or default_function_formatter,
        format_observation=format_observation or format_user or default_user_formatter,
        format_tools=format_tools or default_tool_formatter,

View on GitHub (pinned to f28afaf635)

Solutions

  1. Use a unique template name per registration (e.g. "my_model_v1").
  2. In notebooks or re-entrant scripts, guard with: if "custom" not in TEMPLATES: register_template(...) or del TEMPLATES["custom"] before re-registering.
  3. Never reuse a built-in template name; pick a prefixed name to avoid collisions with future releases.

Example fix

# before
register_template(name="custom", ...)
register_template(name="custom", ...)  # ValueError

# after
from llamafactory.data.template import TEMPLATES, register_template
if "custom" not in TEMPLATES:
    register_template(name="custom", ...)
Defensive patterns

Strategy: validation

Validate before calling

from llamafactory.data.template import TEMPLATES

name = "custom"
if name not in TEMPLATES:
    register_template(name=name, ...)  # safe to call

Prevention

When it happens

Trigger: 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".

Common situations: 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.

Related errors


AI-assisted analysis of hiyouga/LlamaFactory@f28afaf635 (2026-08-14). Data as JSON: /api/errors/aaedde2604512b13. Report an issue: GitHub.