{"record":{"id":"677267d9f22ddbe0","repo":"invoke-ai/InvokeAI","slug":"expected-pretrainedtokenizerbase-for-gemma-tokeniz-677267","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/flux_pid_decode.py","lineNumber":102,"sourceCode":"\n        # Fail fast if the connected decoder is for a different backbone (the base-agnostic loader lets\n        # the Nodes editor wire any PiD decoder into this FLUX-specific node).\n        assert_pid_decoder_matches_base(\n            context.models.get_config(self.pid_decoder.decoder).base,\n            BaseModelType.Flux,\n            node_title=\"FLUX PiD Decode\",\n        )\n\n        # 1) 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":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/flux_pid_decode.py#L84-L120","documentation":"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.","triggerScenarios":"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.","commonSituations":"Malformed Gemma encoder install where the tokenizer component resolves to the wrong artifact; using a non-HF tokenizer wrapper; transformers version regressions.","solutions":["Verify the tokenizer record for the Gemma2 encoder resolves to a HF tokenizer and re-select it","Redownload/reinstall the Gemma encoder model so tokenizer files are complete","Check transformers version compatibility (PreTrainedTokenizerBase import and class hierarchy)","Inspect the loaded object's type to identify what is actually returned"],"exampleFix":"// before: tokenizer field pointing at wrong artifact\ntokenizer=<model_record_of_encoder_weights>\n// after\ntokenizer=<gemma tokenizer model record>","handlingStrategy":"type-guard","validationCode":"from transformers import PreTrainedTokenizerBase\ninfo = context.models.load(gemma2_encoder.tokenizer)\n# confirm the tokenizer record resolves to a HF tokenizer before invoking","typeGuard":"def is_hf_tokenizer(obj) -> bool:\n    from transformers import PreTrainedTokenizerBase\n    return isinstance(obj, PreTrainedTokenizerBase)","tryCatchPattern":"try:\n    output = pid_decode.invoke(context)\nexcept TypeError as e:\n    if 'Expected PreTrainedTokenizerBase' in str(e):\n        # fix the tokenizer model record / reinstall Gemma encoder\n        pass\n    else:\n        raise","preventionTips":["Verify the tokenizer component of the Gemma encoder install is intact","Don't substitute non-HF tokenizer wrappers","Check transformers class hierarchy after upgrades"],"tags":["type-error","gemma","tokenizer","flux","invokeai"],"backgroundTag":"unexpected-model-type","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}