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 Gemma2 text encoder used during FLUX.2 PiD decode must be a transformers PreTrainedModel. The check runs right after model_on_device() context entry; a different class means the loaded encoder file is corrupted, incomplete, or not a Gemma model.
Source
Thrown at invokeai/app/invocations/flux2_pid_decode.py:173
with vae_info.model_on_device() as (_, vae):
config = getattr(vae, "config", None)
if config is not None and hasattr(config, "scaling_factor"):
scaling_factor = float(config.scaling_factor)
shift_factor = float(getattr(config, "shift_factor", None) or 0.0)
else:
scaling_factor = float(getattr(vae, "scale_factor", scaling_factor))
shift_factor = float(getattr(vae, "shift_factor", shift_factor))
del vae_info
TorchDevice.empty_cache()
# 3) 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
- Re-download the Gemma2 text encoder via the model manager
- Verify gemma2_encoder.text_encoder points to the correct Gemma model record
- Update transformers to a compatible version
- Re-scan/repair model records if hashes are stale
Example fix
// before: text_encoder -> generic llama ckpt ModelConfig(type='main', path='/models/llama-ckpt/') // after ModelConfig(type='main', path='/models/google/gemma-2-2b/', name='Gemma2 encoder')
Defensive patterns
Strategy: type-guard
Validate before calling
info = context.models.load(gemma2_encoder.text_encoder)
if not isinstance(info.model, PreTrainedModel):
raise TypeError(f'Gemma encoder invalid: {type(info.model).__name__}') Type guard
from transformers import PreTrainedModel
def is_gemma_encoder(obj) -> bool:
return isinstance(obj, PreTrainedModel) Try / catch
try:
result = pid_decode.invoke(context)
except TypeError as e:
if 'Gemma encoder' in str(e):
reimport_model_manager_entry(gemma2_encoder.text_encoder)
raise Prevention
- Verify Gemma downloads complete with matching hashes
- Point the encoder field at verified Gemma2 records
- Keep transformers compatible with Gemma2
When it happens
Trigger: gemma_text_encoder_info.model_on_device() yields an object that is not PreTrainedModel in the invoke ExitStack; the gemma2_encoder.text_encoder field references a wrong or damaged model record.
Common situations: Interrupted downloads of Gemma encoder weights; model config pointing at a non-Gemma directory; transformers version changes changing the wrapper class; converted/quantized models loaded as custom classes.
Related errors
- Expected PreTrainedModel for text encoder, got {type(text_en
- Expected LlavaOnevisionForConditionalGeneration, got {type(m
- Expected PreTrainedTokenizerBase for tokenizer, got {type(to
- Expected ModelPatchRaw for LoRA '{lora.lora.key}', got {type
- Expected PreTrainedTokenizerBase for Gemma tokenizer, got {t
AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29).
Data as JSON: /api/errors/4a19960a45539e31.
Report an issue: GitHub.