{"record":{"id":"ab19fd5dc0ce80c2","repo":"invoke-ai/InvokeAI","slug":"expected-autoencoderklwan-or-fluxautoencoder-got","errorCode":null,"errorMessage":"Expected AutoencoderKLWan or FluxAutoEncoder, got {type(vae).__name__}.","messagePattern":"Expected AutoencoderKLWan or FluxAutoEncoder, got (.+?)\\.","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/anima_image_to_latents.py","lineNumber":81,"sourceCode":"            )\n\n        if isinstance(vae_info.model, AutoencoderKLWan):\n            estimated_working_memory = estimate_vae_working_memory_anima(\n                operation=\"encode\",\n                image_tensor=image_tensor,\n                vae=vae_info.model,\n                tile_size=None,\n            )\n        else:\n            estimated_working_memory = estimate_vae_working_memory_flux(\n                operation=\"encode\",\n                image_tensor=image_tensor,\n                vae=vae_info.model,\n            )\n\n        with vae_info.model_on_device(working_mem_bytes=estimated_working_memory) as (_, vae):\n            if not isinstance(vae, (AutoencoderKLWan, FluxAutoEncoder)):\n                raise TypeError(f\"Expected AutoencoderKLWan or FluxAutoEncoder, got {type(vae).__name__}.\")\n\n            vae_dtype = next(iter(vae.parameters())).dtype\n            image_tensor = image_tensor.to(device=TorchDevice.choose_torch_device(), dtype=vae_dtype)\n\n            with torch.inference_mode():\n                if isinstance(vae, FluxAutoEncoder):\n                    # FLUX VAE handles scaling internally\n                    generator = torch.Generator(device=TorchDevice.choose_torch_device()).manual_seed(0)\n                    latents = vae.encode(image_tensor, sample=True, generator=generator)\n                else:\n                    # The cached VAE instance is shared with the decode invocation, which\n                    # may have enabled tiling — encode untiled for exactness.\n                    vae.disable_tiling()\n                    # AutoencoderKLWan expects 5D input [B, C, T, H, W]\n                    if image_tensor.ndim == 4:\n                        image_tensor = image_tensor.unsqueeze(2)  # [B, C, H, W] -> [B, C, 1, H, W]\n\n                    encoded = vae.encode(image_tensor, return_dict=False)[0]","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/anima_image_to_latents.py#L63-L99","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Reload the workflow / re-run so the model is freshly loaded and re-validated.","Update InvokeAI to the latest version where on-device model handling matches the expected types.","Confirm only one model instance per key exists in the model manager to avoid type confusion."],"exampleFix":"# before: mutated/cached model record reused across runs\nvae_info = context.models.load(self.vae.vae)\n# after: force a fresh load consistent with current model manager state\n# upgrade InvokeAI / clear model cache so model_on_device yields the real VAE instance","handlingStrategy":"type-guard","validationCode":"with vae_info.model_on_device() as (_, vae):\n    if not is_anima_vae(vae):\n        vae_info = context.models.load(self.vae.vae)  # reload and retry once","typeGuard":"def is_anima_vae_on_device(vae) -> bool:\n    from diffusers import AutoencoderKLWan\n    from invokeai.backend.flux.model import FluxAutoEncoder\n    return isinstance(vae, (AutoencoderKLWan, FluxAutoEncoder))","tryCatchPattern":"try:\n    with vae_info.model_on_device() as (_, vae):\n        run_decode(vae)\nexcept TypeError as e:\n    if \"Expected AutoencoderKLWan or FluxAutoEncoder\" in str(e):\n        vae_info = context.models.load(vae_field.vae)  # fresh load, retry once\n    else:\n        raise","preventionTips":["Keep InvokeAI up to date so on-device wrappers match expected types","Avoid mutating the model list mid-generation","Restart the backend if model residency state looks stale"],"tags":["vae","type-error","device-management"],"backgroundTag":"incompatible-model-type","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}