{"record":{"id":"0024acdcbc4218ab","repo":"reflex-dev/reflex","slug":"array-elements-must-be-of-type-literalvar-not-ty","errorCode":null,"errorMessage":"Array elements must be of type LiteralVar, not {type(element_var)}","messagePattern":"Array elements must be of type LiteralVar, not (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"packages/reflex-base/src/reflex_base/vars/sequence.py","lineNumber":684,"sourceCode":"            The hash of the var.\n        \"\"\"\n        return hash((self.__class__.__name__, self._js_expr))\n\n    def json(self) -> str:\n        \"\"\"Get the JSON representation of the var.\n\n        Returns:\n            The JSON representation of the var.\n\n        Raises:\n            TypeError: If the array elements are not of type LiteralVar.\n        \"\"\"\n        elements = []\n        for element in self._var_value:\n            element_var = LiteralVar.create(element)\n            if not isinstance(element_var, LiteralVar):\n                msg = f\"Array elements must be of type LiteralVar, not {type(element_var)}\"\n                raise TypeError(msg)\n            elements.append(element_var.json())\n\n        return \"[\" + \", \".join(elements) + \"]\"\n\n    @classmethod\n    def create(\n        cls,\n        value: OTHER_ARRAY_VAR_TYPE,\n        _var_type: type[OTHER_ARRAY_VAR_TYPE] | None = None,\n        _var_data: VarData | None = None,\n    ) -> LiteralArrayVar[OTHER_ARRAY_VAR_TYPE]:\n        \"\"\"Create a var from a string value.\n\n        Args:\n            value: The value to create the var from.\n            _var_type: The type of the var.\n            _var_data: Additional hooks and imports associated with the Var.\n","sourceCodeStart":666,"sourceCodeEnd":702,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-base/src/reflex_base/vars/sequence.py#L666-L702","documentation":"LiteralArrayVar.json() serializes each element of the stored list via LiteralVar.create; elements that cannot become literal vars (functions, objects without serializers, live connections) make TypeError fire because there is no JS literal for them.","triggerScenarios":"Putting non-serializable objects (callables, custom class instances, file handles) inside a list stored in a frontend state var, then rendering it or serializing state to JSON during hydration/compile.","commonSituations":"Storing lists of model instances or callables in frontend state; computed vars returning lists of arbitrary objects; forgetting to mark data-only-for-server lists as backend ('_' prefix).","solutions":["Mark the list as backend (leading underscore) if the client doesn't need it.","Serialize each element to a JSON-safe dict/str before storing (e.g. [m.model_dump() for m in models]).","Register serializers for your custom types."],"exampleFix":"# before\nclass State(rx.State):\n    callbacks: list = [str.upper]  # function not serializable\n\n# after\nclass State(rx.State):\n    _callbacks: list = [str.upper]  # backend only\n    names: list[str] = []","handlingStrategy":"validation","validationCode":"import json\n\ndef list_elements_serializable(lst) -> bool:\n    return all(json_safe(e) for e in lst) if isinstance(lst, list) else False\n\ndef json_safe(v) -> bool:\n    import json\n    try:\n        json.dumps(v); return True\n    except TypeError:\n        return False","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep frontend list state JSON-serializable (dicts, str, numbers, bool, None).","Convert model instances with .model_dump()/.dict() before storing.","Store callables/connections only in '_'-prefixed backend vars."],"tags":["reflex","literal-var","array","serialization"],"backgroundTag":"var-unserializable-to-json","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}