{"record":{"id":"43692e1df6c5e603","repo":"sgl-project/sglang","slug":"encoded-prompt-has-tensor-shape-1-tokens-expec","errorCode":null,"errorMessage":"Encoded prompt has {tensor.shape[1]} tokens, expected at least {max_sequence_length}","messagePattern":"Encoded prompt has (.+?) tokens, expected at least (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"python/sglang/multimodal_gen/runtime/pipelines/sana_video.py","lineNumber":46,"sourceCode":"    \"settling into a curled position, peacefully falling asleep on a warm sunny \"\n    \"windowsill, with gentle sunlight filtering through surrounding pots of \"\n    \"blooming red flowers.\\n\"\n    \"- User Prompt: A busy city street -> Enhanced: A bustling city street scene \"\n    \"at dusk, featuring glowing street lamps gradually lighting up, a diverse \"\n    \"crowd of people in colorful clothing walking past, and a double-decker bus \"\n    \"smoothly passing by towering glass skyscrapers.\\n\"\n    \"Please generate only the enhanced description for the prompt below and avoid \"\n    \"including any additional commentary or evaluations:\\n\"\n    \"User Prompt: \"\n)\n\n\ndef select_sana_video_prompt_window(\n    tensor: torch.Tensor, max_sequence_length: int\n) -> torch.Tensor:\n    \"\"\"Keep the BOS token and the final prompt window, matching Diffusers.\"\"\"\n    if tensor.shape[1] < max_sequence_length:\n        raise ValueError(\n            f\"Encoded prompt has {tensor.shape[1]} tokens, expected at least \"\n            f\"{max_sequence_length}\"\n        )\n    if max_sequence_length == 1:\n        return tensor[:, :1]\n    return torch.cat([tensor[:, :1], tensor[:, -(max_sequence_length - 1) :]], dim=1)\n\n\nclass SanaVideoTextEncodingStage(TextEncodingStage):\n    \"\"\"Apply SANA-Video's asymmetric positive/negative prompt encoding.\"\"\"\n\n    @staticmethod\n    def _normalize_text(text: str | list[str]) -> str | list[str]:\n        if isinstance(text, str):\n            return text.lower().strip()\n        return [item.lower().strip() for item in text]\n\n    def _encode_negative_text(self, batch, server_args, all_indices):","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/multimodal_gen/runtime/pipelines/sana_video.py#L28-L64","documentation":"Raised by select_sana_video_prompt_window when the encoded prompt tensor's sequence dimension (dim 1) is shorter than max_sequence_length. The windowing keeps the BOS token plus the final (max_sequence_length-1) tokens, which is impossible if the prompt has fewer tokens than the requested window.","triggerScenarios":"Calling select_sana_video_prompt_window(embeds, max_sequence_length=N) with embeds.shape[1] < N; during forward when a short text prompt is encoded but the model/config requests a larger prompt window; tests with tiny synthetic prompts.","commonSituations":"Short user prompts for SANA video generation; a config bumping max_sequence_length beyond the tokenizer/prompt-encoder output length; empty or truncated prompt preprocessing that drops tokens.","solutions":["Reduce max_sequence_length to <= the encoded prompt length (e.g. clamp it to tensor.shape[1])","Lengthen the text prompt so its encoding meets the window size","Fix upstream preprocessing that may be truncating or dropping the encoded prompt before the call"],"exampleFix":"# before\nwindow = select_sana_video_prompt_window(embeds, max_sequence_length=512)  # embeds has 120 tokens\n\n# after\nmax_len = min(512, embeds.shape[1])\nwindow = select_sana_video_prompt_window(embeds, max_sequence_length=max_len)","handlingStrategy":"validation","validationCode":"if tensor.shape[1] < max_sequence_length:\n    max_sequence_length = tensor.shape[1]  # or enforce a minimum prompt length upstream\nwindow = select_sana_video_prompt_window(tensor, max_sequence_length)","typeGuard":null,"tryCatchPattern":"try:\n    window = select_sana_video_prompt_window(embeds, max_len)\nexcept ValueError as e:\n    if 'expected at least' in str(e):\n        max_len = embeds.shape[1]\n        window = select_sana_video_prompt_window(embeds, max_len)\n    else:\n        raise","preventionTips":["Clamp max_sequence_length to the encoded prompt length at the call site","Enforce minimum prompt length in request validation"],"tags":["sana-video","prompt-window","sequence-length","validation","sglang"],"backgroundTag":"sequence-length-mismatch","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}