invoke-ai/InvokeAI · error · ValueError
Cannot divide by zero
Error message
Cannot divide by zero
What it means
The IntegerMathInvocation's pydantic field_validator no_unrepresentable_results validates operand b before invoke. When operation == 'DIV' and b == 0, raising ValueError('Cannot divide by zero') prevents an unrepresentable ZeroDivisionError at invoke time. Pydantic surfaces this as a field validation error when the invocation node is created.
Source
Thrown at invokeai/app/invocations/math.py:192
"min",
"max",
],
category="math",
version="1.0.1",
)
class IntegerMathInvocation(BaseInvocation):
"""Performs integer math."""
operation: INTEGER_OPERATIONS = InputField(
default="ADD", description="The operation to perform", ui_choice_labels=INTEGER_OPERATIONS_LABELS
)
a: int = InputField(default=1, description=FieldDescriptions.num_1)
b: int = InputField(default=1, description=FieldDescriptions.num_2)
@field_validator("b")
def no_unrepresentable_results(cls, v: int, info: ValidationInfo):
if info.data["operation"] == "DIV" and v == 0:
raise ValueError("Cannot divide by zero")
elif info.data["operation"] == "MOD" and v == 0:
raise ValueError("Cannot divide by zero")
elif info.data["operation"] == "EXP" and v < 0:
raise ValueError("Result of exponentiation is not an integer")
return v
def invoke(self, context: InvocationContext) -> IntegerOutput:
# Python doesn't support switch statements until 3.10, but InvokeAI supports back to 3.9
if self.operation == "ADD":
return IntegerOutput(value=self.a + self.b)
elif self.operation == "SUB":
return IntegerOutput(value=self.a - self.b)
elif self.operation == "MUL":
return IntegerOutput(value=self.a * self.b)
elif self.operation == "DIV":
return IntegerOutput(value=int(self.a / self.b))
elif self.operation == "EXP":
return IntegerOutput(value=self.a**self.b)View on GitHub (pinned to 0b6a024f2f)
Solutions
- Set the b input on the IntegerMathInvocation node to a non-zero value
- If b is wired from another node, add a guard node to clamp the value before division
- Catch the pydantic ValidationError at graph-submit time and show a clear message in your client
Example fix
// before node = IntegerMathInvocation(a=10, b=0, operation="DIV") // after node = IntegerMathInvocation(a=10, b=2, operation="DIV")
Defensive patterns
Strategy: validation
Validate before calling
if operation == "DIV" and b == 0:
raise ValueError("Cannot divide by zero: fix the b input before queueing") Type guard
def is_safe_divisor(b: int) -> bool:
return b != 0 Try / catch
try:
graph.validate()
session = api.queue(graph)
except ValidationError as e:
if "Cannot divide by zero" in str(e):
node.b = 1
session = api.queue(graph)
else:
raise Prevention
- Never leave b at a value that can be 0 when operation is DIV
- Clamp upstream-wired divisors to at least 1
- Validate graphs client-side before submission
When it happens
Trigger: Creating/enqueueing an IntegerMathInvocation with operation='DIV' and b=0 (e.g. default b never changed, or a prior node output wired in as 0).
Common situations: Workflows where a division node's b input is fed by another node whose value happens to be 0 at run time; users forgetting to set the b input from its default then switching operation to DIV.
Related errors
- cfg_scale values must be finite.
- shift must be finite.
- source_url must be a string
- source_url must be an http or https URL
- unknown override field: {field_name}
AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29).
Data as JSON: /api/errors/8d7906dcfe18e51a.
Report an issue: GitHub.