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__}.

What it means

_encode_prompt type-checks the loaded text encoder object, requiring it to be a transformers PreTrainedModel. The model manager returned some other object type (wrong model format, wrong model class, or a placeholder), so it raises a TypeError naming the actual type received.

Source

Thrown at invokeai/app/invocations/anima_text_encoder.py:146

            # Use the encoder's intended compute device, not its current parameter residency: partial loading may
            # have temporarily offloaded all weights to RAM, which would wrongly run the whole encode on the CPU (see
            # #9373). Qwen3 is fully autocast-capable, so nothing pins it to the compute device otherwise.
            device = text_encoder_info.compute_device

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

            if not isinstance(text_encoder, PreTrainedModel):
                raise TypeError(f"Expected PreTrainedModel for text encoder, got {type(text_encoder).__name__}.")
            if not isinstance(tokenizer, PreTrainedTokenizerBase):
                raise TypeError(f"Expected PreTrainedTokenizerBase for tokenizer, got {type(tokenizer).__name__}.")

            context.util.signal_progress("Running Qwen3 0.6B text encoder")

            # Anima uses base Qwen3 (not instruct) — tokenize directly, no chat template.
            # A safety cap is applied to prevent GPU OOM on extremely long prompts.
            text_inputs = tokenizer(
                prompt,
                padding=False,
                truncation=True,
                max_length=QWEN3_MAX_SEQ_LEN,
                return_attention_mask=True,
                return_tensors="pt",
            )

            text_input_ids = text_inputs.input_ids
            attention_mask = text_inputs.attention_mask

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Re-convert/re-import the Qwen3 text encoder model so it is stored as a standard HF PreTrainedModel.
  2. Verify the model manager record's type matches a Qwen3 text-encoder model, not another model class.
  3. Update the transformers library to a version compatible with the stored model format and reload.
Defensive patterns

Strategy: type-guard

Validate before calling

from transformers import PreTrainedModel
info = context.models.load(text_encoder_key)
if not isinstance(info.model, PreTrainedModel):
    print(f"Text encoder is {type(info.model).__name__}, re-import required")

Type guard

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

Try / catch

try:
    result = invocation.invoke(context)
except TypeError as e:
    if "Expected PreTrainedModel for text encoder" in str(e):
        reimport_text_encoder_model()
    else:
        raise

Prevention

When it happens

Trigger: During invoke → _encode_prompt, after loading the Qwen3 text encoder via context.models, when isinstance(text_encoder, PreTrainedModel) fails — e.g. the model record points to a non-PreTrainedModel artifact or the wrong model type was loaded for the key.

Common situations: Corrupted or misconfigured model conversion; loading a model saved in a custom format; a model-manager record whose config/type does not match the actual on-disk weights; version drift between transformers and the stored model format.

Related errors


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