Skip to content

API Development

Each invocation’s invoke method is provided a single arg - the Invocation Context.

This object provides an API the invocation can use to interact with application services, for example:

  • Saving images
  • Logging messages
  • Loading models
class MyInvocation(BaseInvocation):
...
def invoke(self, context: InvocationContext) -> ImageOutput:
# Load an image
image_pil = context.images.get_pil(self.image.image_name)
# Do something to the image
output_image = do_something_cool(image_pil)
# Save the image
image_dto = context.images.save(output_image)
# Log a message
context.logger.info(f"Did something cool, image saved!")
# Return the output
return ImageOutput.build(image_dto)
...

The full generated API reference is documented below.

Two important mixins are provided to facilitate working with metadata and gallery boards.

Inherit from this class (in addition to BaseInvocation) to add a metadata input to your node. When you do this, you can access the metadata dict from self.metadata in the invoke() function.

The dict will be populated via the node’s input, and you can add any metadata you’d like to it. When you call context.images.save(), if the metadata dict has any data, it be automatically embedded in the image.

Inherit from this class (in addition to BaseInvocation) to add a board input to your node. This renders as a drop-down to select a board. The user’s selection will be accessible from self.board in the invoke() function.

When you call context.images.save(), if a board was selected, the image will added to that board as it is saved.

ImagesInterfacecontext.images

MethodDescription
get_dtoGets an image as an ImageDTO object.
get_metadataGets an image's metadata, if it has any.
get_pathGets the internal path to an image or thumbnail.
get_pilGets an image as a PIL Image object. This method returns a copy of the image.
saveSaves an image, returning its DTO.

get_dto

(image_name: str) -> ImageDTO

Gets an image as an ImageDTO object.

NameTypeDescriptionDefault
image_namestrThe name of the image to get.required
TypeDescription
ImageDTOThe image as an ImageDTO object.

get_metadata

(image_name: str) -> Optional[MetadataField]

Gets an image's metadata, if it has any.

NameTypeDescriptionDefault
image_namestrThe name of the image to get the metadata for.required
TypeDescription
Optional[MetadataField]The image's metadata, if it has any.

get_path

(image_name: str, thumbnail: bool = False) -> Path

Gets the internal path to an image or thumbnail.

NameTypeDescriptionDefault
image_namestrThe name of the image to get the path of.required
thumbnailboolGet the path of the thumbnail instead of the full imageFalse
TypeDescription
PathThe local path of the image or thumbnail.

get_pil

(image_name: str, mode: Optional[Literal['L', 'RGB', 'RGBA', 'CMYK', 'YCbCr', 'LAB', 'HSV', 'I', 'F']] = None) -> Image

Gets an image as a PIL Image object. This method returns a copy of the image.

NameTypeDescriptionDefault
image_namestrThe name of the image to get.required
modeOptional[Literal['L', 'RGB', 'RGBA', 'CMYK', 'YCbCr', 'LAB', 'HSV', 'I', 'F']]The color mode to convert the image to. If None, the original mode is used.None
TypeDescription
ImageThe image as a PIL Image object.

save

(image: Image, board_id: Optional[str] = None, image_category: ImageCategory = ImageCategory.GENERAL, metadata: Optional[MetadataField] = None) -> ImageDTO

Saves an image, returning its DTO. If the current queue item has a workflow or metadata, it is automatically saved with the image.

NameTypeDescriptionDefault
imageImageThe image to save, as a PIL image.required
board_idOptional[str]The board ID to add the image to, if it should be added. It the invocation inherits from WithBoard, that board will be used automatically. Use this only if you want to override or provide a board manually!None
image_categoryImageCategoryThe category of the image. Only the GENERAL category is added to the gallery.ImageCategory.GENERAL
metadataOptional[MetadataField]The metadata to save with the image, if it should have any. If the invocation inherits from WithMetadata, that metadata will be used automatically. Use this only if you want to override or provide metadata manually!None
TypeDescription
ImageDTOThe saved image DTO.

TensorsInterfacecontext.tensors

MethodDescription
loadLoads a tensor by name. This method returns a copy of the tensor.
saveSaves a tensor, returning its name.

load

(name: str) -> Tensor

Loads a tensor by name. This method returns a copy of the tensor.

NameTypeDescriptionDefault
namestrThe name of the tensor to load.required
TypeDescription
TensorThe tensor.

save

(tensor: Tensor) -> str

Saves a tensor, returning its name.

NameTypeDescriptionDefault
tensorTensorThe tensor to save.required
TypeDescription
strThe name of the saved tensor.

ConditioningInterfacecontext.conditioning

MethodDescription
loadLoads conditioning data by name. This method returns a copy of the conditioning data.
saveSaves a conditioning data object, returning its name.

load

(name: str) -> ConditioningFieldData

Loads conditioning data by name. This method returns a copy of the conditioning data.

NameTypeDescriptionDefault
namestrThe name of the conditioning data to load.required
TypeDescription
ConditioningFieldDataThe conditioning data.

save

(conditioning_data: ConditioningFieldData) -> str

Saves a conditioning data object, returning its name.

NameTypeDescriptionDefault
conditioning_dataConditioningFieldDataThe conditioning data to save.required
TypeDescription
strThe name of the saved conditioning data.

ModelsInterfacecontext.models

Common API for loading, downloading and managing models.

MethodDescription
download_and_cache_modelDownload the model file located at source to the models cache and return its Path.
existsCheck if a model exists.
get_absolute_pathGets the absolute path for a given model config or path.
get_configGet a model's config.
loadLoad a model.
load_by_attrsLoad a model by its attributes.
load_local_modelLoad the model file located at the indicated path
load_remote_modelDownload, cache, and load the model file located at the indicated URL or repo_id.
search_by_attrsSearch for models by attributes.
search_by_pathSearch for models by path.

download_and_cache_model

(source: str | AnyHttpUrl) -> Path

Download the model file located at source to the models cache and return its Path. This can be used to single-file install models and other resources of arbitrary types which should not get registered with the database. If the model is already installed, the cached path will be returned. Otherwise it will be downloaded.

NameTypeDescriptionDefault
sourcestr | AnyHttpUrlA URL that points to the model, or a huggingface repo_id.required
TypeDescription
PathPath to the downloaded model

exists

(identifier: Union[str, ModelIdentifierField]) -> bool

Check if a model exists.

NameTypeDescriptionDefault
identifierUnion[str, ModelIdentifierField]The key or ModelField representing the model.required
TypeDescription
boolTrue if the model exists, False if not.

get_absolute_path

(config_or_path: Union[AnyModelConfig, Path, str]) -> Path

Gets the absolute path for a given model config or path. For example, if the model's path is flux/main/FLUX Dev.safetensors, and the models path is /home/username/InvokeAI/models, this method will return /home/username/InvokeAI/models/flux/main/FLUX Dev.safetensors.

NameTypeDescriptionDefault
config_or_pathUnion[AnyModelConfig, Path, str]The model config or path.required
TypeDescription
PathThe absolute path to the model.

get_config

(identifier: Union[str, ModelIdentifierField]) -> AnyModelConfig

Get a model's config.

NameTypeDescriptionDefault
identifierUnion[str, ModelIdentifierField]The key or ModelField representing the model.required
TypeDescription
AnyModelConfigThe model's config.

load

(identifier: Union[str, ModelIdentifierField], submodel_type: Optional[SubModelType] = None) -> LoadedModel

Load a model.

NameTypeDescriptionDefault
identifierUnion[str, ModelIdentifierField]The key or ModelField representing the model.required
submodel_typeOptional[SubModelType]The submodel of the model to get.None
TypeDescription
LoadedModelAn object representing the loaded model.

load_by_attrs

(name: str, base: BaseModelType, type: ModelType, submodel_type: Optional[SubModelType] = None) -> LoadedModel

Load a model by its attributes.

NameTypeDescriptionDefault
namestrName of the model.required
baseBaseModelTypeThe models' base type, e.g. BaseModelType.StableDiffusion1, BaseModelType.StableDiffusionXL, etc.required
typeModelTypeType of the model, e.g. ModelType.Main, ModelType.Vae, etc.required
submodel_typeOptional[SubModelType]The type of submodel to load, e.g. SubModelType.UNet, SubModelType.TextEncoder, etc. Only main models have submodels.None
TypeDescription
LoadedModelAn object representing the loaded model.

load_local_model

(model_path: Path, loader: Optional[Callable[[Path], AnyModel]] = None) -> LoadedModelWithoutConfig

Load the model file located at the indicated path If a loader callable is provided, it will be invoked to load the model. Otherwise, safetensors.torch.load_file() or torch.load() will be called to load the model. Be aware that the LoadedModelWithoutConfig object has no config attribute

NameTypeDescriptionDefault
model_pathPathrequired
loaderOptional[Callable[[Path], AnyModel]]A Callable that expects a Path and returns a dict[str|int, Any]None
TypeDescription
LoadedModelWithoutConfigA LoadedModelWithoutConfig object.

load_remote_model

(source: str | AnyHttpUrl, loader: Optional[Callable[[Path], AnyModel]] = None) -> LoadedModelWithoutConfig

Download, cache, and load the model file located at the indicated URL or repo_id. If the model is already downloaded, it will be loaded from the cache. If the a loader callable is provided, it will be invoked to load the model. Otherwise, safetensors.torch.load_file() or torch.load() will be called to load the model. Be aware that the LoadedModelWithoutConfig object has no config attribute

NameTypeDescriptionDefault
sourcestr | AnyHttpUrlA URL or huggingface repoid.required
loaderOptional[Callable[[Path], AnyModel]]A Callable that expects a Path and returns a dict[str|int, Any]None
TypeDescription
LoadedModelWithoutConfigA LoadedModelWithoutConfig object.

search_by_attrs

(name: Optional[str] = None, base: Optional[BaseModelType] = None, type: Optional[ModelType] = None, format: Optional[ModelFormat] = None) -> list[AnyModelConfig]

Search for models by attributes.

NameTypeDescriptionDefault
nameOptional[str]The name to search for (exact match).None
baseOptional[BaseModelType]The base to search for, e.g. BaseModelType.StableDiffusion1, BaseModelType.StableDiffusionXL, etc.None
typeOptional[ModelType]Type type of model to search for, e.g. ModelType.Main, ModelType.Vae, etc.None
formatOptional[ModelFormat]The format of model to search for, e.g. ModelFormat.Checkpoint, ModelFormat.Diffusers, etc.None
TypeDescription
list[AnyModelConfig]A list of models that match the attributes.

search_by_path

(path: Path) -> list[AnyModelConfig]

Search for models by path.

NameTypeDescriptionDefault
pathPathThe path to search for.required
TypeDescription
list[AnyModelConfig]A list of models that match the path.

LoggerInterfacecontext.logger

MethodDescription
debugLogs a debug message.
errorLogs an error message.
infoLogs an info message.
warningLogs a warning message.

debug

(message: str) -> None

Logs a debug message.

NameTypeDescriptionDefault
messagestrThe message to log.required
TypeDescription
None

error

(message: str) -> None

Logs an error message.

NameTypeDescriptionDefault
messagestrThe message to log.required
TypeDescription
None

info

(message: str) -> None

Logs an info message.

NameTypeDescriptionDefault
messagestrThe message to log.required
TypeDescription
None

warning

(message: str) -> None

Logs a warning message.

NameTypeDescriptionDefault
messagestrThe message to log.required
TypeDescription
None

ConfigInterfacecontext.config

MethodDescription
getGets the app's config.

get

() -> InvokeAIAppConfig

Gets the app's config.

TypeDescription
InvokeAIAppConfigThe app's config.

UtilInterfacecontext.util

MethodDescription
flux2_step_callbackThe step callback for FLUX.2 Klein models (32-channel VAE).
flux_step_callbackThe step callback emits a progress event with the current step, the total number of
is_canceledChecks if the current session has been canceled.
sd_step_callbackThe step callback emits a progress event with the current step, the total number of
signal_progressSignals the progress of some long-running invocation. The progress is displayed in the UI.

flux2_step_callback

(intermediate_state: PipelineIntermediateState) -> None

The step callback for FLUX.2 Klein models (32-channel VAE).

NameTypeDescriptionDefault
intermediate_statePipelineIntermediateStateThe intermediate state of the diffusion pipeline.required
TypeDescription
None

flux_step_callback

(intermediate_state: PipelineIntermediateState) -> None

The step callback emits a progress event with the current step, the total number of steps, a preview image, and some other internal metadata. This should be called after each denoising step.

NameTypeDescriptionDefault
intermediate_statePipelineIntermediateStateThe intermediate state of the diffusion pipeline.required
TypeDescription
None

is_canceled

() -> bool

Checks if the current session has been canceled.

TypeDescription
boolTrue if the current session has been canceled, False if not.

sd_step_callback

(intermediate_state: PipelineIntermediateState, base_model: BaseModelType) -> None

The step callback emits a progress event with the current step, the total number of steps, a preview image, and some other internal metadata. This should be called after each denoising step.

NameTypeDescriptionDefault
intermediate_statePipelineIntermediateStateThe intermediate state of the diffusion pipeline.required
base_modelBaseModelTypeThe base model for the current denoising step.required
TypeDescription
None

signal_progress

(message: str, percentage: float | None = None, image: Image | None = None, image_size: tuple[int, int] | None = None) -> None

Signals the progress of some long-running invocation. The progress is displayed in the UI. If a percentage is provided, the UI will display a progress bar and automatically append the percentage to the message. You should not include the percentage in the message. Example:

total_steps = 10
for i in range(total_steps):
percentage = i / (total_steps - 1)
context.util.signal_progress("Doing something cool", percentage)
If an image is provided, the UI will display it. If your image should be displayed at a different size, provide a tuple of (width, height) for the image_size parameter. The image will be displayed at the specified size in the UI. For example, SD denoising progress images are 1/8 the size of the original image, so you'd do this to ensure the image is displayed at the correct size:
# Calculate the output size of the image (8x the progress image's size)
width = progress_image.width * 8
height = progress_image.height * 8
# Signal the progress with the image and output size
signal_progress("Denoising", percentage, progress_image, (width, height))
If your progress image is very large, consider downscaling it to reduce the payload size and provide the original size to the image_size parameter. The PIL thumbnail method is useful for this, as it maintains the aspect ratio of the image:
# thumbnail modifies the image in-place, so we need to first make a copy
thumbnail_image = progress_image.copy()
# Resize the image to a maximum of 256x256 pixels, maintaining the aspect ratio
thumbnail_image.thumbnail((256, 256))
# Signal the progress with the thumbnail, passing the original size
signal_progress("Denoising", percentage, thumbnail, progress_image.size)

NameTypeDescriptionDefault
messagestrA message describing the current status. Do not include the percentage in this message.required
percentagefloat | NoneThe current percentage completion for the process. Omit for indeterminate progress.None
imageImage | NoneAn optional image to display.None
image_sizetuple[int, int] | NoneThe optional size of the image to display. If omitted, the image will be displayed at its original size.None
TypeDescription
None

BoardsInterfacecontext.boards

MethodDescription
add_image_to_boardAdds an image to a board.
createCreates a board for the current user.
get_allGets all boards accessible to the current user.
get_all_image_names_for_boardGets all image names for a board.
get_dtoGets a board DTO.

add_image_to_board

(board_id: str, image_name: str) -> None

Adds an image to a board.

NameTypeDescriptionDefault
board_idstrThe ID of the board to add the image to.required
image_namestrThe name of the image to add to the board.required
TypeDescription
None

create

(board_name: str) -> BoardDTO

Creates a board for the current user.

NameTypeDescriptionDefault
board_namestrThe name of the board to create.required
TypeDescription
BoardDTOThe created board DTO.

get_all

() -> list[BoardDTO]

Gets all boards accessible to the current user.

TypeDescription
list[BoardDTO]A list of all boards accessible to the current user.

get_all_image_names_for_board

(board_id: str) -> list[str]

Gets all image names for a board.

NameTypeDescriptionDefault
board_idstrThe ID of the board to get the image names for.required
TypeDescription
list[str]A list of all image names for the board.

get_dto

(board_id: str) -> BoardDTO

Gets a board DTO.

NameTypeDescriptionDefault
board_idstrThe ID of the board to get.required
TypeDescription
BoardDTOThe board DTO.
This site was designed and developed by Aether Fox Studio.