{"record":{"id":"54244273492530d9","repo":"invoke-ai/InvokeAI","slug":"expected-autoencoderkl-or-fluxautoencoder-got-ty-542442","errorCode":null,"errorMessage":"Expected AutoencoderKL or FluxAutoEncoder, got {type(vae).__name__}. VAE model type changed unexpectedly after loading.","messagePattern":"Expected AutoencoderKL or FluxAutoEncoder, got (.+?)\\. VAE model type changed unexpectedly after loading\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/z_image_latents_to_image.py","lineNumber":72,"sourceCode":"\n        is_flux_vae = isinstance(vae_info.model, FluxAutoEncoder)\n\n        # Estimate working memory needed for VAE decode\n        estimated_working_memory = estimate_vae_working_memory_flux(\n            operation=\"decode\",\n            image_tensor=latents,\n            vae=vae_info.model,\n        )\n\n        # FLUX VAE doesn't support seamless, so only apply for AutoencoderKL\n        seamless_context = (\n            nullcontext() if is_flux_vae else SeamlessExt.static_patch_model(vae_info.model, self.vae.seamless_axes)\n        )\n\n        with seamless_context, vae_info.model_on_device(working_mem_bytes=estimated_working_memory) as (_, vae):\n            context.util.signal_progress(\"Running VAE\")\n            if not isinstance(vae, (AutoencoderKL, FluxAutoEncoder)):\n                raise TypeError(\n                    f\"Expected AutoencoderKL or FluxAutoEncoder, got {type(vae).__name__}. \"\n                    \"VAE model type changed unexpectedly after loading.\"\n                )\n\n            vae_dtype = next(iter(vae.parameters())).dtype\n            # Use the VAE's intended compute device (CUDA/MPS, or CPU if configured cpu_only). Do NOT infer it from\n            # current param residency: partial loading may have temporarily offloaded all weights to RAM, which would\n            # wrongly place the latents (and thus the whole decode) on the CPU (see #9373).\n            latents = latents.to(device=vae_info.compute_device, dtype=vae_dtype)\n\n            # Disable tiling for AutoencoderKL\n            if isinstance(vae, AutoencoderKL):\n                vae.disable_tiling()\n\n            # Clear memory as VAE decode can request a lot\n            TorchDevice.empty_cache()\n\n            with torch.inference_mode():","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/z_image_latents_to_image.py#L54-L90","documentation":"Inside the device-context manager (model_on_device), the VAE object handed back is re-checked: it must still be AutoencoderKL or FluxAutoEncoder. This is a defensive invariant check - the type was already validated at load time, so failure here means the model wrapper returned a different object than expected (e.g. a swapped, re-loaded, or partially-converted model). The message says 'changed unexpectedly after loading'.","triggerScenarios":"Within invoke() of ZImageLatentsToImage, entering `with ... vae_info.model_on_device(...) as (_, vae)` and the yielded object fails isinstance(vae, (AutoencoderKL, FluxAutoEncoder)).","commonSituations":"Concurrency/memory pressure causing the model to be unloaded and swapped mid-session; a patched loader returning a wrapper or converted dtype object; race with another invocation replacing the model; exotic SeamlessVae wrapper logic interacting with a non-standard VAE.","solutions":["Re-run the invocation; transient model-cache swaps are the usual cause.","Verify the VAE field still points at a Z-Image/Flux compatible VAE and re-open the workflow.","Clear/restart the model RAM cache or restart InvokeAI to flush stale loaded models.","Check for multiple concurrent workflows sharing the VAE and serialize them; update InvokeAI if reproducible."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":"def vae_stable_after_load(vae) -> bool:\n    from diffusers import AutoencoderKL\n    from invokeai.backend.flux.vae import FluxAutoEncoder\n    return isinstance(vae, (AutoencoderKL, FluxAutoEncoder))","tryCatchPattern":"try:\n    out = z_image_l2i.invoke(context)\nexcept TypeError as e:\n    if \"changed unexpectedly\" in str(e):\n        context.logger.warning(\"Model cache swapped VAE mid-run; retrying once.\")\n        out = retry(z_image_l2i.invoke, context)\n    else:\n        raise","preventionTips":["Avoid running many concurrent workflows that compete for the same VAE in RAM cache.","Restart InvokeAI / clear model cache if this error recurs after model changes.","Re-run the invocation first - transient swaps often resolve on retry."],"tags":["vae","race-condition","model-cache","typeerror","z-image"],"backgroundTag":"model-type-changed-unexpectedly","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}