invoke-ai/InvokeAI · error · TypeError

Expected AutoencoderKL or FluxAutoEncoder, got {type(vae).__

Error message

Expected AutoencoderKL or FluxAutoEncoder, got {type(vae).__name__}. VAE model type changed unexpectedly after loading.

What it means

After loading the VAE onto the compute device with model_on_device, the code re-checks the model's runtime type. If it changed from AutoencoderKL/FluxAutoEncoder (checked before encode) to something else, this TypeError fires — a defensive guard against the model being swapped or wrapped unexpectedly during load/eviction.

Source

Thrown at invokeai/app/invocations/z_image_image_to_latents.py:60

    @staticmethod
    def vae_encode(vae_info: LoadedModel, image_tensor: torch.Tensor) -> torch.Tensor:
        if not isinstance(vae_info.model, (AutoencoderKL, FluxAutoEncoder)):
            raise TypeError(
                f"Expected AutoencoderKL or FluxAutoEncoder for Z-Image VAE, got {type(vae_info.model).__name__}. "
                "Ensure you are using a compatible VAE model."
            )

        # Estimate working memory needed for VAE encode
        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, (AutoencoderKL, FluxAutoEncoder)):
                raise TypeError(
                    f"Expected AutoencoderKL or FluxAutoEncoder, got {type(vae).__name__}. "
                    "VAE model type changed unexpectedly after loading."
                )

            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:
                    # AutoencoderKL - needs manual scaling
                    vae.disable_tiling()
                    image_tensor_dist = vae.encode(image_tensor).latent_dist
                    latents: torch.Tensor = image_tensor_dist.sample().to(dtype=vae.dtype)

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Retry the invocation (transient cache-swap race); avoid running competing generations that evict the same VAE
  2. Increase RAM/VRAM headroom or model cache size to prevent mid-run eviction
  3. Ensure only one workflow branch loads/mutates the shared VAE concurrently
  4. If reproducible, re-import the VAE in the model manager and update InvokeAI

Example fix

// before
# concurrent invocations evict the shared VAE mid-run -> TypeError
run(graph_a); run(graph_b)  # both share one VAE, low RAM
// after
# run sequentially or raise cache size so the VAE stays loaded
run(graph_a); run(graph_b)
Defensive patterns

Strategy: try-catch

Type guard

def is_decoded_vae(model) -> bool:
    return isinstance(model, (AutoencoderKL, FluxAutoEncoder))

Try / catch

try:
    latents = img2latents.invoke(context)
except TypeError as e:
    if "VAE model type changed unexpectedly" in str(e):
        time.sleep(0.5)  # let model cache settle, then retry once
        latents = img2latents.invoke(context)
    else:
        raise

Prevention

When it happens

Trigger: invoke() reaches the model_on_device context and the materialized vae_info.model object is not an AutoencoderKL/FluxAutoEncoder — typically due to concurrent model unloading/swapping in the RAM cache, or a model object wrapped/proxied by another loader path.

Common situations: Low-VRAM/RAM environments where the model cache evicts and reloads models mid-invocation; race conditions with parallel graph nodes sharing the same VAE; a loader wrapper changing the model class between the pre-check and on-device context.

Related errors


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