Skip to content

Commit 9a004bd

Browse files
committed
Fix: catch TemplateSyntaxError when parsing metadata chat templates
Some models (e.g., LLaVA 1.5) contain non-standard Jinja2 tags (like {% generation %}) in their metadata. This commit adds a try-except block to prevent initialization crashes, allowing the model to load even if the metadata template is invalid.
1 parent ecff482 commit 9a004bd

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

llama_cpp/llama.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -565,13 +565,25 @@ def free_lora_adapter():
565565
file=sys.stderr,
566566
)
567567

568+
# Iterate through all the chat templates found in the model's metadata
568569
for name, template in template_choices.items():
569-
self._chat_handlers[name] = llama_chat_format.Jinja2ChatFormatter(
570-
template=template,
571-
eos_token=eos_token,
572-
bos_token=bos_token,
573-
stop_token_ids=[eos_token_id],
574-
).to_chat_handler()
570+
try:
571+
# Attempt to parse and register the template as a valid chat handler.
572+
# We wrap this in a try-block because some models (like LLaVA) contain
573+
# non-standard Jinja2 tags (e.g., {% generation %}) that cause the
574+
# standard parser to crash.
575+
self._chat_handlers[name] = llama_chat_format.Jinja2ChatFormatter(
576+
template=template,
577+
eos_token=eos_token,
578+
bos_token=bos_token,
579+
stop_token_ids=[eos_token_id],
580+
).to_chat_handler()
581+
except Exception as e:
582+
# If parsing fails (e.g., TemplateSyntaxError), log a warning but do not crash.
583+
# This ensures the model still loads even if one metadata template is broken.
584+
if self.verbose:
585+
print(f"Warning: Failed to parse chat template '{name}': {e}", file=sys.stderr)
586+
pass
575587

576588
if (
577589
self.chat_format is None

0 commit comments

Comments
 (0)