Building ComfyUI custom nodes for a remote generation API: polling, tensors, VIDEO outputs and keeping keys out of workflows
ComfyUI is usually about running models on your own GPU, but a growing number of node packs call remote APIs instead: the graph stays local, the heavy lifting happens on someone else's hardware. I recently wrote one of these, comfyui-vellria , which wraps Vellria, a hosted image and video generation API ( vellria.com ). It is MIT licensed and small (five short Python modules), so it makes a…
ComfyUI is commonly used for running models locally on a GPU, but many node packs now call remote APIs instead. One example is comfyui-vellria, which wraps a hosted image and video generation API called Vellria. This post focuses on the more challenging aspects of creating custom nodes for such remote generation APIs.
ComfyUI discovers custom nodes in the ComfyUI/custom_nodes/ directory and imports them as Python packages. Each node is a simple class with several key components: INPUT_TYPES defines the necessary widgets and sockets, RETURN_TYPES specifies the outputs, and FUNCTION indicates the method ComfyUI will call. This classic interface is kept for compatibility with older ComfyUI installations.
For the root __init__.py file, ComfyUI checks if it's being loaded as a package (as in a ComfyUI environment) or as a standalone module (like when being tested with pytest). This ensures the module imports cleanly in both contexts. Additionally, the node pack fetches the list of available models via a GET /v1/models request, with a 5-second timeout and several fallback options in case of network issues or JSON errors.
The asynchronous nature of image and video generation on the Vellria API means that a POST request returns a 202 status code with a job ID. The client then needs to poll the GET /v1/generations/{id} endpoint until the job's status is either completed or failed. The poll interval starts at 2 seconds and increases by 1.25 times for each subsequent attempt, up to a maximum of 8 seconds. The node must handle transient errors gracefully, retrying up to five times before raising an exception.
Cancellation is often overlooked but crucial. When a user cancels a job in the ComfyUI queue, the node must check for a cancellation flag using comfy.model_management.throw_exception_if_processing_interrupted(). This ensures that the Python thread dedicated to the job can be interrupted, preventing it from running indefinitely. If a node simply sleeps for a fixed duration without checking for cancellation, it may continue running for up to 8 seconds per poll, wasting resources.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to