sgl-project/sglang · error · ValueError

Failed to render chat template for embedding input: {templat

Error message

Failed to render chat template for embedding input: {template_error}

What it means

The Jinja render for an embedding input raised TypeError/KeyError/AttributeError rather than a TemplateError — typically the template or rendering code touched a missing key or wrong type (e.g. messages lacking a field the template indexes). It's wrapped as ValueError with a 'Failed to render chat template for embedding input' prefix.

Source

Thrown at python/sglang/srt/entrypoints/openai/serving_embedding.py:241

                video_data=[],
                audio_data=[],
                modalities=[],
            )
            try:
                prompt = self.tokenizer_manager.tokenizer.apply_chat_template(
                    [processed_msg],
                    tokenize=False,
                    add_generation_prompt=True,
                )
            except jinja2.TemplateError as template_error:
                location = getattr(template_error, "lineno", None)
                name = getattr(template_error, "name", None)
                suffix = ""
                if name or location:
                    suffix = f" (template={name or '<unknown>'}, line={location})"
                raise ValueError(f"{template_error}{suffix}") from template_error
            except (TypeError, KeyError, AttributeError) as template_error:
                raise ValueError(
                    f"Failed to render chat template for embedding input: {template_error}"
                ) from template_error
            prompts.append(prompt)

        return prompts

    async def _handle_non_streaming_request(
        self,
        adapted_request: EmbeddingReqInput,
        request: EmbeddingRequest,
        raw_request: Request,
    ) -> Union[EmbeddingResponse, ErrorResponse, ORJSONResponse]:
        """Handle the embedding request"""
        try:
            ret = await self.tokenizer_manager.generate_request(
                adapted_request, raw_request
            ).__anext__()
        except ValueError as e:

View on GitHub (pinned to 0132848349)

Solutions

  1. Match the input shape the template expects (list of {role, content} dicts)
  2. Fix the template to use .get() with defaults instead of direct indexing
  3. Test the template locally with your exact payload before sending
Defensive patterns

Strategy: validation

Validate before calling

assert all(isinstance(m, dict) and 'role' in m and 'content' in m for m in messages)

Type guard

def valid_chat_messages(v) -> bool:
    return isinstance(v, list) and all(isinstance(m, dict) and isinstance(m.get('role'), str) and 'content' in m for m in v)

Try / catch

except ValueError as e: if 'Failed to render chat template' in str(e): fix input shape

Prevention

When it happens

Trigger: Embedding request where message dicts are missing keys the template accesses directly (KeyError), or input types don't match template expectations (TypeError/AttributeError).

Common situations: Sending plain strings where the template expects message lists; missing 'role' keys; template using attribute access on dicts that lack the attribute.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/7906373cf08da7e7. Report an issue: GitHub.