{"record":{"id":"4616fb530040d4da","repo":"Comfy-Org/ComfyUI","slug":"math-expression-expression-must-evaluate-to-a","errorCode":null,"errorMessage":"Math Expression '{expression}' must evaluate to a numeric result, got {type(result).__name__}: {result!r}","messagePattern":"Math Expression '(.+?)' must evaluate to a numeric result, got (.+?): (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"comfy_extras/nodes_math.py","lineNumber":101,"sourceCode":"                io.Int.Output(display_name=\"INT\"),\n                io.Boolean.Output(display_name=\"BOOL\"),\n            ],\n        )\n\n    @classmethod\n    def execute(\n        cls, expression: str, values: io.Autogrow.Type\n    ) -> io.NodeOutput:\n        if not expression.strip():\n            raise ValueError(\"Expression cannot be empty.\")\n\n        context: dict = dict(values)\n        context[\"values\"] = list(values.values())\n\n        result = simple_eval(expression, names=context, functions=MATH_FUNCTIONS)\n        # bool check must come first because bool is a subclass of int in Python\n        if not isinstance(result, (int, float)):\n            raise ValueError(\n                f\"Math Expression '{expression}' must evaluate to a numeric result, \"\n                f\"got {type(result).__name__}: {result!r}\"\n            )\n        try:\n            float_result = float(result)\n        except OverflowError:\n            raise ValueError(\n                f\"Math Expression '{expression}' produced a result too large to \"\n                f\"represent as a float: {result}\"\n            ) from None\n        if not math.isfinite(float_result):\n            raise ValueError(\n                f\"Math Expression '{expression}' produced a non-finite result: {result}\"\n            )\n        return io.NodeOutput(float_result, int(result), bool(result))\n\n\nclass MathExtension(ComfyExtension):","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/Comfy-Org/ComfyUI/blob/1c6d8d45b3693bfbb32385b410d813a7fd6be216/comfy_extras/nodes_math.py#L83-L119","documentation":"Raised by the Math Expression node when simple_eval returns a non-numeric result: the node outputs FLOAT/INT/BOOL, so anything else (a string, list, or dict produced by e.g. '\"abc\"', '[1,2]', or string concatenation) is rejected. bool is intentionally allowed (checked first since bool subclasses int). The offending value and its type are embedded in the message.","triggerScenarios":"Expression evaluating to a string: '\"hello\" + name' or str-type variables; comparison chains are fine but list literals like '[values[0], 1]' evaluate to a list; functions returning None.","commonSituations":"Users experimenting with string values in a math node; referencing an Autogrow input whose upstream node emitted STRING; forgetting that values[i] indexes into the input list.","solutions":["Make the final expression a numeric one: wrap with float(...) is not available on strings — instead select a numeric variable or use len()/int() style operations.","Ensure every named input feeding the math node is FLOAT/INT/BOOL, not STRING or LIST.","Use values[0], values[1] ... to reference positional Autogrow inputs numerically."],"exampleFix":"// before\nexpression = '\"result: \" + name'   // string result -> raises\n\n// after\nexpression = 'values[0] * 1.5'      // numeric result from FLOAT inputs","handlingStrategy":"type-guard","validationCode":"from simpleeval import simple_eval\nresult = simple_eval(expression, names=dict(values), functions=MATH_FUNCTIONS)\nif not isinstance(result, (int, float)):\n    raise ValueError(f\"Expression yields {type(result).__name__}; make it evaluate to a number\")","typeGuard":"def is_numeric_expression_result(expression: str, values: dict) -> bool:\n    from simpleeval import simple_eval\n    try:\n        return isinstance(simple_eval(expression, names=values), (int, float))\n    except Exception:\n        return False","tryCatchPattern":"try:\n    result = simple_eval(expression, names=context, functions=MATH_FUNCTIONS)\n    if not isinstance(result, (int, float)):\n        raise ValueError(\"non-numeric result\")\nexcept ValueError:\n    expression = \"values[0]\"  # safe numeric fallback for the widget\n    result = simple_eval(expression, names=context, functions=MATH_FUNCTIONS)","preventionTips":["Only wire FLOAT/INT/BOOL outputs into math expression inputs.","End expressions with an arithmetic operation, not a string or list literal.","Use values[i] to reference positional inputs numerically."],"tags":["math","expression","type-error","validation"],"backgroundTag":null,"analyzedSha":"1c6d8d45b3693bfbb32385b410d813a7fd6be216","analyzedAt":"2026-08-14T19:37:18.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}