Comfy-Org/ComfyUI · error · ValueError

Cannot convert string to number: {value!r}

Error message

Cannot convert string to number: {value!r}

What it means

Raised by the number-conversion node when float(text) raises ValueError, i.e. the stripped string is non-empty but not parseable as a float. The from None suppresses the chained float() traceback so only the offending value is reported via {value!r}.

Source

Thrown at comfy_extras/nodes_number_convert.py:60

    @classmethod
    def execute(cls, value) -> io.NodeOutput:
        if isinstance(value, bool):
            float_val = 1.0 if value else 0.0
            int_val = 1 if value else 0
        elif isinstance(value, int):
            float_val = float(value)
            int_val = value
        elif isinstance(value, float):
            float_val = value
            int_val = int(value)
        elif isinstance(value, str):
            text = value.strip()
            if not text:
                raise ValueError("Cannot convert empty string to number.")
            try:
                float_val = float(text)
            except ValueError:
                raise ValueError(
                    f"Cannot convert string to number: {value!r}"
                ) from None
            if not math.isfinite(float_val):
                raise ValueError(
                    f"Cannot convert non-finite value to number: {float_val}"
                )
            try:
                int_val = int(text)
            except ValueError:
                int_val = int(float_val)
        else:
            raise TypeError(
                f"Unsupported input type: {type(value).__name__}"
            )

        if not math.isfinite(float_val):
            raise ValueError(
                f"Cannot convert non-finite value to number: {float_val}"

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Send a plain decimal string: '3.14', '-2', '1e5'.
  2. Strip units/symbols/commas upstream ('1,000' -> '1000', '512px' -> '512').
  3. For values that may legitimately be NaN/Inf, resolve them upstream — this node also rejects non-finite results (see error 944).

Example fix

// before
NumberConvert(value="1,024px")
// after
NumberConvert(value="1024")
Defensive patterns

Strategy: validation

Validate before calling

def to_float_or_none(text: str):
    try:
        return float(text.strip().replace(',', ''))
    except ValueError:
        return None

val = to_float_or_none(raw)
if val is None:
    raise ValueError(f"upstream produced non-numeric text: {raw!r}")

Prevention

When it happens

Trigger: Strings like 'abc', '1.2.3', 'NaNabc', '$5', or '1,000' (comma thousands separator) — anything Python's float() rejects. Triggered only in the isinstance(value, str) branch.

Common situations: Currency or comma-formatted numbers pasted from spreadsheets; text with units like '512px'; LLM- or template-generated strings that include prose; locale-formatted decimals.

Related errors


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