sgl-project/sglang · error · ValueError

`negative_prompt`: {negative_prompt} has batch size {len(neg

Error message

`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`: {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches the batch size of `prompt`.

What it means

When negative_prompt is a list, its length must equal the batch size implied by prompt. This ValueError is raised right after the type check when len(negative_prompt) != batch_size, because each batch item needs its own negative prompt embedding.

Source

Thrown at python/sglang/multimodal_gen/runtime/pipelines_core/stages/model_specific_stages/glm_image.py:1032

        prompt_embeds = prompt_embeds.repeat(1, 1, 1)
        prompt_embeds = prompt_embeds.reshape(1, seq_len, -1)

        negative_prompt_embeds = None
        if do_classifier_free_guidance:
            negative_prompt = ""
            negative_prompt = (
                batch_size * [negative_prompt]
                if isinstance(negative_prompt, str)
                else negative_prompt
            )

            if prompt is not None and type(prompt) is not type(negative_prompt):
                raise TypeError(
                    f"`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} !="
                    f" {type(prompt)}."
                )
            elif batch_size != len(negative_prompt):
                raise ValueError(
                    f"`negative_prompt`: {negative_prompt} has batch size {len(negative_prompt)}, but `prompt`:"
                    f" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"
                    " the batch size of `prompt`."
                )

            negative_prompt_embeds = self._get_glyph_embeds(
                negative_prompt, max_sequence_length, device, dtype
            )

            seq_len = negative_prompt_embeds.size(1)
            negative_prompt_embeds = negative_prompt_embeds.repeat(1, 1, 1)
            negative_prompt_embeds = negative_prompt_embeds.reshape(1, seq_len, -1)

        return prompt_embeds, negative_prompt_embeds

    def prepare_latents(
        self,
        batch_size,

View on GitHub (pinned to 0132848349)

Solutions

  1. Repeat the negative prompt to match batch size: negative_prompt * batch_size or [neg] * len(prompt)
  2. Compute batch size from prompt before building the negative list
  3. Wrap in a caller-side check comparing len(prompt) and len(negative_prompt)

Example fix

# before
pipe(prompt=["a cat", "a dog", "a bird"], negative_prompt=["blurry"])

# after
prompts = ["a cat", "a dog", "a bird"]
pipe(prompt=prompts, negative_prompt=["blurry"] * len(prompts))
Defensive patterns

Strategy: validation

Validate before calling

if isinstance(negative_prompt, list) and isinstance(prompt, list):
    if len(negative_prompt) != len(prompt):
        negative_prompt = negative_prompt[:1] * len(prompt)  # broadcast single neg

Try / catch

except ValueError as e:
    if "batch size" in str(e) and isinstance(negative_prompt, list):
        negative_prompt = negative_prompt * batch_size
        retry_call()
    else:
        raise

Prevention

When it happens

Trigger: Calling forward() with prompt as a list of length N but negative_prompt as a list of length M != N (including the common single-element ["blurry"] against a multi-prompt batch).

Common situations: Batching prompts dynamically while keeping a hardcoded one-element negative prompt list; appending prompts to a batch without extending the negative prompt list.

Related errors


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