reflex-dev/reflex · error · MatchTypeError

Match cases should have the same return types. Case

Error message

Match cases should have the same return types. Case {index} with return value `{return_value._js_expr if isinstance(return_value, Var) else textwrap.shorten(str(return_value), width=250)}` of type {type(return_value)!r} is not {return_type}

What it means

rx.match requires all case return values (and the default) to share the same type so it can produce a well-typed var. This MatchTypeError fires when one case's return value type differs from the first case's.

Solutions

  1. Make all return values (including the default) the same type
  2. Convert mixed literals with Var.create(value, _var_type=...) or wrap strings/numbers consistently
  3. If mixing components and strings is intended, wrap strings in rx.text so all returns are components

Example fix

# before
rx.match(State.n, (1, "one"), (2, 2), "other")
# after
rx.match(State.n, (1, "one"), (2, "two"), "other")
Defensive patterns

Strategy: validation

Validate before calling

types = {type(c[-1]).__name__ if not isinstance(c[-1], Var) else str(c[-1]._var_type) for c in cases}
assert len(types) == 1, f'mixed return types: {types}'
# include the default in the check

Type guard

def same_return_type(cases: list[tuple], default: Any) -> bool:
    first = type(cases[0][-1])
    return all(type(c[-1]) is first for c in cases) and type(default) is first

Prevention

When it happens

Trigger: e.g. rx.match(State.n, (1, "one"), (2, 2), "other") — mixing str and int returns. Also mixing Var-typed returns with literal returns of a different _var_type.

Common situations: Returning a component in one case and a string in another; mixing a State var of type int with literal strings; forgetting the default value counts toward the type check.

Related errors


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

Appendix: source

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

        """
        first_case_return = match_cases[0][-1]
        return_type = type(first_case_return)

        if isinstance(first_case_return, BaseComponent):
            return_type = BaseComponent
        elif isinstance(first_case_return, Var):
            return_type = Var

        cases = []
        for index, case in enumerate(match_cases):
            conditions, return_value = case
            if not isinstance(return_value, return_type):
                msg = (
                    f"Match cases should have the same return types. Case {index} with return "
                    f"value `{return_value._js_expr if isinstance(return_value, Var) else textwrap.shorten(str(return_value), width=250)}`"
                    f" of type {type(return_value)!r} is not {return_type}"
                )
                raise MatchTypeError(msg)
            cases.append((conditions, return_value))
        return cases

    @classmethod
    def _create_match_cond_var_or_component(
        cls,
        match_cond_var: Var,
        match_cases: list[tuple[list[Var], BaseComponent]]
        | list[tuple[list[Var], Var]],
        default: Var | BaseComponent | None,
    ) -> Component | Var:
        """Create and return the match condition var or component.

        Args:
            match_cond_var: The match condition.
            match_cases: The list of match cases.
            default: The default case.

View on GitHub (pinned to 45b8ed5ab7)