{"record":{"id":"4a19960a45539e31","repo":"invoke-ai/InvokeAI","slug":"expected-pretrainedmodel-for-gemma-encoder-got-t","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/flux2_pid_decode.py","lineNumber":173,"sourceCode":"            with vae_info.model_on_device() as (_, vae):\n                config = getattr(vae, \"config\", None)\n                if config is not None and hasattr(config, \"scaling_factor\"):\n                    scaling_factor = float(config.scaling_factor)\n                    shift_factor = float(getattr(config, \"shift_factor\", None) or 0.0)\n                else:\n                    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\n        # 3) 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":155,"sourceCodeEnd":191,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/flux2_pid_decode.py#L155-L191","documentation":"The Gemma2 text encoder used during FLUX.2 PiD decode must be a transformers PreTrainedModel. The check runs right after model_on_device() context entry; a different class means the loaded encoder file is corrupted, incomplete, or not a Gemma model.","triggerScenarios":"gemma_text_encoder_info.model_on_device() yields an object that is not PreTrainedModel in the invoke ExitStack; the gemma2_encoder.text_encoder field references a wrong or damaged model record.","commonSituations":"Interrupted downloads of Gemma encoder weights; model config pointing at a non-Gemma directory; transformers version changes changing the wrapper class; converted/quantized models loaded as custom classes.","solutions":["Re-download the Gemma2 text encoder via the model manager","Verify gemma2_encoder.text_encoder points to the correct Gemma model record","Update transformers to a compatible version","Re-scan/repair model records if hashes are stale"],"exampleFix":"// before: text_encoder -> generic llama ckpt\nModelConfig(type='main', path='/models/llama-ckpt/')\n// after\nModelConfig(type='main', path='/models/google/gemma-2-2b/', name='Gemma2 encoder')","handlingStrategy":"type-guard","validationCode":"info = context.models.load(gemma2_encoder.text_encoder)\nif not isinstance(info.model, PreTrainedModel):\n    raise TypeError(f'Gemma encoder invalid: {type(info.model).__name__}')","typeGuard":"from transformers import PreTrainedModel\n\ndef is_gemma_encoder(obj) -> bool:\n    return isinstance(obj, PreTrainedModel)","tryCatchPattern":"try:\n    result = pid_decode.invoke(context)\nexcept TypeError as e:\n    if 'Gemma encoder' in str(e):\n        reimport_model_manager_entry(gemma2_encoder.text_encoder)\n    raise","preventionTips":["Verify Gemma downloads complete with matching hashes","Point the encoder field at verified Gemma2 records","Keep transformers compatible with Gemma2"],"tags":["model-loading","type-check","transformers"],"backgroundTag":"model-type-mismatch","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}