{"record":{"id":"bef6cfa934e98a37","repo":"unslothai/unsloth","slug":"chat-template-exceeds-the-max-chat-template-bytes-bef6cf","errorCode":null,"errorMessage":"Chat template exceeds the {MAX_CHAT_TEMPLATE_BYTES}-byte limit.","messagePattern":"Chat template exceeds the (.+?)-byte limit\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"studio/backend/picker/schemas.py","lineNumber":37,"sourceCode":"    \"reject\": such a template can never render.\n    \"\"\"\n    try:\n        return len(value.encode(\"utf-8\"))\n    except UnicodeEncodeError:\n        return None\n\n\nclass ValidateChatTemplateRequest(BaseModel):\n    template: str = Field(default = \"\")\n\n    @field_validator(\"template\")\n    @classmethod\n    def _enforce_template_size(cls, value: str) -> str:\n        size = chat_template_byte_length(value)\n        if size is None:\n            raise ValueError(\"Chat template contains unpaired surrogate characters.\")\n        if size > MAX_CHAT_TEMPLATE_BYTES:\n            raise ValueError(f\"Chat template exceeds the {MAX_CHAT_TEMPLATE_BYTES}-byte limit.\")\n        return value\n\n\nclass ValidateChatTemplateResponse(BaseModel):\n    valid: bool\n    error: Optional[str] = None\n\n\nclass ModelTemplateResponse(BaseModel):\n    model_name: str\n    chat_template: Optional[str] = None\n","sourceCodeStart":19,"sourceCodeEnd":49,"githubUrl":"https://github.com/unslothai/unsloth/blob/203007d19051dcd2ae33876786d117c99f6b0368/studio/backend/picker/schemas.py#L19-L49","documentation":"Field validator on ValidateChatTemplateRequest.template enforcing a hard byte-size cap (MAX_CHAT_TEMPLATE_BYTES) on the chat template. The size is computed as the UTF-8 byte length (chat_template_byte_length), not character count, so templates with many multi-byte characters hit the limit sooner. Oversized templates are rejected because they bloat every tokenizer config request and can exceed downstream storage/transfer limits.","triggerScenarios":"POSTing a ValidateChatTemplateRequest whose template's UTF-8 encoding exceeds MAX_CHAT_TEMPLATE_BYTES (e.g. a template with dozens of macro definitions or large embedded few-shot examples).","commonSituations":"Templates with long hardcoded system prompts or few-shot examples inlined; templates copied from another repo that include extensive branching for many message roles; non-ASCII (CJK/emoji) content roughly tripling byte count versus character count.","solutions":["Trim the template: move large few-shot examples or system text out of the template and pass them as message content at runtime instead.","Reduce duplicated Jinja branches by using macros/loops over message roles.","Verify the byte size locally before sending: len(template.encode('utf-8')) and keep it under the limit."],"exampleFix":"# before\ntemplate = \"{%- for shot in [LONG_FEW_SHOT_1, LONG_FEW_SHOT_2, ...] %}...\"  # > limit bytes\n\n# after\n# keep only structural Jinja in the template; inject few-shot text via messages at inference time\ntemplate = \"{%- for message in messages %}{{ message.content }}{% endfor %}\"","handlingStrategy":"validation","validationCode":"MAX_CHAT_TEMPLATE_BYTES = 65_536  # keep in sync with the server constant\n\ndef template_fits(template: str) -> bool:\n    try:\n        return len(template.encode(\"utf-8\")) <= MAX_CHAT_TEMPLATE_BYTES\n    except UnicodeEncodeError:\n        return False  # unpaired surrogates — see separate error","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Measure bytes, not characters: len(s.encode('utf-8')).","Keep large prompt content out of the template; inject it via messages at runtime."],"tags":["pydantic","chat-template","size-limit","validation"],"backgroundTag":null,"analyzedSha":"203007d19051dcd2ae33876786d117c99f6b0368","analyzedAt":"2026-08-15T02:48:39.846Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}