sgl-project/sglang · error · KeyError

Unknown token_type {token_type}, only support "text" or "ima

Error message

Unknown token_type {token_type}, only support "text" or "image".

What it means

Longcat image pipeline's _prepare_pos_ids builds (modality, row, col) position ids and only accepts token_type of "text" or "image". Any other value raises this KeyError.

Source

Thrown at python/sglang/multimodal_gen/configs/pipeline_configs/longcat_image.py:110

    num_token=None,
    height=None,
    width=None,
):
    if token_type == "text":
        assert num_token
        pos_ids = torch.zeros(num_token, 3)
        pos_ids[..., 0] = modality_id
        pos_ids[..., 1] = torch.arange(num_token) + start[0]
        pos_ids[..., 2] = torch.arange(num_token) + start[1]
    elif token_type == "image":
        assert height and width
        pos_ids = torch.zeros(height, width, 3)
        pos_ids[..., 0] = modality_id
        pos_ids[..., 1] = pos_ids[..., 1] + torch.arange(height)[:, None] + start[0]
        pos_ids[..., 2] = pos_ids[..., 2] + torch.arange(width)[None, :] + start[1]
        pos_ids = pos_ids.reshape(height * width, 3)
    else:
        raise KeyError(
            f'Unknown token_type {token_type}, only support "text" or "image".'
        )
    return pos_ids


def _tokenize_prompt_for_encode(prompt, tokenizer):
    """Quote-aware tokenization mirroring diffusers LongCatImagePipeline._encode_prompt.

    Quoted substrings are tokenized character-by-character; unquoted substrings
    are tokenized whole. Truncated/padded to TOKENIZER_MAX_LENGTH. Returns the
    padded (input_ids, attention_mask) for the prompt body (without prefix/suffix).
    """
    if isinstance(prompt, str):
        prompt = [prompt]

    batch_all_tokens = []
    for each_prompt in prompt:
        all_tokens = []

View on GitHub (pinned to 0132848349)

Solutions

  1. Use exactly "text" or "image" (lowercase) as token_type
  2. Check for typos/case: "Image", "TEXT", "img" are all invalid
  3. If you need a new modality, extend _prepare_pos_ids's branch handling rather than passing an unknown token_type

Example fix

# before
pos_ids = cfg._prepare_pos_ids(token_type="img", ...)

# after
pos_ids = cfg._prepare_pos_ids(token_type="image", ...)
Defensive patterns

Strategy: validation

Validate before calling

assert token_type in ("text", "image"), f"bad token_type: {token_type!r}"

Type guard

def is_supported_token_type(t: str) -> bool:
    return t in ("text", "image")

Try / catch

except KeyError as e:
    if "Unknown token_type" in str(e):
        token_type = token_type.lower()
        # remap aliases then retry or fail fast with context

Prevention

When it happens

Trigger: Calling the internal ID-preparation path (maybe_prepare_latent_ids, prepare_pos_cond_kwargs, prepare_neg_cond_kwargs, or _edit_img_ids) with a token_type other than "text"/"image" — e.g. "video", "Text", "img", or None.

Common situations: Extending the pipeline to new modalities and passing a new token type; typos or case mismatches ("Image" vs "image"); copying code from another pipeline that uses different token_type names.

Related errors


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