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

In the same invoke of z_image_pid_decode.py, the Gemma tokenizer loaded from the gemma2_encoder submodel must be a transformers PreTrainedTokenizerBase. If model_on_device returns anything else, a TypeError is raised with the actual type name. This ensures tokenization of captions uses a real HF tokenizer API.

Source

Thrown at invokeai/app/invocations/z_image_pid_decode.py:142

                    shift_factor = float(getattr(vae, "shift_factor", shift_factor))
            del vae_info
            TorchDevice.empty_cache()
        context.logger.info(
            f"Z-Image PiD decode: latent shape={tuple(latents.shape)} dtype={latents.dtype} "
            f"stats[min={latents.min().item():.3f} max={latents.max().item():.3f} "
            f"mean={latents.mean().item():.3f}] using scale={scaling_factor:.4f} shift={shift_factor:.4f}"
        )

        # 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

  1. Re-download the Gemma tokenizer files (tokenizer.json, tokenizer_config.json, special_tokens_map.json).
  2. Confirm the gemma2_encoder.tokenizer submodel reference points to a valid tokenizer model config.
  3. Update transformers/InvokeAI so tokenizers load as PreTrainedTokenizerBase.
  4. Re-import the Gemma encoder model through the model manager to rebuild tokenizer metadata.
Defensive patterns

Strategy: type-guard

Validate before calling

with context.models.load(tokenizer_key).model_on_device() as (_, tok):
    if not isinstance(tok, PreTrainedTokenizerBase):
        fail_fast(tok)

Type guard

def is_hf_tokenizer(obj) -> bool:
    from transformers import PreTrainedTokenizerBase
    return isinstance(obj, PreTrainedTokenizerBase)

Try / catch

try:
    decode(context)
except TypeError as e:
    if "Expected PreTrainedTokenizerBase for Gemma tokenizer" in str(e):
        reinstall_tokenizer_files()
    else:
        raise

Prevention

When it happens

Trigger: Invoking the PiD decode path where context.models.load(self.gemma2_encoder.tokenizer).model_on_device() yields an object failing isinstance(gemma_tokenizer, PreTrainedTokenizerBase).

Common situations: Tokenizer directory missing tokenizer.json/tokenizer_config.json so a fallback object is loaded; wrong submodel wired to the tokenizer field; incompatible transformers version or corrupted download.

Related errors


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