sgl-project/sglang · error · ValueError

Encoded prompt has {tensor.shape[1]} tokens, expected at lea

Error message

Encoded prompt has {tensor.shape[1]} tokens, expected at least {max_sequence_length}

What it means

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.

Source

Thrown at python/sglang/multimodal_gen/runtime/pipelines/sana_video.py:46

    "settling into a curled position, peacefully falling asleep on a warm sunny "
    "windowsill, with gentle sunlight filtering through surrounding pots of "
    "blooming red flowers.\n"
    "- User Prompt: A busy city street -> Enhanced: A bustling city street scene "
    "at dusk, featuring glowing street lamps gradually lighting up, a diverse "
    "crowd of people in colorful clothing walking past, and a double-decker bus "
    "smoothly passing by towering glass skyscrapers.\n"
    "Please generate only the enhanced description for the prompt below and avoid "
    "including any additional commentary or evaluations:\n"
    "User Prompt: "
)


def select_sana_video_prompt_window(
    tensor: torch.Tensor, max_sequence_length: int
) -> torch.Tensor:
    """Keep the BOS token and the final prompt window, matching Diffusers."""
    if tensor.shape[1] < max_sequence_length:
        raise ValueError(
            f"Encoded prompt has {tensor.shape[1]} tokens, expected at least "
            f"{max_sequence_length}"
        )
    if max_sequence_length == 1:
        return tensor[:, :1]
    return torch.cat([tensor[:, :1], tensor[:, -(max_sequence_length - 1) :]], dim=1)


class SanaVideoTextEncodingStage(TextEncodingStage):
    """Apply SANA-Video's asymmetric positive/negative prompt encoding."""

    @staticmethod
    def _normalize_text(text: str | list[str]) -> str | list[str]:
        if isinstance(text, str):
            return text.lower().strip()
        return [item.lower().strip() for item in text]

    def _encode_negative_text(self, batch, server_args, all_indices):

View on GitHub (pinned to 0132848349)

Solutions

  1. Reduce max_sequence_length to <= the encoded prompt length (e.g. clamp it to tensor.shape[1])
  2. Lengthen the text prompt so its encoding meets the window size
  3. Fix upstream preprocessing that may be truncating or dropping the encoded prompt before the call

Example fix

# before
window = select_sana_video_prompt_window(embeds, max_sequence_length=512)  # embeds has 120 tokens

# after
max_len = min(512, embeds.shape[1])
window = select_sana_video_prompt_window(embeds, max_sequence_length=max_len)
Defensive patterns

Strategy: validation

Validate before calling

if tensor.shape[1] < max_sequence_length:
    max_sequence_length = tensor.shape[1]  # or enforce a minimum prompt length upstream
window = select_sana_video_prompt_window(tensor, max_sequence_length)

Try / catch

try:
    window = select_sana_video_prompt_window(embeds, max_len)
except ValueError as e:
    if 'expected at least' in str(e):
        max_len = embeds.shape[1]
        window = select_sana_video_prompt_window(embeds, max_len)
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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