Skip to content

Fix Hermes chat template validation error#4246

Open
danielhanchen wants to merge 1 commit into
mainfrom
dh/recover-4183-hermes-chat-template
Open

Fix Hermes chat template validation error#4246
danielhanchen wants to merge 1 commit into
mainfrom
dh/recover-4183-hermes-chat-template

Conversation

@danielhanchen
Copy link
Copy Markdown
Member

Replacement for #4183 due to Studio rebasing

Fixes #4150

Summary

Hermes models use ChatML-style templates that don't require the {% if add_generation_prompt %} validation check. This PR adds hermes to the list of models that bypass this check, similar to how mistral and qwen3guard are already handled.

Changes

  • Added hermes to the model bypass list in load_correct_tokenizer function in unsloth/tokenizer_utils.py

Root Cause

When loading a Hermes model (or a LoRA adapter trained with Hermes/ChatML template), the fix_chat_template function was raising a RuntimeError because:

  1. The ChatML template doesn't have {% if add_generation_prompt %} in a format the auto-fixer expects
  2. The auto-fixer _fix_chat_template couldn't patch it automatically
  3. This caused the error: "does not have a {% if add_generation_prompt %} for generation purposes"

Testing

The fix follows the existing pattern used for mistral and qwen3guard models, which have similar template structures that don't require this validation.

Diff Stats

 unsloth/tokenizer_utils.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a RuntimeError encountered when loading Hermes models due to an incompatibility with the fix_chat_template function's validation check. By adding Hermes to an existing bypass list, it ensures that models using ChatML-style templates, which do not require the add_generation_prompt validation, can be loaded correctly, improving the robustness of the tokenizer loading process for various model architectures.

Highlights

  • Hermes Model Support: Added "hermes" to the list of models that bypass the {% if add_generation_prompt %} validation check in unsloth/tokenizer_utils.py, resolving a RuntimeError when loading Hermes models.
Changelog
  • unsloth/tokenizer_utils.py
    • Added "hermes" to the list of model names that bypass the add_generation_prompt validation.
Activity
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly adds hermes to the list of models that bypass the chat template validation, which resolves a RuntimeError. The change is straightforward and aligns with the existing implementation for mistral and qwen3guard. I have one suggestion to improve the code's maintainability by extracting the hardcoded list of model names into a constant.

Comment on lines 605 to 608
if any(
s in str(getattr(tokenizer, "name_or_path", "")).lower()
for s in ["mistral", "qwen3guard"]
for s in ["mistral", "qwen3guard", "hermes"]
):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For better readability and maintainability, it's good practice to extract this list of model names into a well-named constant. This makes the code easier to understand and modify in the future. Using a tuple instead of a list is also slightly more performant for this kind of check.

Suggested change
if any(
s in str(getattr(tokenizer, "name_or_path", "")).lower()
for s in ["mistral", "qwen3guard"]
for s in ["mistral", "qwen3guard", "hermes"]
):
MODELS_BYPASSING_CHAT_TEMPLATE_FIX = ("mistral", "qwen3guard", "hermes")
if any(
s in str(getattr(tokenizer, "name_or_path", "")).lower()
for s in MODELS_BYPASSING_CHAT_TEMPLATE_FIX
):

@mykeehu
Copy link
Copy Markdown

mykeehu commented Mar 25, 2026

This is a more elegant solution:
#4426
or this:
#4298

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Hermes Lora chat test bug: does not have a {% if add_generation_prompt %} for generation purposes

3 participants