invoke-ai/InvokeAI · error · TypeError

Expected PreTrainedModel for text encoder, got {type(text_en

Error message

Expected PreTrainedModel for text encoder, got {type(text_encoder).__name__}. The Qwen3 encoder model may be corrupted or incompatible.

What it means

In z_image_text_encoder.py:_encode_prompt, the loaded Qwen3 text encoder is checked with isinstance(text_encoder, PreTrainedModel) before running. If the model loader returns a different type, a TypeError is raised with the actual class name and a note that the Qwen3 encoder may be corrupted or incompatible. This prevents calling a forward pass on a non-transformers object.

Source

Thrown at invokeai/app/invocations/z_image_text_encoder.py:107

                context.logger.warning(
                    f"Recovered {repaired_tensors} required Qwen3 tensor(s) onto {device} after a partial device mismatch."
                )

            # Apply LoRA models to the text encoder
            lora_dtype = TorchDevice.choose_bfloat16_safe_dtype(device)
            exit_stack.enter_context(
                LayerPatcher.apply_smart_model_patches(
                    model=text_encoder,
                    patches=self._lora_iterator(context),
                    prefix=Z_IMAGE_LORA_QWEN3_PREFIX,
                    dtype=lora_dtype,
                    cached_weights=cached_weights,
                )
            )

            context.util.signal_progress("Running Qwen3 text encoder")
            if not isinstance(text_encoder, PreTrainedModel):
                raise TypeError(
                    f"Expected PreTrainedModel for text encoder, got {type(text_encoder).__name__}. "
                    "The Qwen3 encoder model may be corrupted or incompatible."
                )
            if not isinstance(tokenizer, PreTrainedTokenizerBase):
                raise TypeError(
                    f"Expected PreTrainedTokenizerBase for tokenizer, got {type(tokenizer).__name__}. "
                    "The Qwen3 tokenizer may be corrupted or incompatible."
                )

            # Apply chat template similar to diffusers ZImagePipeline
            # The chat template formats the prompt for the Qwen3 model
            try:
                prompt_formatted = tokenizer.apply_chat_template(
                    [{"role": "user", "content": prompt}],
                    tokenize=False,
                    add_generation_prompt=True,
                    enable_thinking=True,
                )

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Re-download or repair the Qwen3 text encoder model files.
  2. Re-import the Z-Image model ensuring the text_encoder submodel is the standard HF Qwen3 encoder.
  3. Upgrade transformers and InvokeAI to compatible versions so the encoder loads as PreTrainedModel.
  4. Check that the invocation's text_encoder submodel reference points at the intended Qwen3 model.
Defensive patterns

Strategy: type-guard

Validate before calling

with text_encoder_info.model_on_device() as (_, enc):
    if not isinstance(enc, PreTrainedModel):
        fail_fast(enc)

Type guard

def is_pretrained_model(obj) -> bool:
    from transformers import PreTrainedModel
    return isinstance(obj, PreTrainedModel)

Try / catch

try:
    encode(context)
except TypeError as e:
    if "Expected PreTrainedModel for text encoder" in str(e):
        repair_qwen3_encoder()
    else:
        raise

Prevention

When it happens

Trigger: Calling the Z-Image text encoder invocation where context.models.load(...).model_on_device() for the Qwen3 encoder returns an object failing isinstance(..., PreTrainedModel).

Common situations: Corrupted/partially downloaded Qwen3 encoder; incompatible conversion of the Qwen3 checkpoint; transformers version mismatch; wrong submodel bound to the text_encoder field of the invocation.

Related errors


AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29). Data as JSON: /api/errors/bf11a0854d916504. Report an issue: GitHub.