{"record":{"id":"7906373cf08da7e7","repo":"sgl-project/sglang","slug":"failed-to-render-chat-template-for-embedding-input","errorCode":null,"errorMessage":"Failed to render chat template for embedding input: {template_error}","messagePattern":"Failed to render chat template for embedding input: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":400,"severity":"error","filePath":"python/sglang/srt/entrypoints/openai/serving_embedding.py","lineNumber":241,"sourceCode":"                video_data=[],\n                audio_data=[],\n                modalities=[],\n            )\n            try:\n                prompt = self.tokenizer_manager.tokenizer.apply_chat_template(\n                    [processed_msg],\n                    tokenize=False,\n                    add_generation_prompt=True,\n                )\n            except jinja2.TemplateError as template_error:\n                location = getattr(template_error, \"lineno\", None)\n                name = getattr(template_error, \"name\", None)\n                suffix = \"\"\n                if name or location:\n                    suffix = f\" (template={name or '<unknown>'}, line={location})\"\n                raise ValueError(f\"{template_error}{suffix}\") from template_error\n            except (TypeError, KeyError, AttributeError) as template_error:\n                raise ValueError(\n                    f\"Failed to render chat template for embedding input: {template_error}\"\n                ) from template_error\n            prompts.append(prompt)\n\n        return prompts\n\n    async def _handle_non_streaming_request(\n        self,\n        adapted_request: EmbeddingReqInput,\n        request: EmbeddingRequest,\n        raw_request: Request,\n    ) -> Union[EmbeddingResponse, ErrorResponse, ORJSONResponse]:\n        \"\"\"Handle the embedding request\"\"\"\n        try:\n            ret = await self.tokenizer_manager.generate_request(\n                adapted_request, raw_request\n            ).__anext__()\n        except ValueError as e:","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/srt/entrypoints/openai/serving_embedding.py#L223-L259","documentation":"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.","triggerScenarios":"Embedding request where message dicts are missing keys the template accesses directly (KeyError), or input types don't match template expectations (TypeError/AttributeError).","commonSituations":"Sending plain strings where the template expects message lists; missing 'role' keys; template using attribute access on dicts that lack the attribute.","solutions":["Match the input shape the template expects (list of {role, content} dicts)","Fix the template to use .get() with defaults instead of direct indexing","Test the template locally with your exact payload before sending"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"assert all(isinstance(m, dict) and 'role' in m and 'content' in m for m in messages)","typeGuard":"def valid_chat_messages(v) -> bool:\n    return isinstance(v, list) and all(isinstance(m, dict) and isinstance(m.get('role'), str) and 'content' in m for m in v)","tryCatchPattern":"except ValueError as e: if 'Failed to render chat template' in str(e): fix input shape","preventionTips":["Send messages as role/content dicts, not raw strings","Use .get() with defaults in custom templates","Render-test payloads locally first"],"tags":["jinja","embeddings","chat-template","type-error"],"backgroundTag":"chat-template-render-failed","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}