invoke-ai/InvokeAI · error · TypeError

Expected AutoencoderKLWan or FluxAutoEncoder, got {type(vae)

Error message

Expected AutoencoderKLWan or FluxAutoEncoder, got {type(vae).__name__}.

What it means

A second, post-loading re-check inside vae_encode: after the model is moved onto the compute device via model_on_device, the object yielded is re-validated as AutoencoderKLWan or FluxAutoEncoder. If the on-device wrapper yielded a different object type, this TypeError is raised.

Source

Thrown at invokeai/app/invocations/anima_image_to_latents.py:81

            )

        if isinstance(vae_info.model, AutoencoderKLWan):
            estimated_working_memory = estimate_vae_working_memory_anima(
                operation="encode",
                image_tensor=image_tensor,
                vae=vae_info.model,
                tile_size=None,
            )
        else:
            estimated_working_memory = estimate_vae_working_memory_flux(
                operation="encode",
                image_tensor=image_tensor,
                vae=vae_info.model,
            )

        with vae_info.model_on_device(working_mem_bytes=estimated_working_memory) as (_, vae):
            if not isinstance(vae, (AutoencoderKLWan, FluxAutoEncoder)):
                raise TypeError(f"Expected AutoencoderKLWan or FluxAutoEncoder, got {type(vae).__name__}.")

            vae_dtype = next(iter(vae.parameters())).dtype
            image_tensor = image_tensor.to(device=TorchDevice.choose_torch_device(), dtype=vae_dtype)

            with torch.inference_mode():
                if isinstance(vae, FluxAutoEncoder):
                    # FLUX VAE handles scaling internally
                    generator = torch.Generator(device=TorchDevice.choose_torch_device()).manual_seed(0)
                    latents = vae.encode(image_tensor, sample=True, generator=generator)
                else:
                    # The cached VAE instance is shared with the decode invocation, which
                    # may have enabled tiling — encode untiled for exactness.
                    vae.disable_tiling()
                    # AutoencoderKLWan expects 5D input [B, C, T, H, W]
                    if image_tensor.ndim == 4:
                        image_tensor = image_tensor.unsqueeze(2)  # [B, C, H, W] -> [B, C, 1, H, W]

                    encoded = vae.encode(image_tensor, return_dict=False)[0]

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Reload the workflow / re-run so the model is freshly loaded and re-validated.
  2. Update InvokeAI to the latest version where on-device model handling matches the expected types.
  3. Confirm only one model instance per key exists in the model manager to avoid type confusion.

Example fix

# before: mutated/cached model record reused across runs
vae_info = context.models.load(self.vae.vae)
# after: force a fresh load consistent with current model manager state
# upgrade InvokeAI / clear model cache so model_on_device yields the real VAE instance
Defensive patterns

Strategy: type-guard

Validate before calling

with vae_info.model_on_device() as (_, vae):
    if not is_anima_vae(vae):
        vae_info = context.models.load(self.vae.vae)  # reload and retry once

Type guard

def is_anima_vae_on_device(vae) -> bool:
    from diffusers import AutoencoderKLWan
    from invokeai.backend.flux.model import FluxAutoEncoder
    return isinstance(vae, (AutoencoderKLWan, FluxAutoEncoder))

Try / catch

try:
    with vae_info.model_on_device() as (_, vae):
        run_decode(vae)
except TypeError as e:
    if "Expected AutoencoderKLWan or FluxAutoEncoder" in str(e):
        vae_info = context.models.load(vae_field.vae)  # fresh load, retry once
    else:
        raise

Prevention

When it happens

Trigger: The model_on_device context manager yields an object that is not an AutoencoderKLWan/FluxAutoEncoder instance — typically when the loaded model type changed between the initial check and device residency (e.g. partially offloaded/wrapped model) or the model record was mutated concurrently.

Common situations: Version drift where the on-device model wrapper changed class; a hot-swapped model record during long-running generation; running a workflow built against an older InvokeAI model-loading API.

Related errors


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