Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
from typing import Optional
from invokeai.app.invocations.baseinvocation import (
BaseInvocation,
BaseInvocationOutput,
Classification,
invocation,
invocation_output,
)
from invokeai.app.invocations.fields import FieldDescriptions, Input, InputField, OutputField
from invokeai.app.invocations.model import (
ModelIdentifierField,
Qwen3EncoderField,
TransformerField,
VAEField,
is_self_contained_sdnq_pipeline,
)
from invokeai.app.services.shared.invocation_context import InvocationContext
from invokeai.backend.architectures import accepted_vae_bases
from invokeai.backend.model_manager.taxonomy import (
BaseModelType,
ModelFormat,
ModelType,
Qwen3VariantType,
SubModelType,
)
@invocation_output("z_image_model_loader_output")
class ZImageModelLoaderOutput(BaseInvocationOutput):
"""Z-Image base model loader output."""
transformer: TransformerField = OutputField(description=FieldDescriptions.transformer, title="Transformer")
qwen3_encoder: Qwen3EncoderField = OutputField(description=FieldDescriptions.qwen3_encoder, title="Qwen3 Encoder")
vae: VAEField = OutputField(description=FieldDescriptions.vae, title="VAE")
@invocation(
"z_image_model_loader",
title="Main Model - Z-Image",
tags=["model", "z-image"],
category="model",
# 3.1.0: the Qwen3 Source input dropped its `ui_model_format=Diffusers` pin so SDNQ pipelines are
# offered too, and the loader falls back to a self-contained main model for VAE / Qwen3. A node
# serialized against 3.0.0 carries the old field contract, so the version has to move with it.
version="3.1.0",
classification=Classification.Prototype,
)
class ZImageModelLoaderInvocation(BaseInvocation):
"""Loads a Z-Image model, outputting its submodels.
Similar to FLUX, you can mix and match components:
- Transformer: From Z-Image main model (GGUF quantized or Diffusers format)
- VAE: Separate FLUX VAE (shared with FLUX models) or from a Diffusers Z-Image model
- Qwen3 Encoder: Separate Qwen3Encoder model or from a Diffusers Z-Image model
"""
model: ModelIdentifierField = InputField(
description=FieldDescriptions.z_image_model,
input=Input.Direct,
ui_model_base=BaseModelType.ZImage,
ui_model_type=ModelType.Main,
title="Transformer",
)
vae_model: Optional[ModelIdentifierField] = InputField(
default=None,
description="Standalone VAE model. Z-Image uses the same VAE as FLUX (16-channel). "
"If not provided, VAE will be loaded from the Qwen3 Source model.",
input=Input.Direct,
ui_model_base=accepted_vae_bases(BaseModelType.ZImage),
ui_model_type=ModelType.VAE,
title="VAE",
)
qwen3_encoder_model: Optional[ModelIdentifierField] = InputField(
default=None,
description="Standalone Qwen3 Encoder model. "
"If not provided, encoder will be loaded from the Qwen3 Source model.",
input=Input.Direct,
ui_model_type=ModelType.Qwen3Encoder,
title="Qwen3 Encoder",
)
qwen3_source_model: Optional[ModelIdentifierField] = InputField(
default=None,
description="Z-Image pipeline model to extract VAE and/or Qwen3 encoder from. "
"Use this if you don't have separate VAE/Qwen3 models. "
"Ignored if both VAE and Qwen3 Encoder are provided separately.",
input=Input.Direct,
ui_model_base=BaseModelType.ZImage,
ui_model_type=ModelType.Main,
# No ui_model_format hint: both plain Diffusers pipelines and SDNQ-quantized ZImagePipeline
# folders (which ship VAE/Qwen3 submodels) are valid sources. _validate_diffusers_format
# accepts both, so pinning the format here would wrongly filter SDNQ pipelines out of the
# generic node/workflow model pickers.
title="Qwen3 Source",
)
def invoke(self, context: InvocationContext) -> ZImageModelLoaderOutput:
# Transformer always comes from the main model
transformer = self.model.model_copy(update={"submodel_type": SubModelType.Transformer})
# SDNQ Z-Image pipeline installs are self-contained: a single install ships the transformer,
# Qwen3 encoder and VAE as submodels of the main model. When the user hasn't selected a
# standalone VAE / Qwen3 Encoder or a separate Qwen3 Source, fall back to the main model
# itself for those submodels. This lets a freshly installed SDNQ Z-Image pipeline generate
# without the user having to manually re-select the same model as a component source.
self_contained_source = self._get_self_contained_source(context)
# Determine VAE source
if self.vae_model is not None:
# Use standalone FLUX VAE
vae = self.vae_model.model_copy(update={"submodel_type": SubModelType.VAE})
elif self.qwen3_source_model is not None:
# Extract from Diffusers Z-Image model
self._validate_diffusers_format(context, self.qwen3_source_model, "Qwen3 Source")
vae = self.qwen3_source_model.model_copy(update={"submodel_type": SubModelType.VAE})
elif self_contained_source is not None:
# Extract from the self-contained SDNQ main model
vae = self_contained_source.model_copy(update={"submodel_type": SubModelType.VAE})
else:
raise ValueError(
"No VAE source provided. Either set 'VAE' to a FLUX VAE model, "
"or set 'Qwen3 Source' to a Diffusers Z-Image model."
)
# Determine Qwen3 Encoder source
if self.qwen3_encoder_model is not None:
# Use standalone Qwen3 Encoder
# Asked for as an encoder: a Text LLM holding a Qwen3 folder serves as one.
encoder = self.qwen3_encoder_model.model_copy(update={"type": ModelType.Qwen3Encoder})
self._validate_qwen3_encoder_variant(context, encoder)
qwen3_tokenizer = encoder.model_copy(update={"submodel_type": SubModelType.Tokenizer})
qwen3_encoder = encoder.model_copy(update={"submodel_type": SubModelType.TextEncoder})
elif self.qwen3_source_model is not None:
# Extract from Diffusers Z-Image model
self._validate_diffusers_format(context, self.qwen3_source_model, "Qwen3 Source")
qwen3_tokenizer = self.qwen3_source_model.model_copy(update={"submodel_type": SubModelType.Tokenizer})
qwen3_encoder = self.qwen3_source_model.model_copy(update={"submodel_type": SubModelType.TextEncoder})
elif self_contained_source is not None:
# Extract from the self-contained SDNQ main model
qwen3_tokenizer = self_contained_source.model_copy(update={"submodel_type": SubModelType.Tokenizer})
qwen3_encoder = self_contained_source.model_copy(update={"submodel_type": SubModelType.TextEncoder})
else:
raise ValueError(
"No Qwen3 Encoder source provided. Either set 'Qwen3 Encoder' to a standalone model, "
"or set 'Qwen3 Source' to a Diffusers Z-Image model."
)
return ZImageModelLoaderOutput(
transformer=TransformerField(transformer=transformer, loras=[]),
qwen3_encoder=Qwen3EncoderField(tokenizer=qwen3_tokenizer, text_encoder=qwen3_encoder),
vae=VAEField(vae=vae),
)
def _validate_qwen3_encoder_variant(self, context: InvocationContext, encoder: ModelIdentifierField) -> None:
"""Z-Image's caption embedder takes the Qwen3 4B hidden size (2560). Every Qwen3 size installs
under the same model type, so the 8B (4096) or 0.6B (1024) would otherwise load and then fail
as a shape mismatch on the first denoise step, naming a tensor rather than the selection."""
config = context.models.get_config(encoder)
variant = getattr(config, "variant", None)
is_qwen3 = config.type is ModelType.Qwen3Encoder
if is_qwen3 and variant == Qwen3VariantType.Qwen3_4B:
return
described = (
f"a Qwen3 encoder of variant '{getattr(variant, 'value', variant)}'" if is_qwen3 else "not a Qwen3 encoder"
)
raise ValueError(
f"'{config.name}' is {described}. Z-Image conditions on the Qwen3 4B encoder (hidden size 2560): "
"select a Qwen3 4B encoder, or clear it and set 'Qwen3 Source' to a Z-Image pipeline."
)
def _get_self_contained_source(self, context: InvocationContext) -> Optional[ModelIdentifierField]:
"""Return the main model as a submodel source when it is a self-contained pipeline that
ships its own VAE and Qwen3 submodels.
A truthy ``submodels`` dict is not sufficient: Main_SDNQ_Diffusers_ZImage_Config builds
whatever submodels it recognizes from model_index.json, so a partial (or partially
recognized) pipeline can expose e.g. only the transformer. The loader then loads the VAE /
Qwen3 encoder / tokenizer from fixed ``vae`` / ``text_encoder`` / ``tokenizer`` subfolders, so
we only treat the main model as self-contained when all of those submodels are present.
Otherwise we fall through and require an explicit VAE / Qwen3 source."""
config = context.models.get_config(self.model)
if is_self_contained_sdnq_pipeline(config):
return self.model
return None
def _validate_diffusers_format(
self, context: InvocationContext, model: ModelIdentifierField, model_name: str
) -> None:
"""Validate that a model exposes the diffusers-style submodel layout (transformer / vae /
text_encoder / tokenizer subfolders). Plain diffusers Z-Image pipelines satisfy this;
SDNQ-quantized ZImagePipeline folders do too, but only when they ship the VAE + Qwen3
submodels. Single-file SDNQ Z-Image checkpoints and partial pipelines are rejected."""
config = context.models.get_config(model)
if config.format == ModelFormat.Diffusers:
return
if is_self_contained_sdnq_pipeline(config):
return
raise ValueError(
f"The {model_name} model must be a Diffusers-style Z-Image pipeline (with VAE / Qwen3 "
f"submodels). The selected model '{config.name}' is in {config.format.value} format."
)