invoke-ai/InvokeAI · error · TypeError
Expected PreTrainedTokenizerBase for Gemma tokenizer, got {t
Error message
Expected PreTrainedTokenizerBase for Gemma tokenizer, got {type(gemma_tokenizer).__name__}. What it means
The same Gemma loading path also materializes the tokenizer and requires it to be a HuggingFace PreTrainedTokenizerBase before encoding prompts. A non-tokenizer object means the record referenced by gemma2_encoder.tokenizer is wrong or was loaded incorrectly, so a TypeError is raised before any caption encoding.
Source
Thrown at invokeai/app/invocations/sdxl_pid_decode.py:145
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,
)
caption_embs = caption_embs.detach().to("cpu")View on GitHub (pinned to 0b6a024f2f)
Solutions
- Re-point the Gemma2 encoder node's tokenizer field at the correct tokenizer record and re-run.
- Re-download/re-import the Gemma encoder so tokenizer files and records are rebuilt.
- Confirm tokenizer files (tokenizer.json/tokenizer.model) exist in the model folder.
- Verify the installed transformers version matches what InvokeAI expects.
Example fix
// before
(_, gemma_tokenizer) = stack.enter_context(gemma_tokenizer_info.model_on_device())
// after
(_, gemma_tokenizer) = stack.enter_context(gemma_tokenizer_info.model_on_device())
assert isinstance(gemma_tokenizer, PreTrainedTokenizerBase), f"bad tokenizer: {type(gemma_tokenizer).__name__}" Defensive patterns
Strategy: type-guard
Validate before calling
tok_path = gemma_model_dir / "tokenizer.json"
if not tok_path.exists():
raise FileNotFoundError("Gemma tokenizer files missing") Type guard
def is_tokenizer(obj) -> bool:
from transformers import PreTrainedTokenizerBase
return isinstance(obj, PreTrainedTokenizerBase) Try / catch
try:
result = invoke(context)
except TypeError as e:
if "Gemma tokenizer" in str(e):
reimport_gemma_tokenizer()
retry(context)
else:
raise Prevention
- Ensure tokenizer files ship with the Gemma model folder
- Re-select the tokenizer field after model re-imports
- Upgrade transformers only in step with InvokeAI releases
When it happens
Trigger: model_on_device() for the tokenizer yields an object that fails isinstance(obj, PreTrainedTokenizerBase) — e.g. the tokenizer field points at a model checkpoint instead of a tokenizer record, or the loader returned an unexpected wrapper.
Common situations: Wiring a raw model into the tokenizer field; tokenizer files missing so the loader fell back to a default object; transformers version changes altering tokenizer classes; corrupted tokenizer record in the DB.
Related errors
- Expected PreTrainedTokenizerBase for tokenizer, got {type(to
- Expected PreTrainedTokenizerBase for Gemma tokenizer, got {t
- Expected PreTrainedModel for Gemma encoder, got {type(gemma_
- Expected PreTrainedTokenizerBase for Gemma tokenizer, got {t
- Expected PreTrainedTokenizerBase for tokenizer, got {type(to
AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29).
Data as JSON: /api/errors/456445c55d3d985f.
Report an issue: GitHub.