{"record":{"id":"ebfab8d1388afba4","repo":"invoke-ai/InvokeAI","slug":"expected-pretrainedmodel-for-gemma-encoder-got-t-ebfab8","errorCode":null,"errorMessage":"Expected PreTrainedModel for Gemma encoder, got {type(gemma_encoder).__name__}.","messagePattern":"Expected PreTrainedModel for Gemma encoder, got (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/flux_pid_decode.py","lineNumber":100,"sourceCode":"    def invoke(self, context: InvocationContext) -> ImageOutput:\n        latents = context.tensors.load(self.latents.latents_name)\n\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,","sourceCodeStart":82,"sourceCodeEnd":118,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/flux_pid_decode.py#L82-L118","documentation":"The PiD decode invocation loads the Gemma text encoder onto device and asserts it is a transformers PreTrainedModel. If model_on_device() returns a different wrapper/type (e.g., a partial loader, quantized wrapper, or wrong model class), it raises TypeError. This guards against feeding captioning through an incompatible encoder object.","triggerScenarios":"invoke() loads self.gemma2_encoder.text_encoder and the object yielded by model_on_device() is not an instance of PreTrainedModel — wrong model class registered under that key, corrupted/partial load, or a custom model implementation.","commonSituations":"Pointing the node at a non-Gemma/non-transformers model record; a model manager wrapper returning a optimized/quantized object that isn't a PreTrainedModel; transformers version where the loaded class differs.","solutions":["Verify the Gemma2 encoder model record is a valid transformers text-encoder model and re-select it","Reinstall/redownload the Gemma encoder model (files may be corrupt or partially loaded)","Check that installed transformers/diffusers versions return PreTrainedModel from model_on_device","Inspect what type is registered for that model key in the Model Manager"],"exampleFix":"// before: node pointed at a generic/quantized encoder record\ngemma2_encoder=<quantized_wrapper_model>\n// after\ngemma2_encoder=<proper Gemma2 text-encoder model record>","handlingStrategy":"type-guard","validationCode":"info = context.models.load(gemma2_encoder.text_encoder)\n# confirm the record is a transformers-based text encoder before invoking","typeGuard":"def is_pretrained_model(obj) -> bool:\n    from transformers import PreTrainedModel\n    return isinstance(obj, PreTrainedModel)","tryCatchPattern":"try:\n    output = pid_decode.invoke(context)\nexcept TypeError as e:\n    if 'Expected PreTrainedModel' in str(e):\n        # re-select or reinstall the Gemma encoder\n        pass\n    else:\n        raise","preventionTips":["Point the node only at a valid Gemma2 text-encoder model record","Reinstall partially downloaded models","Pin compatible transformers/diffusers versions"],"tags":["type-error","gemma","text-encoder","flux","invokeai"],"backgroundTag":"unexpected-model-type","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}