invoke-ai/InvokeAI · error · TypeError

Expected PreTrainedModel for Gemma encoder, got {type(gemma_

Error message

Expected PreTrainedModel for Gemma encoder, got {type(gemma_encoder).__name__}.

What it means

The PiD decode invocation loads the Gemma text encoder onto device and asserts it is a transformers PreTrainedModel. If model_on_device() returns a different wrapper/type (e.g., a partial loader, quantized wrapper, or wrong model class), it raises TypeError. This guards against feeding captioning through an incompatible encoder object.

Source

Thrown at invokeai/app/invocations/flux_pid_decode.py:100

    def invoke(self, context: InvocationContext) -> ImageOutput:
        latents = context.tensors.load(self.latents.latents_name)

        # Fail fast if the connected decoder is for a different backbone (the base-agnostic loader lets
        # the Nodes editor wire any PiD decoder into this FLUX-specific node).
        assert_pid_decoder_matches_base(
            context.models.get_config(self.pid_decoder.decoder).base,
            BaseModelType.Flux,
            node_title="FLUX PiD Decode",
        )

        # 1) Encode caption with Gemma-2.
        gemma_text_encoder_info = context.models.load(self.gemma2_encoder.text_encoder)
        gemma_tokenizer_info = context.models.load(self.gemma2_encoder.tokenizer)
        with ExitStack() as stack:
            (_, gemma_encoder) = stack.enter_context(gemma_text_encoder_info.model_on_device())
            (_, gemma_tokenizer) = stack.enter_context(gemma_tokenizer_info.model_on_device())
            if not isinstance(gemma_encoder, PreTrainedModel):
                raise TypeError(f"Expected PreTrainedModel for Gemma encoder, got {type(gemma_encoder).__name__}.")
            if not isinstance(gemma_tokenizer, PreTrainedTokenizerBase):
                raise TypeError(
                    f"Expected PreTrainedTokenizerBase for Gemma tokenizer, got {type(gemma_tokenizer).__name__}."
                )

            # Encode on the encoder's intended compute device. compute_device honours cpu_only and is
            # stable under partial loading — the first parameter may be offloaded to CPU while later
            # modules load on CUDA, so inferring the device from the first parameter could place caption
            # inputs on the wrong device.
            device = gemma_text_encoder_info.compute_device
            encode_dtype = TorchDevice.choose_bfloat16_safe_dtype(device)
            context.util.signal_progress("Encoding caption with Gemma-2")
            caption_embs, caption_mask = encode_caption_for_pid(
                [self.prompt],
                tokenizer=gemma_tokenizer,
                encoder=gemma_encoder,
                device=device,
                dtype=encode_dtype,

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Verify the Gemma2 encoder model record is a valid transformers text-encoder model and re-select it
  2. Reinstall/redownload the Gemma encoder model (files may be corrupt or partially loaded)
  3. Check that installed transformers/diffusers versions return PreTrainedModel from model_on_device
  4. Inspect what type is registered for that model key in the Model Manager

Example fix

// before: node pointed at a generic/quantized encoder record
gemma2_encoder=<quantized_wrapper_model>
// after
gemma2_encoder=<proper Gemma2 text-encoder model record>
Defensive patterns

Strategy: type-guard

Validate before calling

info = context.models.load(gemma2_encoder.text_encoder)
# confirm the record is a transformers-based text encoder before invoking

Type guard

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

Try / catch

try:
    output = pid_decode.invoke(context)
except TypeError as e:
    if 'Expected PreTrainedModel' in str(e):
        # re-select or reinstall the Gemma encoder
        pass
    else:
        raise

Prevention

When it happens

Trigger: invoke() loads self.gemma2_encoder.text_encoder and the object yielded by model_on_device() is not an instance of PreTrainedModel — wrong model class registered under that key, corrupted/partial load, or a custom model implementation.

Common situations: Pointing the node at a non-Gemma/non-transformers model record; a model manager wrapper returning a optimized/quantized object that isn't a PreTrainedModel; transformers version where the loaded class differs.

Related errors


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