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
import json
from collections.abc import Hashable
from typing import Any, Literal, Optional, Sequence, Union
import torch
from pydantic import BaseModel, ConfigDict, Field
from invokeai.app.invocations.baseinvocation import (
BaseInvocation,
BaseInvocationOutput,
Classification,
invocation,
invocation_output,
)
from invokeai.app.invocations.constants import LATENT_SCALE_FACTOR
from invokeai.app.invocations.fields import (
FieldDescriptions,
ImageField,
InputField,
LatentsField,
MetadataField,
OutputField,
TensorField,
UIType,
VideoField,
)
from invokeai.app.invocations.metadata_constants import (
CORE_MODEL_LABELS as CORE_MODEL_LABELS,
)
from invokeai.app.invocations.metadata_constants import (
CUSTOM_LABEL as CUSTOM_LABEL,
)
from invokeai.app.invocations.model import (
ModelIdentifierField,
PiDDecoderField,
TransformerField,
UNetField,
VAEField,
WanTransformerField,
)
from invokeai.app.invocations.primitives import LatentsOutput, StringOutput
from invokeai.app.services.shared.invocation_context import InvocationContext
from invokeai.app.util.controlnet_utils import CONTROLNET_MODE_VALUES, CONTROLNET_RESIZE_VALUES
from invokeai.version.invokeai_version import __version__
class MetadataItemField(BaseModel):
label: str = Field(description=FieldDescriptions.metadata_item_label)
value: Any = Field(description=FieldDescriptions.metadata_item_value)
class LoRAMetadataField(BaseModel):
"""LoRA Metadata Field"""
model: ModelIdentifierField = Field(description=FieldDescriptions.lora_model)
weight: float = Field(description=FieldDescriptions.lora_weight)
target: Optional[Literal["high", "low", "both"]] = Field(
default=None,
description="Target expert for dual-expert models (e.g., Wan 2.2 A14B): 'high', 'low', or 'both'",
)
low_noise_weight: Optional[float] = Field(
default=None,
description="Weight for the low-noise expert when target is 'both' and it differs from the high-noise weight",
)
class IPAdapterMetadataField(BaseModel):
"""IP Adapter Field, minus the CLIP Vision Encoder model"""
image: ImageField = Field(description="The IP-Adapter image prompt.")
ip_adapter_model: ModelIdentifierField = Field(description="The IP-Adapter model.")
clip_vision_model: Literal["ViT-L", "ViT-H", "ViT-G"] = Field(description="The CLIP Vision model")
method: Literal["full", "style", "composition", "style_strong", "style_precise"] = Field(
description="Method to apply IP Weights with"
)
weight: Union[float, list[float]] = Field(description="The weight given to the IP-Adapter")
begin_step_percent: float = Field(description="When the IP-Adapter is first applied (% of total steps)")
end_step_percent: float = Field(description="When the IP-Adapter is last applied (% of total steps)")
class T2IAdapterMetadataField(BaseModel):
image: ImageField = Field(description="The control image.")
processed_image: Optional[ImageField] = Field(default=None, description="The control image, after processing.")
t2i_adapter_model: ModelIdentifierField = Field(description="The T2I-Adapter model to use.")
weight: Union[float, list[float]] = Field(default=1, description="The weight given to the T2I-Adapter")
begin_step_percent: float = Field(
default=0, ge=0, le=1, description="When the T2I-Adapter is first applied (% of total steps)"
)
end_step_percent: float = Field(
default=1, ge=0, le=1, description="When the T2I-Adapter is last applied (% of total steps)"
)
resize_mode: CONTROLNET_RESIZE_VALUES = Field(default="just_resize", description="The resize mode to use")
class ControlNetMetadataField(BaseModel):
image: ImageField = Field(description="The control image")
processed_image: Optional[ImageField] = Field(default=None, description="The control image, after processing.")
control_model: ModelIdentifierField = Field(description="The ControlNet model to use")
control_weight: Union[float, list[float]] = Field(default=1, description="The weight given to the ControlNet")
begin_step_percent: float = Field(
default=0, ge=0, le=1, description="When the ControlNet is first applied (% of total steps)"
)
end_step_percent: float = Field(
default=1, ge=0, le=1, description="When the ControlNet is last applied (% of total steps)"
)
control_mode: CONTROLNET_MODE_VALUES = Field(default="balanced", description="The control mode to use")
resize_mode: CONTROLNET_RESIZE_VALUES = Field(default="just_resize", description="The resize mode to use")
@invocation_output("metadata_item_output")
class MetadataItemOutput(BaseInvocationOutput):
"""Metadata Item Output"""
item: MetadataItemField = OutputField(description="Metadata Item")
_SUBMODEL_WRAPPER_ATTRS: tuple[str, ...] = (
"vae",
"text_encoder",
"unet",
"transformer",
"decoder",
"vocoder",
"latent_upsampler",
)
_WAN_TRANSFORMER_DICT_KEYS: frozenset[str] = frozenset(
{"transformer", "transformer_low_noise", "loras", "loras_low_noise", "boundary_ratio"}
)
_PRESERVE_TRUTHY_FIELDS: tuple[str, ...] = ("loras", "loras_low_noise", "seamless_axes", "skipped_layers")
_PRESERVE_NOT_NONE_FIELDS: tuple[str, ...] = (
"transformer_low_noise",
"boundary_ratio",
"variant",
"adaln_overlay",
"processor",
"connectors",
"vision_tower",
"freeu_config",
)
# (primary_key, allowed_keys, preserve_when_custom_keys)
_WRAPPER_DICT_SPECS: tuple[tuple[str, frozenset[str], tuple[str, ...]], ...] = (
(
"text_encoder",
frozenset({"tokenizer", "processor", "text_encoder", "vision_tower", "connectors", "loras", "skipped_layers"}),
("loras", "processor", "connectors", "vision_tower", "skipped_layers"),
),
("clip", frozenset({"tokenizer", "clip", "loras", "skipped_layers"}), ("loras", "skipped_layers")),
("vae", frozenset({"vae", "seamless_axes"}), ("seamless_axes",)),
(
"transformer",
frozenset({"transformer", "loras", "variant", "adaln_overlay"}),
("loras", "variant", "adaln_overlay"),
),
(
"unet",
frozenset({"unet", "scheduler", "loras", "seamless_axes", "freeu_config"}),
("loras", "seamless_axes", "freeu_config"),
),
("decoder", frozenset({"decoder"}), ()),
("vocoder", frozenset({"vocoder"}), ()),
("latent_upsampler", frozenset({"latent_upsampler"}), ()),
)
def _is_model_identifier_value(val: Any) -> bool:
return isinstance(val, ModelIdentifierField) or (isinstance(val, dict) and "key" in val)
def _has_preservable_wrapper_fields(val: Any, keys: Optional[Sequence[str]] = None) -> bool:
if isinstance(val, dict):
check_keys = keys if keys is not None else (*_PRESERVE_TRUTHY_FIELDS, *_PRESERVE_NOT_NONE_FIELDS)
for k in check_keys:
if k not in val:
continue
v = val[k]
if k in _PRESERVE_NOT_NONE_FIELDS:
if v is not None:
return True
elif v:
return True
return False
for attr in _PRESERVE_TRUTHY_FIELDS:
if getattr(val, attr, None):
return True
for attr in _PRESERVE_NOT_NONE_FIELDS:
if getattr(val, attr, None) is not None:
return True
return False
def unwrap_metadata_value(val: Any, label: Optional[str] = None) -> Any:
"""Unwrap model loader submodel fields (VAEField, encoder fields, UNetField, TransformerField, PiDDecoderField)
into their inner ModelIdentifierField (or ModelIdentifierField-shaped dict) when stored in metadata.
For core model labels (e.g. 'model', 'vae', 'clip_l_model'), wrappers are always unwrapped so gallery recall
can find the top-level 'key'. For custom labels (e.g. user-named labels or CUSTOM_LABEL), outer wrappers with
LoRAs or non-default submodel/routing/config fields are preserved so user modifications are not discarded.
"""
is_custom = label not in CORE_MODEL_LABELS
if isinstance(val, UNetField):
if is_custom and _has_preservable_wrapper_fields(val):
return val
return val.unet
if isinstance(val, (TransformerField, WanTransformerField)):
if is_custom and _has_preservable_wrapper_fields(val):
return val
if isinstance(val, WanTransformerField) and label == "wan_transformer_low_noise":
return val.transformer_low_noise
return val.transformer
if isinstance(val, VAEField):
if is_custom and _has_preservable_wrapper_fields(val):
return val
return val.vae
if isinstance(val, PiDDecoderField):
return val.decoder
if isinstance(val, BaseModel) and not isinstance(val, ModelIdentifierField):
for attr in ("text_encoder", "clip", "transformer", "unet", "vae", "decoder", "vocoder", "latent_upsampler"):
if hasattr(val, attr):
if is_custom and _has_preservable_wrapper_fields(val):
return val
inner = getattr(val, attr)
if _is_model_identifier_value(inner):
return inner
elif isinstance(val, dict) and "key" not in val and "image_name" not in val and "video_name" not in val:
keys = set(val.keys())
if keys.issubset(_WAN_TRANSFORMER_DICT_KEYS) and "transformer" in val:
if is_custom and _has_preservable_wrapper_fields(
val, ("loras", "loras_low_noise", "transformer_low_noise", "boundary_ratio")
):
return val
if label == "wan_transformer_low_noise":
inner = val.get("transformer_low_noise")
if inner is None:
return None
if _is_model_identifier_value(inner):
return inner
return val
inner = val["transformer"]
if _is_model_identifier_value(inner):
return inner
for primary_key, allowed_keys, preserve_keys in _WRAPPER_DICT_SPECS:
if primary_key in val and keys.issubset(allowed_keys):
if is_custom and _has_preservable_wrapper_fields(val, preserve_keys):
return val
inner = val[primary_key]
if _is_model_identifier_value(inner):
return inner
return val
@invocation("metadata_item", title="Metadata Item", tags=["metadata"], category="metadata", version="1.1.0")
class MetadataItemInvocation(BaseInvocation):
"""Used to create an arbitrary metadata item. Provide "label" and make a connection to "value" to store that data as the value."""
label: str = InputField(description=FieldDescriptions.metadata_item_label)
value: Any = InputField(description=FieldDescriptions.metadata_item_value, ui_type=UIType.Any)
def _validate_invoke_fields(self) -> None:
if "value" not in self.model_fields_set:
super()._validate_invoke_fields()
elif self.label is None:
super()._validate_invoke_fields()
def invoke(self, context: InvocationContext) -> MetadataItemOutput:
return MetadataItemOutput(
item=MetadataItemField(label=self.label, value=unwrap_metadata_value(self.value, label=self.label))
)
@invocation_output("metadata_output")
class MetadataOutput(BaseInvocationOutput):
metadata: MetadataField = OutputField(description="Metadata Dict")
@invocation_output("latents_meta_output")
class LatentsMetaOutput(LatentsOutput, MetadataOutput):
"""Base class for invocations that output latents with metadata"""
@classmethod
def build(
cls,
latents_name: str,
latents: torch.Tensor,
seed: Optional[int] = None,
metadata: Optional[MetadataField] = None,
width: Optional[int] = None,
height: Optional[int] = None,
) -> "LatentsMetaOutput":
return cls(
latents=LatentsField(latents_name=latents_name, seed=seed),
width=width if width is not None else latents.shape[-1] * LATENT_SCALE_FACTOR,
height=height if height is not None else latents.shape[-2] * LATENT_SCALE_FACTOR,
metadata=metadata or MetadataField.model_validate({}),
)
@invocation("metadata", title="Metadata", tags=["metadata"], category="metadata", version="1.1.0")
class MetadataInvocation(BaseInvocation):
"""Takes a MetadataItem or collection of MetadataItems and outputs a MetadataDict."""
items: Union[list[MetadataItemField], MetadataItemField] = InputField(
description=FieldDescriptions.metadata_item_polymorphic
)
def invoke(self, context: InvocationContext) -> MetadataOutput:
if isinstance(self.items, MetadataItemField):
# single metadata item
data = {self.items.label: self.items.value} if self.items.value is not None else {}
else:
# collection of metadata items
data = {item.label: item.value for item in self.items if item.value is not None}
# add app version
data.update({"app_version": __version__, "metadata_version": CORE_METADATA_VERSION})
return MetadataOutput(metadata=MetadataField.model_validate(data))
@invocation("merge_metadata", title="Metadata Merge", tags=["metadata"], category="metadata", version="1.0.1")
class MergeMetadataInvocation(BaseInvocation):
"""Merged a collection of MetadataDict into a single MetadataDict."""
collection: list[MetadataField] = InputField(description=FieldDescriptions.metadata_collection)
def invoke(self, context: InvocationContext) -> MetadataOutput:
data = {}
for item in self.collection:
data.update(item.model_dump())
return MetadataOutput(metadata=MetadataField.model_validate(data))
GENERATION_MODES = Literal[
"txt2img",
"img2img",
"inpaint",
"outpaint",
"sdxl_txt2img",
"sdxl_img2img",
"sdxl_inpaint",
"sdxl_outpaint",
"flux_txt2img",
"flux_img2img",
"flux_inpaint",
"flux_outpaint",
"flux2_txt2img",
"flux2_img2img",
"flux2_inpaint",
"flux2_outpaint",
"sd3_txt2img",
"sd3_img2img",
"sd3_inpaint",
"sd3_outpaint",
"cogview4_txt2img",
"cogview4_img2img",
"cogview4_inpaint",
"cogview4_outpaint",
"z_image_txt2img",
"z_image_img2img",
"z_image_inpaint",
"z_image_outpaint",
"ernie_image_txt2img",
"ideogram4_txt2img",
"qwen_image_txt2img",
"qwen_image_img2img",
"qwen_image_inpaint",
"qwen_image_outpaint",
"anima_txt2img",
"anima_img2img",
"anima_inpaint",
"anima_outpaint",
"krea2_txt2img",
"krea2_img2img",
"krea2_inpaint",
"krea2_outpaint",
"qwen_image_2_1_txt2img",
"qwen_image_2_1_img2img",
"qwen_image_2_1_inpaint",
"qwen_image_2_1_outpaint",
"flux3_txt2img",
"flux3_img2img",
"flux3_inpaint",
"flux3_outpaint",
"moss_image_txt2img",
"moss_image_img2img",
"moss_image_inpaint",
"moss_image_outpaint",
"wan_txt2img",
"wan_img2img",
"wan_inpaint",
"wan_outpaint",
"wan_t2v",
"wan_i2v",
"wan_interpolate",
"wan_extend_video",
"minimax_h3_t2v",
"minimax_h3_i2v",
"ltx2_t2v",
"ltx2_i2v",
"ltx2_a2v",
"ltx2_v2a",
"ltx2_lf2v",
"ltx2_flf2v",
"ltx2_extend_video",
"minimax_h3_lf2v",
"minimax_h3_flf2v",
"minimax_h3_extend_video",
"minimax_h3_ref2v",
"minimax_h3_txt2img",
]
class MiniMaxH3ReferenceMetadataField(BaseModel):
"""One recorded Ref2VA reference: names and options only, in conditioning order."""
kind: Literal["image", "video"] = Field(description="Whether the reference was an image or a video.")
image_name: Optional[str] = Field(default=None, description="The reference image's name.")
video_name: Optional[str] = Field(default=None, description="The reference video's name.")
conditioning: Optional[str] = Field(
default=None, description="A video reference's conditioning choice (video_audio / video / audio)."
)
detail: Optional[str] = Field(default=None, description="An image reference's sizing choice (max / match).")
start_frame: Optional[int] = Field(default=None, description="A video reference's trim start (inclusive).")
end_frame: Optional[int] = Field(default=None, description="A video reference's trim end (inclusive).")
# The version of the metadata record `core_metadata` emits, stamped as `metadata_version`. It is
# semver over the *record*, not this node: a minor bump adds keys (any 1.x reader parses any
# other 1.x, ignoring unknown keys); a major bump renames, removes or retypes one. A record
# without the key predates versioning and is read with the same rules. Documented, with the
# field table, in docs/development/Architecture/media-metadata.
CORE_METADATA_VERSION = "1.3.0"
def _serialize_lora_entry(
lora_model: Any,
weight: float,
target: Optional[Literal["high", "low", "both"]] = None,
low_noise_weight: Optional[float] = None,
) -> dict[str, Any]:
if isinstance(lora_model, ModelIdentifierField):
return LoRAMetadataField(
model=lora_model,
weight=weight,
target=target,
low_noise_weight=low_noise_weight,
).model_dump(exclude_none=True, exclude={"id", "type", "is_intermediate", "use_cache"})
entry: dict[str, Any] = {"model": lora_model, "weight": weight}
if target is not None:
entry["target"] = target
if low_noise_weight is not None:
entry["low_noise_weight"] = low_noise_weight
return entry
def _lora_dedup_key(lora: Any) -> Any:
"""Return a hashable deduplication key for ModelIdentifierField, dict, or fallback LoRA values."""
if isinstance(lora, dict):
key = lora.get("key")
if key is not None:
return key
return json.dumps(lora, sort_keys=True, default=str)
key = getattr(lora, "key", None)
if key is not None:
return key
return lora if isinstance(lora, Hashable) else id(lora)
def serialize_lora_metadata(*lora_lists: Optional[Sequence[Any]]) -> list[dict[str, Any]]:
"""Serialize and deduplicate LoRA field sequences into metadata dictionaries."""
seen: set[tuple[Any, float]] = set()
result: list[dict[str, Any]] = []
for lora_list in lora_lists:
if not lora_list:
continue
for item in lora_list:
key = _lora_dedup_key(item.lora)
ident = (key, item.weight)
if ident in seen:
continue
seen.add(ident)
result.append(_serialize_lora_entry(item.lora, item.weight))
return result
def serialize_wan_lora_metadata(
high_loras: Optional[Sequence[Any]],
low_loras: Optional[Sequence[Any]],
) -> list[dict[str, Any]]:
"""Serialize Wan high/low expert LoRA lists into one metadata entry per unique LoRA key."""
high_by_key: dict[Any, Any] = {}
for item in high_loras or ():
key = _lora_dedup_key(item.lora)
if key not in high_by_key:
high_by_key[key] = item
low_by_key: dict[Any, Any] = {}
for item in low_loras or ():
key = _lora_dedup_key(item.lora)
if key not in low_by_key:
low_by_key[key] = item
result: list[dict[str, Any]] = []
for key, high_item in high_by_key.items():
low_item = low_by_key.get(key)
if low_item is None:
result.append(_serialize_lora_entry(high_item.lora, high_item.weight, target="high"))
elif high_item.weight == low_item.weight:
result.append(_serialize_lora_entry(high_item.lora, high_item.weight, target="both"))
else:
result.append(
_serialize_lora_entry(
high_item.lora,
high_item.weight,
target="both",
low_noise_weight=low_item.weight,
)
)
for key, low_item in low_by_key.items():
if key not in high_by_key:
result.append(_serialize_lora_entry(low_item.lora, low_item.weight, target="low"))
return result
def serialize_adapter_metadata(item_or_items: Any) -> list[Any]:
"""Normalize a single adapter field or list of adapter fields into serialized metadata dicts."""
if item_or_items is None:
return []
items = item_or_items if isinstance(item_or_items, list) else [item_or_items]
return [
item.model_dump(exclude_none=True, exclude={"id", "type", "is_intermediate", "use_cache"})
if hasattr(item, "model_dump")
else item
for item in items
]
def serialize_ip_adapter_ref_images(
ip_adapter: Any,
context: Optional[Any] = None,
default_width: int = 1024,
default_height: int = 1024,
) -> list[dict[str, Any]]:
"""Serialize IP-Adapter inputs into v7 GenerateReferenceImage metadata entries (`ref_images`)."""
if ip_adapter is None:
return []
items = ip_adapter if isinstance(ip_adapter, list) else [ip_adapter]
ref_images: list[dict[str, Any]] = []
idx = 0
for item in items:
raw_image = item.get("image") if isinstance(item, dict) else getattr(item, "image", None)
if raw_image is None:
raw_image_name = item.get("image_name") if isinstance(item, dict) else getattr(item, "image_name", None)
if raw_image_name:
raw_image = {"image_name": raw_image_name}
else:
continue
img_list = raw_image if isinstance(raw_image, list) else [raw_image]
ip_adapter_model = (
item.get("ip_adapter_model") if isinstance(item, dict) else getattr(item, "ip_adapter_model", None)
)
image_encoder_model = (
item.get("image_encoder_model") if isinstance(item, dict) else getattr(item, "image_encoder_model", None)
)
if hasattr(ip_adapter_model, "model_dump"):
model_dict = ip_adapter_model.model_dump(exclude_none=True, exclude={"id", "is_intermediate", "use_cache"})
elif isinstance(ip_adapter_model, dict):
model_dict = dict(ip_adapter_model)
model_dict.setdefault("type", "ip_adapter")
else:
model_dict = {}
explicit_cvm = (
item.get("clip_vision_model") if isinstance(item, dict) else getattr(item, "clip_vision_model", None)
)
if explicit_cvm in ("ViT-H", "ViT-G", "ViT-L"):
clip_vision_model = explicit_cvm
else:
enc_name = (
image_encoder_model.get("name", "")
if isinstance(image_encoder_model, dict)
else getattr(image_encoder_model, "name", "")
) or ""
enc_lower = str(enc_name).lower()
if "vit-l" in enc_lower or "large" in enc_lower:
clip_vision_model = "ViT-L"
elif "sdxl" in enc_lower or "vit-g" in enc_lower:
clip_vision_model = "ViT-G"
else:
clip_vision_model = "ViT-H"
method = (item.get("method") if isinstance(item, dict) else getattr(item, "method", None)) or "full"
has_weight = ("weight" in item) if isinstance(item, dict) else hasattr(item, "weight")
has_strength = ("strength" in item) if isinstance(item, dict) else hasattr(item, "strength")
if has_weight:
raw_weight = item["weight"] if isinstance(item, dict) else item.weight
elif has_strength:
raw_weight = item["strength"] if isinstance(item, dict) else item.strength
if raw_weight is not None and float(raw_weight) <= 0.0:
continue
else:
raw_weight = 1.0
if isinstance(raw_weight, (list, tuple)):
weight: Union[float, list[float]] = [float(w) for w in raw_weight]
else:
weight = float(raw_weight)
begin_step_percent = float(
item.get("begin_step_percent", 0.0) if isinstance(item, dict) else getattr(item, "begin_step_percent", 0.0)
)
end_step_percent = float(
item.get("end_step_percent", 1.0) if isinstance(item, dict) else getattr(item, "end_step_percent", 1.0)
)
raw_mask = item.get("mask") if isinstance(item, dict) else getattr(item, "mask", None)
serialized_mask = None
if raw_mask is not None:
# Ephemeral tensor masks (TensorField) are runtime-only and must not be serialized into metadata.
is_tensor_mask = (
isinstance(raw_mask, TensorField)
or getattr(raw_mask, "tensor_name", None) is not None
or (isinstance(raw_mask, dict) and "tensor_name" in raw_mask)
)
if not is_tensor_mask:
if hasattr(raw_mask, "model_dump"):
serialized_mask = raw_mask.model_dump(exclude_none=True)
elif isinstance(raw_mask, dict):
serialized_mask = dict(raw_mask)
else:
serialized_mask = raw_mask
for img in img_list:
img_name = img.get("image_name") if isinstance(img, dict) else getattr(img, "image_name", None)
if not img_name:
continue
img_width, img_height = int(default_width), int(default_height)
if context is not None:
try:
dto = context.images.get_dto(img_name)
dto_w = getattr(dto, "width", None)
dto_h = getattr(dto, "height", None)
if isinstance(dto_w, int) and dto_w > 0 and isinstance(dto_h, int) and dto_h > 0:
img_width, img_height = int(dto_w), int(dto_h)
except Exception:
pass
idx += 1
config_entry: dict[str, Any] = {
"type": "ip_adapter",
"image": {
"original": {
"image": {
"image_name": img_name,
"width": img_width,
"height": img_height,
}
}
},
"model": model_dict,
"clipVisionModel": clip_vision_model,
"method": method,
"weight": weight,
"beginEndStepPct": [begin_step_percent, end_step_percent],
}
if serialized_mask is not None:
config_entry["mask"] = serialized_mask
ref_images.append(
{
"id": f"ip_adapter_{idx}",
"isEnabled": True,
"config": config_entry,
}
)
return ref_images
def _ref_image_dedup_key(entry: Any) -> Optional[tuple[Any, Any, Any]]:
if not isinstance(entry, dict):
return None
cfg = entry.get("config")
if not isinstance(cfg, dict):
return None
cfg_type = cfg.get("type")
img = cfg.get("image")
image_name = None
if isinstance(img, dict):
crop = img.get("crop")
orig = img.get("original")
if isinstance(crop, dict) and isinstance(crop.get("image"), dict):
image_name = crop["image"].get("image_name")
if not image_name and isinstance(orig, dict) and isinstance(orig.get("image"), dict):
image_name = orig["image"].get("image_name")
if not image_name:
image_name = img.get("image_name")
if not image_name:
return None
model = cfg.get("model")
model_key = model.get("key") if isinstance(model, dict) else getattr(model, "key", None)
return (cfg_type, image_name, model_key)
def build_denoise_metadata(existing: Optional[MetadataField], updates: dict[str, Any]) -> MetadataField:
"""Build a new MetadataField by copying existing metadata and merging denoise updates without mutating input."""
md: dict[str, Any] = {} if existing is None else existing.root.copy()
existing_gen_mode = md.get("generation_mode")
md.update(updates)
if existing_gen_mode is not None and "generation_mode" in updates:
md["generation_mode"] = existing_gen_mode
# Denoise execution lists must be authoritative to the executing denoise node.
authoritative_lists = ("loras", "controlnets", "t2iAdapters", "ref_images")
for key in authoritative_lists:
if key in updates:
md[key] = updates[key]
if key == "controlnets":
md.pop("control_layers", None)
elif key == "ref_images":
md.pop("ipAdapters", None)
md["app_version"] = __version__
md.setdefault("metadata_version", CORE_METADATA_VERSION)
return MetadataField.model_validate(md)
@invocation(
"core_metadata",
title="Core Metadata",
tags=["metadata"],
category="metadata",
version="2.10.0",
classification=Classification.Internal,
)
class CoreMetadataInvocation(BaseInvocation):
"""Used internally by Invoke to collect metadata for generations."""
generation_mode: Optional[GENERATION_MODES] = InputField(
default=None,
description="The generation mode that output this image",
)
positive_prompt: Optional[str] = InputField(default=None, description="The positive prompt parameter")
negative_prompt: Optional[str] = InputField(default=None, description="The negative prompt parameter")
width: Optional[int] = InputField(default=None, description="The width parameter")
height: Optional[int] = InputField(default=None, description="The height parameter")
seed: Optional[int] = InputField(default=None, description="The seed used for noise generation")
rand_device: Optional[str] = InputField(default=None, description="The device used for random number generation")
cfg_scale: Optional[float | list[float]] = InputField(
default=None, description="The classifier-free guidance scale parameter (scalar or per-step schedule)"
)
cfg_rescale_multiplier: Optional[float] = InputField(
default=None, description=FieldDescriptions.cfg_rescale_multiplier
)
steps: Optional[int] = InputField(default=None, description="The number of steps used for inference")
scheduler: Optional[str] = InputField(default=None, description="The scheduler used for inference")
seamless_x: Optional[bool] = InputField(default=None, description="Whether seamless tiling was used on the X axis")
seamless_y: Optional[bool] = InputField(default=None, description="Whether seamless tiling was used on the Y axis")
clip_skip: Optional[int] = InputField(
default=None,
description="The number of skipped CLIP layers",
)
model: Optional[ModelIdentifierField] = InputField(default=None, description="The main model used for inference")
controlnets: Optional[list[ControlNetMetadataField]] = InputField(
default=None, description="The ControlNets used for inference"
)
ipAdapters: Optional[list[IPAdapterMetadataField]] = InputField(
default=None, description="The IP Adapters used for inference"
)
t2iAdapters: Optional[list[T2IAdapterMetadataField]] = InputField(
default=None, description="The IP Adapters used for inference"
)
loras: Optional[list[LoRAMetadataField]] = InputField(default=None, description="The LoRAs used for inference")
strength: Optional[float] = InputField(
default=None,
description="The strength used for latents-to-latents",
)
init_image: Optional[str] = InputField(
default=None,
description="The name of the initial image",
)
vae: Optional[ModelIdentifierField] = InputField(
default=None,
description="The VAE used for decoding, if the main model's default was not used",
)
qwen3_encoder: Optional[ModelIdentifierField] = InputField(
default=None,
description="The Qwen3 text encoder model used for Z-Image inference",
)
# FLUX.2 [dev] and ERNIE-Image use Mistral-family text encoders where FLUX.2 Klein uses Qwen3, so they
# need their own slot rather than reusing `qwen3_encoder` - the two are never both present on one image.
mistral_encoder: Optional[ModelIdentifierField] = InputField(
default=None,
description="The Mistral text encoder model used for FLUX.2 [dev] or ERNIE-Image inference",
)
# Ideogram 4 assembles its structured JSON caption at generation time (ideogram4_caption_builder),
# so this is a declared field rather than a static extra: the graph wires the builder's output to it
# via an edge, capturing the exact caption encoded for each (possibly batched) image.
ideogram4_caption: Optional[str] = InputField(
default=None,
description="The structured JSON caption encoded for Ideogram 4 inference",
)
# MOSS-Image places canvas regions on the prompt at generation time (moss_image_layout_prompt), so, as for
# `ideogram4_caption`, the graph records the layout prompt actually encoded through an edge.
moss_image_layout_prompt: Optional[str] = InputField(
default=None,
description="The JSON layout prompt encoded for MOSS-Image inference",
)
# FLUX.3 can encode a caption other than the typed prompt: canvas regions append a box table to it at generation
# time (flux3_layout_prompt). As for `ideogram4_caption`, an edge records the caption encoded.
flux3_caption: Optional[str] = InputField(
default=None,
description="The caption encoded for FLUX.3 inference, when it is not the positive prompt as typed",
)
# Video generation. Frame count and keyframes have no home among the image fields, and
# the two MiniMax H3 single-file overrides are separate models from the main install, so
# `model` alone would not identify what actually ran.
num_frames: Optional[int] = InputField(
default=None,
description="The number of video frames generated",
)
first_frame_image: Optional[ImageField] = InputField(
default=None,
description="The image used as the video's first frame",
)
last_frame_image: Optional[ImageField] = InputField(
default=None,
description="The image used as the video's last frame",
)
source_video: Optional[VideoField] = InputField(
default=None,
description="The video this generation was derived from, e.g. the clip an extend workflow continues",
)
source_video_start_frame: Optional[int] = InputField(
default=None,
description="The first frame (inclusive) of the source video that was kept",
)
source_video_end_frame: Optional[int] = InputField(
default=None,
description="The last frame (inclusive) of the source video that was kept",
)
ltx2_context_frames: Optional[int] = InputField(
default=None,
description="Frames of the source an LTX-2 continuation opened with, which the join then crossfaded "
"out of both halves",
)
fps: Optional[int] = InputField(
default=None,
description="The frame rate of the generated video",
)
wan_guidance_scale_low_noise: Optional[float] = InputField(
default=None,
description="The classifier-free guidance scale used by the Wan low-noise expert, when it differed",
)
wan_t5_encoder_model: Optional[ModelIdentifierField] = InputField(
default=None,
description="The standalone UMT5-XXL encoder used with a single-file Wan main model",
)
wan_transformer_low_noise: Optional[ModelIdentifierField] = InputField(
default=None,
description="The standalone low-noise expert used with a single-file Wan main model",
)
wan_component_source: Optional[ModelIdentifierField] = InputField(
default=None,
description="The Wan Diffusers install whose VAE and encoder served a single-file main model",
)
minimax_h3_transformer_model: Optional[ModelIdentifierField] = InputField(
default=None,
description="The single-file MiniMax H3 transformer used in place of the main model's transformer",
)
minimax_h3_text_encoder_model: Optional[ModelIdentifierField] = InputField(
default=None,
description="The single-file MiniMax H3 Qwen3-VL text encoder used in place of the main model's",
)
minimax_h3_component_source: Optional[ModelIdentifierField] = InputField(
default=None,
description="The MiniMax H3 Diffusers install whose VAEs and encoder served a single-file transformer",
)
minimax_h3_hybrid_base_model: Optional[ModelIdentifierField] = InputField(
default=None,
description="The FL2VA transformer whose later blocks overlaid the Ref2VA transformer (the hybrid)",
)
minimax_h3_hybrid_start_block: Optional[int] = InputField(
default=None,
description="The first transformer block taken from the hybrid base",
)
minimax_h3_references: Optional[list[MiniMaxH3ReferenceMetadataField]] = InputField(
default=None,
description="The ordered Ref2VA references this generation was conditioned on (names and options only; "
"the media is re-resolved from the gallery at recall time)",
)
# High resolution fix metadata.
hrf_enabled: Optional[bool] = InputField(
default=None,
description="Whether or not high resolution fix was enabled.",
)
# TODO: should this be stricter or do we just let the UI handle it?
hrf_method: Optional[str] = InputField(
default=None,
description="The high resolution fix upscale method.",
)
hrf_strength: Optional[float] = InputField(
default=None,
description="The high resolution fix img2img strength used in the upscale pass.",
)
# SDXL
positive_style_prompt: Optional[str] = InputField(
default=None,
description="The positive style prompt parameter",
)
negative_style_prompt: Optional[str] = InputField(
default=None,
description="The negative style prompt parameter",
)
# SDXL Refiner
refiner_model: Optional[ModelIdentifierField] = InputField(
default=None,
description="The SDXL Refiner model used",
)
refiner_cfg_scale: Optional[float] = InputField(
default=None,
description="The classifier-free guidance scale parameter used for the refiner",
)
refiner_steps: Optional[int] = InputField(
default=None,
description="The number of steps used for the refiner",
)
refiner_scheduler: Optional[str] = InputField(
default=None,
description="The scheduler used for the refiner",
)
refiner_positive_aesthetic_score: Optional[float] = InputField(
default=None,
description="The aesthetic score used for the refiner",
)
refiner_negative_aesthetic_score: Optional[float] = InputField(
default=None,
description="The aesthetic score used for the refiner",
)
refiner_start: Optional[float] = InputField(
default=None,
description="The start value used for refiner denoising",
)
def invoke(self, context: InvocationContext) -> MetadataOutput:
"""Collects and outputs a CoreMetadata object"""
as_dict = self.model_dump(exclude_none=True, exclude={"id", "type", "is_intermediate", "use_cache"})
as_dict["app_version"] = __version__
as_dict["metadata_version"] = CORE_METADATA_VERSION
return MetadataOutput(metadata=MetadataField.model_validate(as_dict))
model_config = ConfigDict(extra="allow")
@invocation(
"metadata_field_extractor",
title="Metadata Field Extractor",
tags=["metadata"],
category="metadata",
version="1.0.0",
classification=Classification.Deprecated,
)
class MetadataFieldExtractorInvocation(BaseInvocation):
"""Extracts the text value from an image's metadata given a key.
Raises an error if the image has no metadata or if the value is not a string (nesting not permitted)."""
image: ImageField = InputField(description="The image to extract metadata from")
key: str = InputField(description="The key in the image's metadata to extract the value from")
def invoke(self, context: InvocationContext) -> StringOutput:
image_name = self.image.image_name
metadata = context.images.get_metadata(image_name=image_name)
if not metadata:
raise ValueError(f"No metadata found on image {image_name}")
try:
val = metadata.root[self.key]
if not isinstance(val, str):
raise ValueError(f"Metadata at key '{self.key}' must be a string")
return StringOutput(value=val)
except KeyError as e:
raise ValueError(f"No key '{self.key}' found in the metadata for {image_name}") from e