{"record":{"id":"8d7906dcfe18e51a","repo":"invoke-ai/InvokeAI","slug":"cannot-divide-by-zero","errorCode":null,"errorMessage":"Cannot divide by zero","messagePattern":"Cannot divide by zero","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/app/invocations/math.py","lineNumber":192,"sourceCode":"        \"min\",\n        \"max\",\n    ],\n    category=\"math\",\n    version=\"1.0.1\",\n)\nclass IntegerMathInvocation(BaseInvocation):\n    \"\"\"Performs integer math.\"\"\"\n\n    operation: INTEGER_OPERATIONS = InputField(\n        default=\"ADD\", description=\"The operation to perform\", ui_choice_labels=INTEGER_OPERATIONS_LABELS\n    )\n    a: int = InputField(default=1, description=FieldDescriptions.num_1)\n    b: int = InputField(default=1, description=FieldDescriptions.num_2)\n\n    @field_validator(\"b\")\n    def no_unrepresentable_results(cls, v: int, info: ValidationInfo):\n        if info.data[\"operation\"] == \"DIV\" and v == 0:\n            raise ValueError(\"Cannot divide by zero\")\n        elif info.data[\"operation\"] == \"MOD\" and v == 0:\n            raise ValueError(\"Cannot divide by zero\")\n        elif info.data[\"operation\"] == \"EXP\" and v < 0:\n            raise ValueError(\"Result of exponentiation is not an integer\")\n        return v\n\n    def invoke(self, context: InvocationContext) -> IntegerOutput:\n        # Python doesn't support switch statements until 3.10, but InvokeAI supports back to 3.9\n        if self.operation == \"ADD\":\n            return IntegerOutput(value=self.a + self.b)\n        elif self.operation == \"SUB\":\n            return IntegerOutput(value=self.a - self.b)\n        elif self.operation == \"MUL\":\n            return IntegerOutput(value=self.a * self.b)\n        elif self.operation == \"DIV\":\n            return IntegerOutput(value=int(self.a / self.b))\n        elif self.operation == \"EXP\":\n            return IntegerOutput(value=self.a**self.b)","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/invocations/math.py#L174-L210","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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"],"exampleFix":"// before\nnode = IntegerMathInvocation(a=10, b=0, operation=\"DIV\")\n// after\nnode = IntegerMathInvocation(a=10, b=2, operation=\"DIV\")","handlingStrategy":"validation","validationCode":"if operation == \"DIV\" and b == 0:\n    raise ValueError(\"Cannot divide by zero: fix the b input before queueing\")","typeGuard":"def is_safe_divisor(b: int) -> bool:\n    return b != 0","tryCatchPattern":"try:\n    graph.validate()\n    session = api.queue(graph)\nexcept ValidationError as e:\n    if \"Cannot divide by zero\" in str(e):\n        node.b = 1\n        session = api.queue(graph)\n    else:\n        raise","preventionTips":["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"],"tags":["invokeai","pydantic","validation","division-by-zero"],"backgroundTag":"division-by-zero","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}