# Comparing a chat template with the original model The template check in `scan` looks for known attack patterns. A template that was written to avoid those patterns passes it, and so does a template that changes the model only in some situations, for example when tools are part of the prompt. The `compare-template` command does not depend on patterns. It compares the chat template in a GGUF file with the template of the model that the file was converted from. A converter copies the chat template without changes. When the two texts differ, someone changed the template after the original publisher wrote it, and that deserves a look. The command never runs a template. ## Using the command Compare a file on your disk with the original model on Hugging Face: ```bash gguf-radar compare-template ./model.gguf --base Qwen/Qwen2.5-0.5B-Instruct ``` Compare a file or a whole repository on Hugging Face. The target can have the same forms as in `scan`, and `--base` can pin a revision in the same way, with `tree/` and a branch name, a tag or a commit hash: ```bash gguf-radar compare-template owner/model-GGUF --base https://huggingface.co/owner/model/tree/9217f5db ``` The command reads the header of every GGUF file in a repository, whatever the file is called, and compares each distinct template once. It names the files that carry the template. A file that holds no template and claims to be a later part of a split model, with `split.no` above zero, is not compared only when a first part of the same set was read and compared and `split.count` fits. Otherwise the command reports an error and names the file. Such a file that does hold a template is compared. The result is `IDENTICAL` only when at least one template was compared and every compared template is identical. If no template was found to compare, the result is `ERROR`. The command reads up to 20 files, and `--max-files` changes this number. If the repository has more files, the result is never `IDENTICAL`. The command lists the files that it did not read and exits with 1. This does not apply when you name one file. If a request fails because of the network or a server error, the command repeats it up to two more times before it reports an error. ## The base A verdict always needs `--base`. It takes one of two things: - A Hugging Face repository of the original model. GGUF Radar reads the template the way the Transformers library does. It uses `chat_template.jinja` if the repository has one. If not, it uses `chat_template.json`, and then the `chat_template` entry of `tokenizer_config.json`, which can be a single text or a list of named templates. - A local file that holds the template text. The metadata of the GGUF file is not used to choose the base. It comes from the file itself, so a modified file can name any repository, including one that its author controls and that holds the same template. Without `--base`, the command prints what the metadata says, with each key labelled, and exits with 1. The keys are `general.source.huggingface.repository`, `general.source.url` and `general.source.repo_url`, which name the repository the file says it was converted from, and `general.base_model.0.repo_url`, which names the parent of the model and often not the model that was converted. If you trust one of the named repositories, the output shows the command to run with `--base`. The comparison is only as trustworthy as the base that you name. ## What counts as equal The templates are equal when their text is the same. Two differences are ignored because they have no effect on the output of the template and because they come from reading and writing files. These are the line ending style, so that `\r\n` and `\r` count as `\n`, and one newline at the end of the text. The result then says that the templates are identical after this normalization. Every other difference counts, including spaces, tabs, empty lines and a second newline at the end. A GGUF file can hold a named template next to the default one, for example `tokenizer.chat_template.tool_use`. The command compares the templates with the same name. A template that exists on only one side is a difference. ## Reading the result The first line of the output is `IDENTICAL`, `OLDER` or `DIFFERENT`. When a file could not be compared, or the comparison is not complete, the line says `ERROR`. For `DIFFERENT`, the command prints a unified diff of the two templates. Then it prints the findings of the template check for the base and for the target, and it says which findings appear only in the target. If the template check finds nothing in the target, the difference still needs a manual look. Most differences are harmless, as the next section explains. A finding that appears only in the target is the strongest reason to distrust the file. When the template differs from the base, the command also looks through the history of the base repository, if the base is one. It checks up to 30 commits before the requested revision, newest first, and skips a commit whose template files cannot be read. If an earlier revision has the same template as the target under the rule above, the result is `OLDER`. The command names the commit and its date, says that nothing shows that the file was edited, and still prints the diff against the requested revision. `OLDER` means that the file was not edited. It does not mean that the template is up to date. An old template can have had a flaw that the publisher fixed later. The command prints text from the model file and from the repositories, such as templates, file names and findings. It writes control characters, such as the escape character and a lone carriage return, in a visible form like `\x1b`, so that a file cannot change what your terminal shows. Add `--json` and a file name to save the same information as JSON. For `OLDER`, the report gives the matching commit and its date. Use `--json -` to print only the JSON. | Exit code | Meaning | | --- | --- | | `0` | Every template is `IDENTICAL` or `OLDER`. | | `2` | At least one template is `DIFFERENT`. | | `1` | The comparison could not be made or is not complete, for example because `--base` is missing, the base is gated, or files were not read. | ## Why a template can differ without harm - Publishers such as Unsloth edit templates on purpose, for example to fix a bug or to add support for tools. - The original publisher changed the template after the file was converted. The command then reports `OLDER` when the history has the template. You can also pin the revision with `tree/` to compare with the revision that was current at the time of the conversion. - Files in one repository can come from different sources, so that two files hold two templates. - The base that you named is the parent of the converted model and not the converted model. ## What the command cannot do - If the base repository is itself malicious, the comparison says nothing, because both templates are the same. - If you name a wrong base, the comparison says nothing. The metadata of the file cannot confirm which base is right. - It cannot tell a harmless change from a harmful one. It shows the difference, and a person has to read it. - It compares only chat templates. It does not look at the weights or at the other parts of the file, which `scan` checks. - It does not read the files in `additional_chat_templates` of a base repository. - It searches only 30 commits of the history of the base.