fix: reject GGUF models before tokenizer load

GGUF ids such as org/model-q4_0-gguf currently reach
AutoTokenizer.from_pretrained and die with an unrelated
sentencepiece/tiktoken ValueError. Detect those ids and local
.gguf paths first and exit with a short message that points
users at Safetensors.

Fixes #533

Co-authored-by: Lixin2026 <2023Anita@users.noreply.github.com>
This commit is contained in:
Cursor Agent 2026-11-04 02:50:43 +00:00
parent f4cf535403
commit c1e41eff4b
No known key found for this signature in database
2 changed files with 111 additions and 0 deletions

View File

@ -108,6 +108,33 @@ def chat_template_supports_system_role(tokenizer: PreTrainedTokenizerBase) -> bo
ModuleIO: TypeAlias = list[dict[str, dict[int, tuple[Tensor, Tensor]]]]
def _looks_like_gguf(model: str) -> bool:
"""
Return whether a model id or path looks like a GGUF checkpoint.
Hugging Face GGUF repos usually contain ``-gguf`` or ``_gguf`` in the id
(for example ``org/model-q4_0-gguf``). Local GGUF files end with ``.gguf``.
The check is purely lexical, so it does not contact the Hub.
"""
normalized = model.strip().replace("\\", "/").lower()
basename = normalized.rstrip("/").rsplit("/", 1)[-1]
return basename.endswith(".gguf") or "-gguf" in normalized or "_gguf" in normalized
def _reject_gguf_model(model: str, *, what: str) -> None:
"""Exit before tokenizer load if ``model`` looks like GGUF."""
if not _looks_like_gguf(model):
return
# SystemExit prints this message and skips the Rich traceback handler
# installed in main(). A normal exception would still dump a traceback.
raise SystemExit(
"heretic: GGUF models are not supported yet "
f"({what} '{model}'). "
"Use a Safetensors checkpoint or a normal Hugging Face model instead."
)
class Model:
model: PreTrainedModel | PeftModel
tokenizer: PreTrainedTokenizerBase
@ -122,6 +149,12 @@ class Model:
self.settings = settings
self.needs_reload = False
# Reject GGUF before AutoTokenizer.from_pretrained. Those ids otherwise
# fail with an unrelated sentencepiece/tiktoken ValueError.
_reject_gguf_model(settings.model, what="model")
if settings.evaluate_model is not None:
_reject_gguf_model(settings.evaluate_model, what="evaluation model")
self.revision_kwargs = {}
if settings.model_commit is not None:
self.revision_kwargs["revision"] = settings.model_commit

78
tests/test_gguf.py Normal file
View File

@ -0,0 +1,78 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
# Copyright (C) 2025-2026 Philipp Emanuel Weidmann <pew@worldwidemann.com> + contributors
import unittest
from unittest.mock import patch
from heretic.config import Settings
from heretic.model import Model, _looks_like_gguf
class GGUFRejectionTests(unittest.TestCase):
def _assert_rejected(self, model: str, *, evaluate_model: str | None = None) -> str:
settings = Settings.model_construct(
model=model,
evaluate_model=evaluate_model,
)
with (
patch(
"heretic.model.AutoTokenizer.from_pretrained",
side_effect=AssertionError("tokenizer should not be loaded"),
) as tokenizer,
self.assertRaises(SystemExit) as caught,
):
Model(settings)
tokenizer.assert_not_called()
message = str(caught.exception)
self.assertIn("GGUF models are not supported yet", message)
self.assertIn("Safetensors", message)
self.assertIn("Hugging Face", message)
return message
def test_rejects_gguf_model_id_before_tokenizer_load(self) -> None:
# Repro from https://github.com/p-e-w/heretic/issues/533
model = "google/gemma-4-26B-A4B-it-qat-q4_0-gguf"
message = self._assert_rejected(model)
self.assertIn(model, message)
self.assertIn("model '", message)
def test_rejects_gguf_paths_and_markers(self) -> None:
models = [
"org/Model_GGUF",
"weights/foo.gguf",
"weights/FOO.GGUF",
r"C:\models\bar.gguf",
"dir/model.gguf/",
]
for model in models:
with self.subTest(model=model):
message = self._assert_rejected(model)
self.assertIn(model, message)
def test_rejects_gguf_evaluate_model_before_tokenizer_load(self) -> None:
evaluate_model = "/models/gemma-q4_0.gguf"
message = self._assert_rejected(
"google/gemma-2-2b-it",
evaluate_model=evaluate_model,
)
self.assertIn("evaluation model", message)
self.assertIn(evaluate_model, message)
self.assertNotIn("google/gemma-2-2b-it", message)
def test_normal_model_ids_are_not_treated_as_gguf(self) -> None:
models = [
"google/gemma-2-2b-it",
"meta-llama/Llama-3.1-8B-Instruct",
"/tmp/model.safetensors",
"org/gguf",
"org/model.gguf.txt",
]
for model in models:
with self.subTest(model=model):
self.assertFalse(_looks_like_gguf(model))
if __name__ == "__main__":
unittest.main()