{"record":{"id":"456445c55d3d985f","repo":"invoke-ai/InvokeAI","slug":"expected-pretrainedtokenizerbase-for-gemma-tokeniz-456445","errorCode":null,"errorMessage":"Expected PreTrainedTokenizerBase for Gemma tokenizer, got {type(gemma_tokenizer).__name__}.","messagePattern":"Expected PreTrainedTokenizerBase for Gemma tokenizer, got (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/sdxl_pid_decode.py","lineNumber":145,"sourceCode":"                    scaling_factor = float(getattr(vae, \"scale_factor\", scaling_factor))\n                    shift_factor = float(getattr(vae, \"shift_factor\", shift_factor))\n            del vae_info\n            TorchDevice.empty_cache()\n        context.logger.info(\n            f\"SDXL PiD decode: latent shape={tuple(latents.shape)} (expect [B, 4, H/8, W/8]) dtype={latents.dtype} \"\n            f\"using scale={scaling_factor:.5f} shift={shift_factor:.5f}\"\n        )\n\n        # 2) Encode caption with Gemma-2.\n        gemma_text_encoder_info = context.models.load(self.gemma2_encoder.text_encoder)\n        gemma_tokenizer_info = context.models.load(self.gemma2_encoder.tokenizer)\n        with ExitStack() as stack:\n            (_, gemma_encoder) = stack.enter_context(gemma_text_encoder_info.model_on_device())\n            (_, gemma_tokenizer) = stack.enter_context(gemma_tokenizer_info.model_on_device())\n            if not isinstance(gemma_encoder, PreTrainedModel):\n                raise TypeError(f\"Expected PreTrainedModel for Gemma encoder, got {type(gemma_encoder).__name__}.\")\n            if not isinstance(gemma_tokenizer, PreTrainedTokenizerBase):\n                raise TypeError(\n                    f\"Expected PreTrainedTokenizerBase for Gemma tokenizer, got {type(gemma_tokenizer).__name__}.\"\n                )\n\n            # Encode on the encoder's intended compute device. compute_device honours cpu_only and is\n            # stable under partial loading — the first parameter may be offloaded to CPU while later\n            # modules load on CUDA, so inferring the device from the first parameter could place caption\n            # inputs on the wrong device.\n            device = gemma_text_encoder_info.compute_device\n            encode_dtype = TorchDevice.choose_bfloat16_safe_dtype(device)\n            context.util.signal_progress(\"Encoding caption with Gemma-2\")\n            caption_embs, caption_mask = encode_caption_for_pid(\n                [self.prompt],\n                tokenizer=gemma_tokenizer,\n                encoder=gemma_encoder,\n                device=device,\n                dtype=encode_dtype,\n            )\n            caption_embs = caption_embs.detach().to(\"cpu\")","sourceCodeStart":127,"sourceCodeEnd":163,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/sdxl_pid_decode.py#L127-L163","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\n(_, gemma_tokenizer) = stack.enter_context(gemma_tokenizer_info.model_on_device())\n// after\n(_, gemma_tokenizer) = stack.enter_context(gemma_tokenizer_info.model_on_device())\nassert isinstance(gemma_tokenizer, PreTrainedTokenizerBase), f\"bad tokenizer: {type(gemma_tokenizer).__name__}\"","handlingStrategy":"type-guard","validationCode":"tok_path = gemma_model_dir / \"tokenizer.json\"\nif not tok_path.exists():\n    raise FileNotFoundError(\"Gemma tokenizer files missing\")","typeGuard":"def is_tokenizer(obj) -> bool:\n    from transformers import PreTrainedTokenizerBase\n    return isinstance(obj, PreTrainedTokenizerBase)","tryCatchPattern":"try:\n    result = invoke(context)\nexcept TypeError as e:\n    if \"Gemma tokenizer\" in str(e):\n        reimport_gemma_tokenizer()\n        retry(context)\n    else:\n        raise","preventionTips":["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"],"tags":["type-check","tokenizer","huggingface"],"backgroundTag":"model-type-mismatch","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}