sgl-project/sglang · error · ValueError
{template_error}
Error message
{template_error} What it means
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.
Source
Thrown at python/sglang/srt/entrypoints/openai/serving_chat.py:1423
try:
rendered_prompt = (
self.tokenizer_manager.tokenizer.apply_chat_template(
openai_compatible_messages,
tokenize=False,
add_generation_prompt=True,
tools=tools,
return_dict=False,
**extra_template_kwargs,
)
)
prompt_ids = self.tokenizer_manager.tokenizer.encode(
rendered_prompt, **encode_kwargs
)
except _CHAT_TEMPLATE_CLIENT_ERRORS as template_error:
# Template errors (e.g., from raise_exception in Jinja templates)
# and TypeError (e.g., tojson filter on Jinja2 Undefined variables)
# should be treated as client errors (400 BadRequest)
raise ValueError(str(template_error)) from template_error
# Append assistant prefix if continue_final_message is enabled
if assistant_prefix:
prompt_ids = self._append_assistant_prefix_to_prompt_ids(
prompt_ids, assistant_prefix
)
if is_multimodal:
prompt = self.tokenizer_manager.tokenizer.decode(prompt_ids)
stop = request.stop
image_data = image_data if image_data else None
audio_data = audio_data if audio_data else None
video_data = video_data if video_data else None
modalities = modalities if modalities else []
return MessageProcessingResult(
prompt=prompt,
prompt_ids=prompt_ids,View on GitHub (pinned to 0132848349)
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
Example fix
# before
curl /v1/chat/completions -d '{"model":"m","messages":[{"role":"tool","content":"42"}]}' # template without tool support
# after
curl /v1/chat/completions -d '{"model":"m","messages":[{"role":"user","content":"hi"}]}' Defensive patterns
Strategy: validation
Validate before calling
required = {r.value for r in collections if r.role in ("system","user","assistant","tool")}
assert all(m.get("role") in required for m in messages)
assert all(not kw or kw for kw in (chat_template_kwargs or {})) Try / catch
try: client.chat.completions.create(...)
except BadRequestError as e:
if 'template' in str(e): fix messages/template kwargs Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Assistant tool call function.arguments must be valid JSON.
- {template_error}{suffix}
- Failed to render chat template for embedding input: {templat
- Invalid content format: {content_format}
- Invalid request body: {e}
AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28).
Data as JSON: /api/errors/f7dfbc5db4fe8d64.
Report an issue: GitHub.