Preserve standalone model chat templates

This commit is contained in:
2026-09-30 11:47:29 +08:00
parent 67946deb82
commit f8937cc505
3 changed files with 18 additions and 2 deletions

View File

@@ -81,7 +81,14 @@ def repair_tokenizer_config(
patched["tokenizer_class"] = "PreTrainedTokenizerFast"
repairs.append("TokenizersBackend -> PreTrainedTokenizerFast")
if not patched.get("chat_template"):
# Transformers can load a standalone template with higher precedence than
# tokenizer_config.json. Do not synthesize one when the repository already
# supplies a template in that format.
has_standalone_template = any(
(model_dir / name).is_file()
for name in ("chat_template.jinja", "chat_template.json")
)
if not patched.get("chat_template") and not has_standalone_template:
config_path = model_dir / "config.json"
if config_path.is_file():
model_config = _json_object(config_path)