Comfy-Org/ComfyUI · error · ValueError

JoyImageTEModel: encoded sequence length {out.shape[1]} is s

Error message

JoyImageTEModel: encoded sequence length {out.shape[1]} is shorter than drop_idx={JOYIMAGE_DROP_IDX}; the prompt did not include the template prefix.

What it means

JoyImage's Qwen3-VL-based text encoder expects the prompt to start with a fixed template prefix of 34 tokens (JOYIMAGE_DROP_IDX = 34), which encode_token_weights strips after encoding. If the encoded sequence is <= 34 tokens, the prefix was never added (or the tokenizer/te was swapped), so slicing would destroy the whole sequence and the code raises instead of producing garbage conditioning.

Source

Thrown at comfy/text_encoders/joyimage.py:77

            device=device, layer=layer, layer_idx=layer_idx, textmodel_json_config={},
            # JoyImage conditions on the pre-final-norm output of the last decoder layer.
            dtype=dtype, special_tokens={"pad": PAD_TOKEN}, layer_norm_hidden_state=False,
            model_class=Qwen3VL8B_JoyImage, enable_attention_masks=attention_mask,
            return_attention_masks=attention_mask, model_options=model_options,
        )


class JoyImageTEModel(sd1_clip.SD1ClipModel):
    def __init__(self, device="cpu", dtype=None, model_options={}):
        super().__init__(
            device=device, dtype=dtype, name="qwen3vl_8b",
            clip_model=_JoyImageClipModel, model_options=model_options,
        )

    def encode_token_weights(self, token_weight_pairs):
        out, pooled, extra = super().encode_token_weights(token_weight_pairs)
        if out.shape[1] <= JOYIMAGE_DROP_IDX:
            raise ValueError(
                f"JoyImageTEModel: encoded sequence length {out.shape[1]} is shorter "
                f"than drop_idx={JOYIMAGE_DROP_IDX}; the prompt did not include the "
                f"template prefix."
            )
        out = out[:, JOYIMAGE_DROP_IDX:]
        if "attention_mask" in extra:
            extra["attention_mask"] = extra["attention_mask"][:, JOYIMAGE_DROP_IDX:]
        return out, pooled, extra


def te(dtype_llama=None, llama_quantization_metadata=None):
    class JoyImageTEModel_(JoyImageTEModel):
        def __init__(self, device="cpu", dtype=None, model_options={}):
            if llama_quantization_metadata is not None:
                model_options = model_options.copy()
                model_options["quantization_metadata"] = llama_quantization_metadata
            if dtype_llama is not None:
                dtype = dtype_llama

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Use the JoyImage-provided node/wrapper that builds the caption template prompt so the 34-token prefix is present before encoding
  2. If calling encode_token_weights directly, prepend the expected JoyCaption template prefix to the prompt text
  3. Verify the tokenizer bundled with the encoder matches (qwen3vl_8b); a wrong tokenizer yields short sequences and the same failure

Example fix

// before
out, pooled, extra = te_model.encode_token_weights([[(t, 1.0) for t in plain_tokens]])

# after
prompt = joyimage_build_template(plain_text)  # adds the 34-token template prefix
out, pooled, extra = te_model.encode_token_weights([[(t, 1.0) for t in tokenize(prompt)]])
Defensive patterns

Strategy: validation

Validate before calling

from comfy.text_encoders.joyimage import JOYIMAGE_DROP_IDX
n = out.shape[1]
assert n > JOYIMAGE_DROP_IDX, f'prompt missing template prefix ({n} <= {JOYIMAGE_DROP_IDX} tokens)'

Prevention

When it happens

Trigger: Calling JoyImageTEModel.encode_token_weights on token weights whose encoded length is <= 34 tokens — typically because the JoyCaption template prompt ('<|begin_of_text|>...description...') was not prepended by the loader/wrapper before calling the model.

Common situations: User wired a generic CLIPTextEncode-style node directly to the JoyImage encoder, bypassing the template-building wrapper; user overrode tokenize_with_weights without the template; mismatched tokenizer produced far fewer tokens than expected.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/2648568af06fdef1. Report an issue: GitHub.