From 709077c83c2e4e4f90dfef1e8f835e60aab456be Mon Sep 17 00:00:00 2001 From: danielhanchen Date: Fri, 17 Jul 2026 13:44:07 +0000 Subject: [PATCH] Fast-path the chat-template size check on character count Reject an oversized chat_template_override on its character count (a lower bound on the UTF-8 byte length) before allocating the encoded bytes, so a multi-megabyte string is turned away without the intermediate encode. --- studio/backend/models/inference.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/studio/backend/models/inference.py b/studio/backend/models/inference.py index db509dd911..f290028a37 100644 --- a/studio/backend/models/inference.py +++ b/studio/backend/models/inference.py @@ -58,9 +58,15 @@ class LoadRequest(BaseModel): @field_validator("chat_template_override") @classmethod def normalize_blank_chat_template_override(cls, value: Optional[str]) -> Optional[str]: - if value is not None and value.strip() == "": + if value is None: return None - if value is not None and len(value.encode("utf-8")) > MAX_CHAT_TEMPLATE_BYTES: + # Reject on character count first (a lower bound on the UTF-8 byte length) + # so an oversized template is rejected before allocating the encoded bytes. + if len(value) > MAX_CHAT_TEMPLATE_BYTES: + raise ValueError(f"Chat template exceeds the {MAX_CHAT_TEMPLATE_BYTES}-byte limit.") + if value.strip() == "": + return None + if len(value.encode("utf-8")) > MAX_CHAT_TEMPLATE_BYTES: raise ValueError(f"Chat template exceeds the {MAX_CHAT_TEMPLATE_BYTES}-byte limit.") return value