{"record":{"id":"aa45f0e0e4eb5d89","repo":"reflex-dev/reflex","slug":"return-value-must-be-a-var-or-component","errorCode":null,"errorMessage":"Return value must be a var or component","messagePattern":"Return value must be a var or component","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"packages/reflex-components-core/src/reflex_components_core/core/match.py","lineNumber":178,"sourceCode":"\n            *conditions, return_value = case\n\n            conditions_vars: list[Var] = []\n            for condition_index, condition in enumerate(conditions):\n                if isinstance(condition, BaseComponent):\n                    msg = f\"Match condition {condition_index} of case {case_index} cannot be a component.\"\n                    raise ValueError(msg)\n                conditions_vars.append(cls._create_case_var_with_var_data(condition))\n\n            return_value = (\n                cls._create_case_var_with_var_data(return_value)\n                if not isinstance(return_value, BaseComponent)\n                else return_value\n            )\n\n            if not isinstance(return_value, (Var, BaseComponent)):\n                msg = \"Return value must be a var or component\"\n                raise ValueError(msg)\n\n            match_cases.append((conditions_vars, return_value))\n\n        return match_cases\n\n    @classmethod\n    def _validate_return_types(\n        cls, match_cases: list[tuple[list[Var], BaseComponent | Var]]\n    ) -> list[tuple[list[Var], Var]] | list[tuple[list[Var], BaseComponent]]:\n        \"\"\"Validate that match cases have the same return types.\n\n        Args:\n            match_cases: The match cases.\n\n        Returns:\n            The validated match cases.\n\n        Raises:","sourceCodeStart":160,"sourceCodeEnd":196,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-components-core/src/reflex_components_core/core/match.py#L160-L196","documentation":"The return value of each rx.match case (the last tuple element) must be a Var or a BaseComponent after conversion. Values that cannot be converted to vars (or are None-like) trigger this.","triggerScenarios":"Passing a non-convertible object as the last tuple element, such as a plain unconvertible class instance, a dict-like that Var.create rejects, or None where a var/component is expected.","commonSituations":"Returning arbitrary Python objects (e.g. custom class instances or functions) from match cases instead of strings, numbers, vars, or components.","solutions":["Return primitives (str/int/float/bool), Var instances, or components from every case","Wrap dynamic values with Var.create(...) or reference state vars like State.value","Keep all return values the same type so downstream type validation also passes"],"exampleFix":"# before\nrx.match(State.n, (1, SomeCustomObject()), \"none\")\n# after\nrx.match(State.n, (1, \"one\"), \"none\")","handlingStrategy":"type-guard","validationCode":"from reflex.vars import Var\nfrom reflex.components import Component\n\nrv = case[-1]\nassert isinstance(rv, (Var, Component)) or Var.create(rv) is not None","typeGuard":"def is_valid_return_value(v: Any) -> TypeGuard[Var | Component]:\n    from reflex.vars import Var\n    from reflex.components import Component\n    return isinstance(v, (Var, Component)) or _is_primitive(v)","tryCatchPattern":null,"preventionTips":["Return primitives, state vars, or components from match cases","Never return arbitrary class instances or None"],"tags":["reflex","match","var-conversion"],"backgroundTag":"invalid-argument-shape","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}