invoke-ai/InvokeAI · error · TypeError

Expected PreTrainedModel for text encoder, got {type(text_en

Error message

Expected PreTrainedModel for text encoder, got {type(text_encoder).__name__}. The Mistral encoder model may be corrupted or incompatible.

What it means

_encode_prompt throws this TypeError when the loaded Mistral text encoder object is not a transformers PreTrainedModel. This indicates the model loaded into memory is corrupted, incompatible, or the wrong type for FLUX.2 [dev] encoding. It is a defensive type check performed before building the prompt template and running the forward pass.

Source

Thrown at invokeai/app/invocations/flux2_dev_text_encoder.py:159

                f"Recovered {repaired_tensors} required Mistral tensor(s) on {device} after a partial device mismatch."
            )

        # Apply any LoRAs attached to the text encoder.
        lora_dtype = TorchDevice.choose_bfloat16_safe_dtype(device)
        exit_stack.enter_context(
            LayerPatcher.apply_smart_model_patches(
                model=text_encoder,
                patches=self._lora_iterator(context),
                prefix=FLUX_LORA_T5_PREFIX,
                dtype=lora_dtype,
                cached_weights=cached_weights,
            )
        )

        context.util.signal_progress("Running Mistral text encoder (FLUX.2 [dev])")

        if not isinstance(text_encoder, PreTrainedModel):
            raise TypeError(
                f"Expected PreTrainedModel for text encoder, got {type(text_encoder).__name__}. "
                "The Mistral encoder model may be corrupted or incompatible."
            )

        # Build the raw FLUX.2 [dev] prompt template — matches ComfyUI's
        # `Flux2Tokenizer.llama_template.format(text)` byte-for-byte. `[SYSTEM_PROMPT]`,
        # `[/SYSTEM_PROMPT]`, `[INST]`, `[/INST]` are Tekken special tokens, so any of
        # the three processors we can land on (Pixtral/Mistral3 processor, plain HF
        # LlamaTokenizerFast, our embedded-Tekken adapter) emit the same sequence.
        text = FLUX2_DEV_PROMPT_TEMPLATE.format(system=FLUX2_DEV_SYSTEM_MESSAGE, prompt=self.prompt)

        # Comfy pads on the LEFT (`pad_left=True`), keeping the meaningful tokens
        # at the right edge of the sequence. HF processors expose this via the
        # `padding_side` attribute on their underlying tokenizer; we set it
        # explicitly so the call matches Comfy's behavior regardless of the
        # tokenizer's default. `processor` is typed as the `AnyModel` union;
        # narrow to `Any` for the duration of the tokenizer call.
        proc = cast(Any, processor)

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Re-download or re-register the Mistral Small 3.1 text encoder model in the model manager to replace the corrupted/incompatible files.
  2. Verify the 'Mistral Encoder' input points at an actual Mistral text encoder model, not another model type.
  3. Update the transformers library version so Mistral models load as PreTrainedModel instances.
Defensive patterns

Strategy: type-guard

Validate before calling

encoder_info = context.models.load(mistral_encoder_ref)
if not isinstance(encoder_info.model, PreTrainedModel):
    raise TypeError(f"Encoder is {type(encoder_info.model).__name__}, expected PreTrainedModel")

Type guard

def is_valid_encoder(obj) -> bool:
    return isinstance(obj, PreTrainedModel)

Try / catch

try:
    output = text_encoder_invocation.invoke(context)
except TypeError as e:
    if "Expected PreTrainedModel for text encoder" in str(e):
        reimport_mistral_encoder_model(context)
        output = text_encoder_invocation.invoke(context)
    else:
        raise

Prevention

When it happens

Trigger: invoke() -> _encode_prompt receives a text_encoder whose runtime type is not PreTrainedModel (e.g. a config object, a dict, or a differently-typed module returned by the model loader).

Common situations: The Mistral encoder model files are corrupted on disk, the wrong model was registered as the encoder, or an incompatible/patched transformers loading path returned a nonstandard object.

Related errors


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