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
This invocation loads the Gemma text encoder onto the device via model_on_device() and asserts the materialized object is a HuggingFace PreTrainedModel before decoding. If the loaded model is not that type, the underlying model record was built/loaded incorrectly and decoding would fail downstream, so a TypeError is raised immediately.
Source
Thrown at invokeai/app/invocations/sdxl_pid_decode.py:143
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()
context.logger.info(
f"SDXL PiD decode: latent shape={tuple(latents.shape)} (expect [B, 4, H/8, W/8]) dtype={latents.dtype} "
f"using scale={scaling_factor:.5f} shift={shift_factor:.5f}"
)
# 2) 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
- Point the Gemma2 encoder node's text_encoder field at the correct Gemma text-encoder model record and re-run.
- Delete and re-download/re-import the Gemma encoder so its record and loader config are rebuilt.
- Verify the transformers library version can instantiate the model as PreTrainedModel; upgrade/downgrade as needed.
- If you control the loader, assert the loaded class type before returning from model_on_device.
Example fix
// before
(_, gemma_encoder) = stack.enter_context(gemma_text_encoder_info.model_on_device())
// after
(_, gemma_encoder) = stack.enter_context(gemma_text_encoder_info.model_on_device())
assert isinstance(gemma_encoder, PreTrainedModel), f"bad encoder: {type(gemma_encoder).__name__}" Defensive patterns
Strategy: type-guard
Validate before calling
# before invoking, confirm the field resolves to a HF encoder record
info = context.models.load(node.gemma2_encoder.text_encoder)
if info.hash is None:
raise LookupError("Gemma encoder record invalid") Type guard
def is_pretrained_model(obj) -> bool:
from transformers import PreTrainedModel
return isinstance(obj, PreTrainedModel) Try / catch
try:
result = invoke(context)
except TypeError as e:
if "Gemma encoder" in str(e):
reimport_gemma_encoder()
retry(context)
else:
raise Prevention
- Pin the transformers version InvokeAI expects
- Re-download Gemma models through the UI, not manual file copies
- Verify model records' types after DB migrations
When it happens
Trigger: context.models.load(self.gemma2_encoder.text_encoder) resolves to a record whose loaded object is not a PreTrainedModel instance — e.g. the field points at the wrong submodel/model type, a loader returned a raw state dict, or the model class for the record is misconfigured.
Common situations: Hand-edited or migrated model-manager records; pointing the Gemma encoder node at a non-encoder checkpoint; a plugin/loader bug returning a wrapper object; loading with an incompatible transformers version.
Related errors
- Expected PreTrainedTokenizerBase for Gemma tokenizer, got {t
- Expected PidNet for PiD decoder, got {type(pid_net).__name__
- Expected PreTrainedModel for text encoder, got {type(text_en
- Expected PreTrainedTokenizerBase for tokenizer, got {type(to
- Expected ModelPatchRaw for LoRA '{lora.lora.key}', got {type
AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29).
Data as JSON: /api/errors/68738b4ec31d83e6.
Report an issue: GitHub.