{"record":{"id":"f7dfbc5db4fe8d64","repo":"sgl-project/sglang","slug":"template-error","errorCode":null,"errorMessage":"{template_error}","messagePattern":"\\{template_error\\}","errorType":"validation","errorClass":"ValueError","httpStatus":400,"severity":"error","filePath":"python/sglang/srt/entrypoints/openai/serving_chat.py","lineNumber":1423,"sourceCode":"                try:\n                    rendered_prompt = (\n                        self.tokenizer_manager.tokenizer.apply_chat_template(\n                            openai_compatible_messages,\n                            tokenize=False,\n                            add_generation_prompt=True,\n                            tools=tools,\n                            return_dict=False,\n                            **extra_template_kwargs,\n                        )\n                    )\n                    prompt_ids = self.tokenizer_manager.tokenizer.encode(\n                        rendered_prompt, **encode_kwargs\n                    )\n                except _CHAT_TEMPLATE_CLIENT_ERRORS as template_error:\n                    # Template errors (e.g., from raise_exception in Jinja templates)\n                    # and TypeError (e.g., tojson filter on Jinja2 Undefined variables)\n                    # should be treated as client errors (400 BadRequest)\n                    raise ValueError(str(template_error)) from template_error\n\n            # Append assistant prefix if continue_final_message is enabled\n            if assistant_prefix:\n                prompt_ids = self._append_assistant_prefix_to_prompt_ids(\n                    prompt_ids, assistant_prefix\n                )\n\n            if is_multimodal:\n                prompt = self.tokenizer_manager.tokenizer.decode(prompt_ids)\n\n        stop = request.stop\n        image_data = image_data if image_data else None\n        audio_data = audio_data if audio_data else None\n        video_data = video_data if video_data else None\n        modalities = modalities if modalities else []\n        return MessageProcessingResult(\n            prompt=prompt,\n            prompt_ids=prompt_ids,","sourceCodeStart":1405,"sourceCodeEnd":1441,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/entrypoints/openai/serving_chat.py#L1405-L1441","documentation":"Raised when applying the model's Jinja chat template fails (e.g. the template calls raise_exception, or a filter like tojson hits an Undefined variable). SGLang wraps the underlying template/TypeError into a ValueError so the OpenAI-compatible layer maps it to a 400 BadRequest client error. It originates in _apply_jinja_template, called while processing chat messages.","triggerScenarios":"POST /v1/chat/completions where the model's chat template raises: invalid messages structure for the template (e.g. missing required roles), passing chat_template_kwargs the template doesn't expect, a tojson filter applied to an undefined variable, or a template that explicitly calls raise_exception on bad input.","commonSituations":"Custom --chat-template that assumes specific message shapes; multi-turn conversations with tool messages fed to a template lacking tool support; wrong or missing tokenizer_config chat_template; passing unsupported chat_template_kwargs.","solutions":["Fix the request payload: ensure message roles/content shapes match what the model's chat template expects (e.g. include tool role support)","Inspect the wrapped template_error message (raise __cause__) to find the exact template line, then fix the custom chat template","If passing chat_template_kwargs, verify the template actually uses those variable names","Retry with a stock template / remove custom --chat-template to isolate the issue"],"exampleFix":"# before\ncurl /v1/chat/completions -d '{\"model\":\"m\",\"messages\":[{\"role\":\"tool\",\"content\":\"42\"}]}'  # template without tool support\n# after\ncurl /v1/chat/completions -d '{\"model\":\"m\",\"messages\":[{\"role\":\"user\",\"content\":\"hi\"}]}'","handlingStrategy":"validation","validationCode":"required = {r.value for r in collections if r.role in (\"system\",\"user\",\"assistant\",\"tool\")}\nassert all(m.get(\"role\") in required for m in messages)\nassert all(not kw or kw for kw in (chat_template_kwargs or {}))","typeGuard":null,"tryCatchPattern":"try: client.chat.completions.create(...)\nexcept BadRequestError as e:\n    if 'template' in str(e): fix messages/template kwargs","preventionTips":["Validate message roles/content before sending","Test chat_template_kwargs against the template once and cache results","Keep a smoke-test request per model in CI"],"tags":["jinja","chat-template","openai-api","bad-request"],"backgroundTag":"chat-template-render-failed","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}