invoke-ai/InvokeAI · error · TypeError

Expected AutoencoderKLQwenImage or AutoencoderKLWan, got {ty

Error message

Expected AutoencoderKLQwenImage or AutoencoderKLWan, got {type(model).__name__}.

What it means

as_qwen_image_vae adapts a VAE model for Krea-2/Qwen-Image latent space. It accepts an AutoencoderKLQwenImage as-is, or an AutoencoderKLWan for config-driven compatibility conversion; any other model type cannot be safely adapted (rebuilding from a state dict would drop hooks and layerwise-casting config), so it raises TypeError.

Source

Thrown at invokeai/backend/krea2/vae_compat.py:48


def as_qwen_image_vae(model: Any) -> QwenImageCompatibleVAE:
    """Return a cache-preserving VAE compatible with the Qwen-Image encode/decode path.

    The only expected non-matching input is ``AutoencoderKLWan`` (the same weights loaded via the
    Anima single-file path). A Wan VAE with the Qwen-Image geometry (16 latent channels, 8x spatial, no
    patchification) has identical encode/decode behavior, state-dict layout, and default latent statistics,
    so the cached module can be used directly. A Wan VAE with any other geometry (e.g. Wan 2.2's 48-channel,
    patchified VAE) is rejected here rather than failing deeper in normalization/decode.

    Returning the original object is important: the model cache injects custom modules for partial
    loading before this helper is called, and rebuilding the module from its state dict would discard
    those modules along with any hooks or layerwise-casting configuration.
    """
    if isinstance(model, AutoencoderKLQwenImage):
        return model
    if not isinstance(model, AutoencoderKLWan):
        raise TypeError(f"Expected AutoencoderKLQwenImage or AutoencoderKLWan, got {type(model).__name__}.")

    config = model.config
    z_dim = getattr(config, "z_dim", None)
    patch_size = getattr(config, "patch_size", None)
    spatial_scale = getattr(config, "scale_factor_spatial", _QWEN_IMAGE_VAE_SPATIAL_SCALE)
    if z_dim != _QWEN_IMAGE_VAE_Z_DIM or patch_size is not None or spatial_scale != _QWEN_IMAGE_VAE_SPATIAL_SCALE:
        raise ValueError(
            "AutoencoderKLWan is not Qwen-Image-compatible "
            f"(z_dim={z_dim}, patch_size={patch_size}, scale_factor_spatial={spatial_scale}); "
            f"expected {_QWEN_IMAGE_VAE_Z_DIM} latent channels, {_QWEN_IMAGE_VAE_SPATIAL_SCALE}x spatial, "
            "and no patchification."
        )

    return model


# The stock AutoencoderKLQwenImage tile geometry: 256px tiles advancing in 192px steps, i.e. a 3/4
# stride ratio with a 64px blend band. Both nodes resolve tile_size=0 to QWEN_IMAGE_VAE_DEFAULT_TILE_SIZE

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Load a Krea-2/Qwen-Image or Wan VAE and pass that instance instead.
  2. Check type(model).__name__ before calling; route non-Wan/Qwen VAEs to their own encode path.
  3. If using a custom VAE wrapper, inherit from AutoencoderKLQwenImage or unwrap the underlying model first.
  4. Verify the model-loader config maps the correct VAE for the Krea-2 base model.

Example fix

// before
vae = load_vae('stabilityai/sd-vae-ft-mse')  # AutoencoderKL
latents = as_qwen_image_vae(vae).encode(...)
// after
vae = load_vae('Qwen/Qwen-Image', variant=AutoencoderKLQwenImage)
latents = as_qwen_image_vae(vae).encode(...)
Defensive patterns

Strategy: type-guard

Validate before calling

from invokeai.backend.krea2.vae_compat import as_qwen_image_vae
if not isinstance(vae, (AutoencoderKLQwenImage, AutoencoderKLWan)):
    raise TypeError(f'{type(vae).__name__} cannot be used as a Krea-2 VAE')

Type guard

def is_krea2_compatible_vae(model) -> bool:
    return isinstance(model, (AutoencoderKLQwenImage, AutoencoderKLWan))

Try / catch

try:
    vae = as_qwen_image_vae(model)
except TypeError as e:
    if 'Expected AutoencoderKLQwenImage or AutoencoderKLWan' in str(e):
        vae = load_correct_vae_for_base_model()
    else:
        raise

Prevention

When it happens

Trigger: Calling as_qwen_image_vae(model) (directly or via vae_encode/invoke) with a model that is neither AutoencoderKLQwenImage nor AutoencoderKLWan — e.g. AutoencoderKL (SD), AutoencoderKLLTXL, AutoencoderKLFlux, or a wrapped/quantized VAE object.

Common situations: Wiring a Stable Diffusion or other diffusion family's VAE into a Krea-2 pipeline by mistake; loading the wrong model config into the VAE slot; passing a custom subclass that does not inherit from the accepted classes.

Related errors


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