Comfy-Org/ComfyUI · error · ValueError

Math Expression '{expression}' must evaluate to a numeric re

Error message

Math Expression '{expression}' must evaluate to a numeric result, got {type(result).__name__}: {result!r}

What it means

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.

Source

Thrown at comfy_extras/nodes_math.py:101

                io.Int.Output(display_name="INT"),
                io.Boolean.Output(display_name="BOOL"),
            ],
        )

    @classmethod
    def execute(
        cls, expression: str, values: io.Autogrow.Type
    ) -> io.NodeOutput:
        if not expression.strip():
            raise ValueError("Expression cannot be empty.")

        context: dict = dict(values)
        context["values"] = list(values.values())

        result = simple_eval(expression, names=context, functions=MATH_FUNCTIONS)
        # bool check must come first because bool is a subclass of int in Python
        if not isinstance(result, (int, float)):
            raise ValueError(
                f"Math Expression '{expression}' must evaluate to a numeric result, "
                f"got {type(result).__name__}: {result!r}"
            )
        try:
            float_result = float(result)
        except OverflowError:
            raise ValueError(
                f"Math Expression '{expression}' produced a result too large to "
                f"represent as a float: {result}"
            ) from None
        if not math.isfinite(float_result):
            raise ValueError(
                f"Math Expression '{expression}' produced a non-finite result: {result}"
            )
        return io.NodeOutput(float_result, int(result), bool(result))


class MathExtension(ComfyExtension):

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. 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.
  2. Ensure every named input feeding the math node is FLOAT/INT/BOOL, not STRING or LIST.
  3. Use values[0], values[1] ... to reference positional Autogrow inputs numerically.

Example fix

// before
expression = '"result: " + name'   // string result -> raises

// after
expression = 'values[0] * 1.5'      // numeric result from FLOAT inputs
Defensive patterns

Strategy: type-guard

Validate before calling

from simpleeval import simple_eval
result = simple_eval(expression, names=dict(values), functions=MATH_FUNCTIONS)
if not isinstance(result, (int, float)):
    raise ValueError(f"Expression yields {type(result).__name__}; make it evaluate to a number")

Type guard

def is_numeric_expression_result(expression: str, values: dict) -> bool:
    from simpleeval import simple_eval
    try:
        return isinstance(simple_eval(expression, names=values), (int, float))
    except Exception:
        return False

Try / catch

try:
    result = simple_eval(expression, names=context, functions=MATH_FUNCTIONS)
    if not isinstance(result, (int, float)):
        raise ValueError("non-numeric result")
except ValueError:
    expression = "values[0]"  # safe numeric fallback for the widget
    result = simple_eval(expression, names=context, functions=MATH_FUNCTIONS)

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of Comfy-Org/ComfyUI@1c6d8d45b3 (2026-08-14). Data as JSON: /api/errors/4616fb530040d4da. Report an issue: GitHub.