Comfy-Org/ComfyUI · error · ValueError

Invalid operation {operation} for morphology. Must be one of

Error message

Invalid operation {operation} for morphology. Must be one of 'erode', 'dilate', 'open', 'close', 'gradient', 'tophat', 'bottomhat'

What it means

Raised by the Morphology node when the operation string matches none of the if/elif branches. The combo UI restricts choices, but the API accepts any string, so only out-of-band values reach this raise. Note: the message lists 'tophat'/'bottomhat' but the code (and combo options) actually require 'top_hat'/'bottom_hat' with underscores — the message is misleading.

Source

Thrown at comfy_extras/nodes_morphology.py:51

        device = comfy.model_management.get_torch_device()
        kernel = torch.ones(kernel_size, kernel_size, device=device)
        image_k = image.to(device).movedim(-1, 1)
        if operation == "erode":
            output = erosion(image_k, kernel)
        elif operation == "dilate":
            output = dilation(image_k, kernel)
        elif operation == "open":
            output = opening(image_k, kernel)
        elif operation == "close":
            output = closing(image_k, kernel)
        elif operation == "gradient":
            output = gradient(image_k, kernel)
        elif operation == "top_hat":
            output = top_hat(image_k, kernel)
        elif operation == "bottom_hat":
            output = bottom_hat(image_k, kernel)
        else:
            raise ValueError(f"Invalid operation {operation} for morphology. Must be one of 'erode', 'dilate', 'open', 'close', 'gradient', 'tophat', 'bottomhat'")
        img_out = output.to(comfy.model_management.intermediate_device()).movedim(1, -1)
        return io.NodeOutput(img_out)


class ImageRGBToYUV(io.ComfyNode):
    @classmethod
    def define_schema(cls):
        return io.Schema(
            node_id="ImageRGBToYUV",
            search_aliases=["color space conversion"],
            display_name="Image RGB to YUV",
            category="image/color",
            inputs=[
                io.Image.Input("image"),
            ],
            outputs=[
                io.Image.Output(display_name="Y"),
                io.Image.Output(display_name="U"),

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Use one of the exact accepted strings: 'erode', 'dilate', 'open', 'close', 'gradient', 'top_hat', 'bottom_hat' (underscores, despite what the message says).
  2. If driving via API, validate the operation against the node's combo options from /object_info before submitting.
  3. Patch the node to fix the misleading error message text.

Example fix

// before
{"class_type": "Morphology", "inputs": {"image": [...], "operation": "tophat", "kernel_size": 3}}
// after
{"class_type": "Morphology", "inputs": {"image": [...], "operation": "top_hat", "kernel_size": 3}}
Defensive patterns

Strategy: validation

Validate before calling

VALID_MORPH_OPS = {'erode', 'dilate', 'open', 'close', 'gradient', 'top_hat', 'bottom_hat'}
if operation not in VALID_MORPH_OPS:
    raise ValueError(f"operation must be one of {sorted(VALID_MORPH_OPS)}")

Type guard

def is_valid_morph_op(op: str) -> bool:
    return op in {'erode', 'dilate', 'open', 'close', 'gradient', 'top_hat', 'bottom_hat'}

Prevention

When it happens

Trigger: Calling the Morphology node via the HTTP API or a hand-edited workflow with an operation value outside {erode, dilate, open, close, gradient, top_hat, bottom_hat}. Copying the error message's own suggestion 'tophat' or 'bottomhat' will fail again because the code compares against 'top_hat'/'bottom_hat'.

Common situations: Scripts POSTing prompts to /prompt with a typo'd operation; workflows authored outside the UI; users following the error text verbatim and hitting the same error.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/605e697cbe5890e1. Report an issue: GitHub.