reflex-dev/reflex · error · ValueError

A case tuple should have at least a match case element and a

Error message

A case tuple should have at least a match case element and a return value.

What it means

Thrown by rx.match when a case tuple has fewer than two elements. Each case must be a tuple of at least one match condition followed by a return value, e.g. (cond, value) or (cond1, cond2, value).

Source

Thrown at packages/reflex-components-core/src/reflex_components_core/core/match.py:159

        cls, cases: list[tuple]
    ) -> list[tuple[list[Var], BaseComponent | Var]]:
        """Process the individual match cases.

        Args:
            cases: The match cases.

        Returns:
            The processed match cases.

        Raises:
            ValueError: If the default case is not the last case or the tuple elements are less than 2.
        """
        match_cases: list[tuple[list[Var], BaseComponent | Var]] = []
        for case_index, case in enumerate(cases):
            # There should be at least two elements in a case tuple(a condition and return value)
            if len(case) < 2:
                msg = "A case tuple should have at least a match case element and a return value."
                raise ValueError(msg)

            *conditions, return_value = case

            conditions_vars: list[Var] = []
            for condition_index, condition in enumerate(conditions):
                if isinstance(condition, BaseComponent):
                    msg = f"Match condition {condition_index} of case {case_index} cannot be a component."
                    raise ValueError(msg)
                conditions_vars.append(cls._create_case_var_with_var_data(condition))

            return_value = (
                cls._create_case_var_with_var_data(return_value)
                if not isinstance(return_value, BaseComponent)
                else return_value
            )

            if not isinstance(return_value, (Var, BaseComponent)):
                msg = "Return value must be a var or component"

View on GitHub (pinned to 45b8ed5ab7)

Solutions

  1. Give each case at least one condition and a return value: rx.match(x, (1, "one"), (2, "two"), "default")
  2. For a single-value fallback, pass it as the final default argument, not as a one-element tuple
  3. Check that you are not passing a generator/string that enumerates into unexpected tuple shapes

Example fix

# before
rx.match(State.value, ("hello",), "default")
# after
rx.match(State.value, ("hello", "is hello"), "default")
Defensive patterns

Strategy: validation

Validate before calling

def valid_case(case):
    return isinstance(case, tuple) and len(case) >= 2

assert all(valid_case(c) for c in cases), 'each match case needs (condition..., return_value)'

Type guard

def is_valid_match_case(case: Any) -> TypeGuard[tuple[Any, ...]]:
    return isinstance(case, tuple) and len(case) >= 2

Prevention

When it happens

Trigger: Calling rx.match(cond, (value_only,), ...) or passing a 1-element tuple (or a bare non-tuple item) as a case. Only the last element of each tuple is the return value; everything before it is treated as conditions, so len(case) < 2 leaves no condition.

Common situations: Writing a case as (value,) intending it to be an equality match against the condition var, or accidentally wrapping a value in a trailing comma tuple like (value,).

Related errors


AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28). Data as JSON: /api/errors/bcbd328d0169dc13. Report an issue: GitHub.