{"record":{"id":"122a3fee3a7ee820","repo":"reflex-dev/reflex","slug":"literalvar-subclasses-must-implement-the-json-meth","errorCode":null,"errorMessage":"LiteralVar subclasses must implement the json method.","messagePattern":"LiteralVar subclasses must implement the json method\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"packages/reflex-base/src/reflex_base/vars/base.py","lineNumber":1820,"sourceCode":"        if isinstance(value, range):\n            return None\n\n        msg = f\"Unsupported type {type(value)} for LiteralVar. Tried to create a LiteralVar from {value}.\"\n        raise TypeError(msg)\n\n    @property\n    def _var_value(self) -> Any:\n        msg = \"LiteralVar subclasses must implement the _var_value property.\"\n        raise NotImplementedError(msg)\n\n    def json(self) -> str:\n        \"\"\"Serialize the var to a JSON string.\n\n        Raises:\n            NotImplementedError: If the method is not implemented.\n        \"\"\"\n        msg = \"LiteralVar subclasses must implement the json method.\"\n        raise NotImplementedError(msg)\n\n\n@serializers.serializer\ndef serialize_literal(value: LiteralVar):\n    \"\"\"Serialize a Literal type.\n\n    Args:\n        value: The Literal to serialize.\n\n    Returns:\n        The serialized Literal.\n    \"\"\"\n    return value._var_value\n\n\ndef get_python_literal(value: LiteralVar | Any) -> Any | None:\n    \"\"\"Get the Python literal value.\n","sourceCodeStart":1802,"sourceCodeEnd":1838,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-base/src/reflex_base/vars/base.py#L1802-L1838","documentation":"LiteralVar's base class declares json() as an abstract-ish method: it must serialize the var to a JSON string for embedding in the frontend. A subclass that does not override json() triggers this NotImplementedError when any code path serializes the var (e.g. during component compilation or state hydration). It is an internal contract for Var subclass authors, not end-user code.","triggerScenarios":"Creating a custom LiteralVar subclass without a json() method and then compiling the app or otherwise serializing it; calling var.json() directly on a LiteralVar that lacks the override.","commonSituations":"Authoring custom Var types for serializer integrations and forgetting json(); copy-pasting an old LiteralVar subclass across Reflex major versions where the abstract surface grew; version mismatch between reflex and reflex-base packages.","solutions":["Implement def json(self) -> str in your LiteralVar subclass returning json.dumps(self._var_value)","Prefer returning a built-in literal type (str/int/bool/list/dict) from serializers so Reflex wraps it in a standard LiteralVar subclass","If no custom subclass exists in your project, run uv sync to realign workspace package versions"],"exampleFix":"# before\nclass MyLiteralVar(rx.vars.LiteralVar):\n    ...\n\n# after\nimport json\nclass MyLiteralVar(rx.vars.LiteralVar):\n    @property\n    def _var_value(self):\n        return self.value\n\n    def json(self) -> str:\n        return json.dumps(self.value)","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"from reflex.vars.base import LiteralVar\n\ndef implements_json(v: LiteralVar) -> bool:\n    return type(v).json is not LiteralVar.json","tryCatchPattern":"try:\n    s = lit.json()\nexcept NotImplementedError:\n    s = repr(lit._var_value)  # degrade gracefully; fix the subclass","preventionTips":["Always pair a _var_value property with a json() method in LiteralVar subclasses","Test serialization (json()) of every custom Var type during development"],"tags":["reflex","internal-api","subclassing","notimplementederror","json"],"backgroundTag":"abstract-method-not-implemented","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}