From 389e13792181f7ec55eaffe3408ee5e4a0fa1e9b Mon Sep 17 00:00:00 2001 From: Mengxi-Xu <109797814+Mengxi-Xu@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:45:45 +0800 Subject: [PATCH] Improve error handling in prompt rendering MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Jinja2ChatFormatter renders chat templates from model metadata using jinja2’s sandboxed environment. When a template calls range() with more than MAX_RANGE (100000) items, the sandbox raises OverflowError — which is not a TemplateError subclass. Since __call__ did not catch it, the exception escaped and crashed the caller (e.g. every POST /v1/chat/completions request for a model whose GGUF embeds such a template returned a raw traceback instead of a clean error). Fix: wrap the render(...) call and translate OverflowError into ValueError, matching the error contract already used by the raise_exception helper in the same method. The sandbox behavior itself is unchanged — only the error surface is normalized --- llama_cpp/llama_chat_format.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/llama_cpp/llama_chat_format.py b/llama_cpp/llama_chat_format.py index 62e236415..3fc4f8c41 100644 --- a/llama_cpp/llama_chat_format.py +++ b/llama_cpp/llama_chat_format.py @@ -266,20 +266,22 @@ def __call__( ) -> ChatFormatterResponse: def raise_exception(message: str): raise ValueError(message) - - prompt = self._environment.render( - messages=messages, - eos_token=self.eos_token, - bos_token=self.bos_token, - raise_exception=raise_exception, - add_generation_prompt=self.add_generation_prompt, - functions=functions, - function_call=function_call, - tools=tools, - tool_choice=tool_choice, - strftime_now=self.strftime_now, - **kwargs, - ) + try: + prompt = self._environment.render( + messages=messages, + eos_token=self.eos_token, + bos_token=self.bos_token, + raise_exception=raise_exception, + add_generation_prompt=self.add_generation_prompt, + functions=functions, + function_call=function_call, + tools=tools, + tool_choice=tool_choice, + strftime_now=self.strftime_now, + **kwargs, + ) + except OverflowError as e: + raise ValueError(str(e)) from e stopping_criteria = None if self.stop_token_ids is not None: