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

Companion check to the encoder guard: the loaded Gemma tokenizer must be an instance of transformers.PreTrainedTokenizerBase. If model_on_device() yields a different object (wrong class, custom tokenizer, corrupted record), invoke() raises TypeError before encoding captions.

Source

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

        # 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,
            )
            caption_embs = caption_embs.detach().to("cpu")

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Verify the tokenizer record for the Gemma2 encoder resolves to a HF tokenizer and re-select it
  2. Redownload/reinstall the Gemma encoder model so tokenizer files are complete
  3. Check transformers version compatibility (PreTrainedTokenizerBase import and class hierarchy)
  4. Inspect the loaded object's type to identify what is actually returned

Example fix

// before: tokenizer field pointing at wrong artifact
tokenizer=<model_record_of_encoder_weights>
// after
tokenizer=<gemma tokenizer model record>
Defensive patterns

Strategy: type-guard

Validate before calling

from transformers import PreTrainedTokenizerBase
info = context.models.load(gemma2_encoder.tokenizer)
# confirm the tokenizer record resolves to a HF tokenizer before invoking

Type guard

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

Try / catch

try:
    output = pid_decode.invoke(context)
except TypeError as e:
    if 'Expected PreTrainedTokenizerBase' in str(e):
        # fix the tokenizer model record / reinstall Gemma encoder
        pass
    else:
        raise

Prevention

When it happens

Trigger: invoke() loads self.gemma2_encoder.tokenizer and the yielded object is not a PreTrainedTokenizerBase — e.g., the tokenizer field points at a non-tokenizer model record, or a custom/fast-tokenizer mismatch in the installed transformers version.

Common situations: Malformed Gemma encoder install where the tokenizer component resolves to the wrong artifact; using a non-HF tokenizer wrapper; transformers version regressions.

Related errors


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