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.
This commit is contained in:
danielhanchen 2026-07-17 13:44:07 +00:00
commit 709077c83c

View file

@ -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