invoke-ai/InvokeAI · error · ValueError
Invalid mode selected
Error message
Invalid mode selected
What it means
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.
Source
Thrown at invokeai/app/invocations/composition-nodes.py:1418
)
radius_h: int = InputField(
ge=0, default=4, description="Height (in pixels) by which to dilate(expand) or erode (contract) the image"
)
mode: DILATE_ERODE_MODES = InputField(default="Dilate", description="How to operate on the image")
def expand_or_contract(self, image_in: Image.Image):
image_out = numpy.array(image_in)
expand_radius_w = self.radius_w
expand_radius_h = self.radius_h
expand_fn = None
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (expand_radius_w * 2 + 1, expand_radius_h * 2 + 1))
if self.mode == "Dilate":
expand_fn = cv2.dilate
elif self.mode == "Erode":
expand_fn = cv2.erode
else:
raise ValueError("Invalid mode selected")
image_out = expand_fn(image_out, kernel, iterations=1)
return Image.fromarray(image_out, mode=image_in.mode)
def invoke(self, context: InvocationContext) -> ImageOutput:
image_in = context.images.get_pil(self.image.image_name)
image_out = image_in
if self.lightness_only:
image_mode = image_in.mode
alpha_channel = None
if (image_mode == "RGBA") or (image_mode == "LA") or (image_mode == "PA"):
alpha_channel = image_in.getchannel("A")
elif (image_mode == "RGBa") or (image_mode == "La") or (image_mode == "Pa"):
alpha_channel = image_in.getchannel("a")
if (image_mode == "RGBA") or (image_mode == "RGBa"):
image_mode = "RGB"
elif (image_mode == "LA") or (image_mode == "La"):
image_mode = "L"View on GitHub (pinned to 0b6a024f2f)
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
Example fix
// before node = ExpandOrContractInvocation(image=img, mode="dilate", radius=4) // after node = ExpandOrContractInvocation(image=img, mode="Dilate", radius=4)
Defensive patterns
Strategy: validation
Validate before calling
ALLOWED_MODES = {"Dilate", "Erode"}
if node.mode not in ALLOWED_MODES:
raise ValueError(f"mode must be one of {sorted(ALLOWED_MODES)}, got {node.mode!r}") Type guard
def is_valid_mode(mode: object) -> bool:
return isinstance(mode, str) and mode in {"Dilate", "Erode"} Try / catch
try:
out = invocation.invoke(context)
except ValueError as e:
if "Invalid mode selected" in str(e):
invocation.mode = "Dilate" # safe default
out = invocation.invoke(context)
else:
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- Cannot achieve the target of num_channels={num_channels}.
- Unexpected control_input type: ${type(control_input)}
- Unexpected T2I-Adapter base model type: '${t2i_adapter_model
- 'latents' or 'noise' must be provided!
- Incompatible 'noise' and 'latents' shapes: ${latents.shape=}
AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29).
Data as JSON: /api/errors/15cd74e15ae3e43c.
Report an issue: GitHub.