{"record":{"id":"be95e34a9c564a4e","repo":"reflex-dev/reflex","slug":"no-valid-json-representation-for-self","errorCode":null,"errorMessage":"No valid JSON representation for {self}","messagePattern":"No valid JSON representation for (.+?)","errorType":"exception","errorClass":"PrimitiveUnserializableToJSONError","httpStatus":null,"severity":"error","filePath":"packages/reflex-base/src/reflex_base/vars/number.py","lineNumber":964,"sourceCode":"class LiteralNumberVar(LiteralVar[NUMBER_T], NumberVar[NUMBER_T]):\n    \"\"\"Base class for immutable literal number vars.\"\"\"\n\n    _var_value: float | int | decimal.Decimal = dataclasses.field(default=0)\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            PrimitiveUnserializableToJSONError: If the var is unserializable to JSON.\n        \"\"\"\n        if isinstance(self._var_value, decimal.Decimal):\n            return json.dumps(float(self._var_value))\n        if math.isinf(self._var_value) or math.isnan(self._var_value):\n            msg = f\"No valid JSON representation for {self}\"\n            raise PrimitiveUnserializableToJSONError(msg)\n        return json.dumps(self._var_value)\n\n    def __hash__(self) -> int:\n        \"\"\"Calculate the hash value of the object.\n\n        Returns:\n            int: The hash value of the object.\n        \"\"\"\n        return hash((type(self).__name__, self._var_value))\n\n    @classmethod\n    def _get_all_var_data_without_creating_var(\n        cls, value: float | int | decimal.Decimal\n    ) -> VarData | None:\n        \"\"\"Get all the var data without creating the var.\n\n        Args:\n            value: The value of the var.","sourceCodeStart":946,"sourceCodeEnd":982,"githubUrl":"https://github.com/reflex-dev/reflex/blob/45b8ed5ab735f8a56bbb09a42384f030eb0208e7/packages/reflex-base/src/reflex_base/vars/number.py#L946-L982","documentation":"NumberVar.json() serializes the underlying Python number to a JSON literal. JSON has no representation for infinity or NaN, so if the var's value is math.inf or math.nan, PrimitiveUnserializableToJSONError is raised instead of emitting invalid JSON.","triggerScenarios":"A literal number var created from float('inf'), float('nan'), or a computation producing those values (division results, parsing bad input), then rendered/serialized during compile or state hydration.","commonSituations":"Default state values using inf/nan sentinels; math operations that can overflow to inf; data ingested from APIs containing NaN before validation; pandas/numpy floats that are NaN passed into state.","solutions":["Replace inf/nan sentinels with finite defaults (e.g. None or 0) and handle absence in logic.","Coerce/validate values before storing them in state (if math.isfinite(v): ...).","If NaN must be represented, serialize as a string or null via a computed var."],"exampleFix":"# before\nclass State(rx.State):\n    ratio: float = float(\"nan\")\n\n# after\nclass State(rx.State):\n    ratio: float = 0.0\n    @rx.var\n    def ratio_display(self) -> str:\n        return \"n/a\" if math.isnan(self.ratio) else str(self.ratio)","handlingStrategy":"validation","validationCode":"import math\n\ndef json_safe_number(v: float) -> bool:\n    return math.isfinite(v)","typeGuard":"import math\n\ndef is_finite_number(v) -> bool:\n    return isinstance(v, (int, float)) and math.isfinite(v)","tryCatchPattern":null,"preventionTips":["Validate incoming numeric data with math.isfinite before storing in frontend state.","Avoid inf/nan sentinel defaults in frontend vars.","Keep unvalidated numeric data in backend vars."],"tags":["reflex","json","nan","infinity","serialization"],"backgroundTag":"var-unserializable-to-json","analyzedSha":"45b8ed5ab735f8a56bbb09a42384f030eb0208e7","analyzedAt":"2026-08-28T19:25:27.644Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}