{"record":{"id":"15cd74e15ae3e43c","repo":"invoke-ai/InvokeAI","slug":"invalid-mode-selected","errorCode":null,"errorMessage":"Invalid mode selected","messagePattern":"Invalid mode selected","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/composition-nodes.py","lineNumber":1418,"sourceCode":"    )\n    radius_h: int = InputField(\n        ge=0, default=4, description=\"Height (in pixels) by which to dilate(expand) or erode (contract) the image\"\n    )\n    mode: DILATE_ERODE_MODES = InputField(default=\"Dilate\", description=\"How to operate on the image\")\n\n    def expand_or_contract(self, image_in: Image.Image):\n        image_out = numpy.array(image_in)\n        expand_radius_w = self.radius_w\n        expand_radius_h = self.radius_h\n\n        expand_fn = None\n        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (expand_radius_w * 2 + 1, expand_radius_h * 2 + 1))\n        if self.mode == \"Dilate\":\n            expand_fn = cv2.dilate\n        elif self.mode == \"Erode\":\n            expand_fn = cv2.erode\n        else:\n            raise ValueError(\"Invalid mode selected\")\n        image_out = expand_fn(image_out, kernel, iterations=1)\n        return Image.fromarray(image_out, mode=image_in.mode)\n\n    def invoke(self, context: InvocationContext) -> ImageOutput:\n        image_in = context.images.get_pil(self.image.image_name)\n        image_out = image_in\n\n        if self.lightness_only:\n            image_mode = image_in.mode\n            alpha_channel = None\n            if (image_mode == \"RGBA\") or (image_mode == \"LA\") or (image_mode == \"PA\"):\n                alpha_channel = image_in.getchannel(\"A\")\n            elif (image_mode == \"RGBa\") or (image_mode == \"La\") or (image_mode == \"Pa\"):\n                alpha_channel = image_in.getchannel(\"a\")\n            if (image_mode == \"RGBA\") or (image_mode == \"RGBa\"):\n                image_mode = \"RGB\"\n            elif (image_mode == \"LA\") or (image_mode == \"La\"):\n                image_mode = \"L\"","sourceCodeStart":1400,"sourceCodeEnd":1436,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/composition-nodes.py#L1400-L1436","documentation":"The ExpandOrContract invocation applies a morphological dilate or erode operation to an image via OpenCV. The `mode` field only accepts the exact strings \"Dilate\" or \"Erode\"; anything else raises this ValueError before the chosen cv2 function is applied.","triggerScenarios":"Calling expand_or_contract (or invoking this node in a graph) with `mode` set to a value other than \"Dilate\" or \"Erode\", e.g. lowercase \"dilate\", \"expand\", \"contract\", or a translated/typo'd mode string.","commonSituations":"Workflow JSON was hand-edited or exported from an older InvokeAI version that used different mode labels; a UI/DropdownField got out of sync with the node's accepted literals; scripted graph construction passed an arbitrary string.","solutions":["Set `mode` to exactly \"Dilate\" or \"Erode\" (case-sensitive)","Re-export or regenerate the workflow from the UI so the dropdown value matches current node schema","If scripting, add validation that asserts mode in {\"Dilate\",\"Erode\"} before invoke"],"exampleFix":"// before\nnode = ExpandOrContractInvocation(image=img, mode=\"dilate\", radius=4)\n// after\nnode = ExpandOrContractInvocation(image=img, mode=\"Dilate\", radius=4)","handlingStrategy":"validation","validationCode":"ALLOWED_MODES = {\"Dilate\", \"Erode\"}\nif node.mode not in ALLOWED_MODES:\n    raise ValueError(f\"mode must be one of {sorted(ALLOWED_MODES)}, got {node.mode!r}\")","typeGuard":"def is_valid_mode(mode: object) -> bool:\n    return isinstance(mode, str) and mode in {\"Dilate\", \"Erode\"}","tryCatchPattern":"try:\n    out = invocation.invoke(context)\nexcept ValueError as e:\n    if \"Invalid mode selected\" in str(e):\n        invocation.mode = \"Dilate\"  # safe default\n        out = invocation.invoke(context)\n    else:\n        raise","preventionTips":["Use the node's dropdown field type instead of free-text strings","Compare against exact case-sensitive literals","Add a schema validator on workflow JSON before submitting"],"tags":["python","valueerror","image-processing","invalid-enum"],"backgroundTag":"invalid-enum-value","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}