Comfy-Org/ComfyUI · error · TypeError

Cannot convert {type(data)} to RangeInput

Error message

Cannot convert {type(data)} to RangeInput

What it means

RangeInput.from_raw is the normalization constructor for the levels-adjustment range type used by the new input API: it accepts an already-built RangeInput or a dict with min/max/midpoint keys. Any other Python type (str, list, tuple, number) cannot be interpreted and raises TypeError with the offending type.

Source

Thrown at comfy_api/latest/_input/range_types.py:40

    So midpoint=0.5 → gamma=1.0 (linear).
    """

    def __init__(self, min_val: float, max_val: float, midpoint: float | None = None):
        self.min_val = min_val
        self.max_val = max_val
        self.midpoint = midpoint

    @staticmethod
    def from_raw(data) -> RangeInput:
        if isinstance(data, RangeInput):
            return data
        if isinstance(data, dict):
            return RangeInput(
                min_val=float(data.get("min", 0.0)),
                max_val=float(data.get("max", 1.0)),
                midpoint=float(data["midpoint"]) if data.get("midpoint") is not None else None,
            )
        raise TypeError(f"Cannot convert {type(data)} to RangeInput")

    def to_lut(self, size: int = 256) -> np.ndarray:
        """Generate a float64 lookup table mapping [0, 1] input through this
        levels adjustment.

        The LUT maps normalized input values (0..1) to output values (0..1),
        matching the GIMP levels formula.
        """
        xs = np.linspace(0.0, 1.0, size, dtype=np.float64)

        in_range = self.max_val - self.min_val
        if abs(in_range) < 1e-10:
            return np.where(xs >= self.min_val, 1.0, 0.0).astype(np.float64)

        # Normalize: map [min, max] → [0, 1]
        result = (xs - self.min_val) / in_range
        result = np.clip(result, 0.0, 1.0)

View on GitHub (pinned to 1c6d8d45b3)

Solutions

  1. Pass a dict: RangeInput.from_raw({'min': 0.0, 'max': 1.0, 'midpoint': 0.5})
  2. If the data may be a JSON string, json.loads it before from_raw
  3. Validate the payload shape at the API boundary before conversion

Example fix

// before
r = RangeInput.from_raw('{"min": 0, "max": 1}')  # str -> TypeError

# after
import json
r = RangeInput.from_raw(json.loads(data) if isinstance(data, str) else data)
Defensive patterns

Strategy: type-guard

Validate before calling

import json
if isinstance(data, (str, bytes)):
    data = json.loads(data)
if not isinstance(data, (dict, RangeInput)):
    raise TypeError(f'expected dict or RangeInput, got {type(data)}')

Type guard

def is_range_raw(d) -> bool:
    return isinstance(d, (dict, RangeInput))

Prevention

When it happens

Trigger: Calling RangeInput.from_raw(data) where data is neither RangeInput nor dict — e.g. a JSON string that was never json.loads'd, a list [min, max], or a bare float.

Common situations: Frontend sends the range as a JSON-encoded string in request payload; deserialization code assumes list format; passing raw request body fields straight through without schema validation.

Related errors


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