sgl-project/sglang · error · TypeError

`negative_prompt` should be the same type to `prompt`, but g

Error message

`negative_prompt` should be the same type to `prompt`, but got {type(negative_prompt)} != {type(prompt)}.

What it means

Diffusers-style input validation in encode_prompt: negative_prompt must have the exact same Python type as prompt (both str, or both list). Passing a list negative_prompt with a str prompt (or vice versa) raises TypeError before any encoding happens.

Source

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

            prompt_embeds = self._get_glyph_embeds(
                prompt, max_sequence_length, device, dtype
            )

        seq_len = prompt_embeds.size(1)
        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)

View on GitHub (pinned to 0132848349)

Solutions

  1. Make both arguments the same type: either both plain str or both list
  2. For batched runs, pass negative_prompt as a list matching the prompt list length
  3. Add a normalization step in caller code: negative_prompt = [negative_prompt] if isinstance(negative_prompt, str) and isinstance(prompt, list) else negative_prompt

Example fix

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

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

Strategy: type-guard

Validate before calling

if prompt is not None and negative_prompt is not None:
    assert type(prompt) is type(negative_prompt), (
        f"{type(prompt)} vs {type(negative_prompt)}"
    )

Type guard

def prompt_types_match(prompt, negative_prompt) -> bool:
    if prompt is None or negative_prompt is None:
        return True
    if isinstance(prompt, str):
        return isinstance(negative_prompt, str)
    return isinstance(negative_prompt, list)

Try / catch

except TypeError as e:
    if "same type" in str(e):
        negative_prompt = [negative_prompt] if isinstance(prompt, list) else str(negative_prompt)
        retry_call()
    else:
        raise

Prevention

When it happens

Trigger: Calling forward()/encode_prompt with prompt="a cat" (str) and negative_prompt=["blurry"] (list), or prompt=["a cat","a dog"] with negative_prompt="blurry" (str).

Common situations: Copy-pasting examples that build negative prompts as lists for batching but use a scalar prompt; dynamically switching between single and batched inference while reusing a negative_prompt variable; UI code that always wraps user text in a list.

Related errors


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