reflex-dev/reflex · error · PrimitiveUnserializableToJSONError
No valid JSON representation for {self}
Error message
No valid JSON representation for {self} What it means
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.
Source
Thrown at packages/reflex-base/src/reflex_base/vars/number.py:964
class LiteralNumberVar(LiteralVar[NUMBER_T], NumberVar[NUMBER_T]):
"""Base class for immutable literal number vars."""
_var_value: float | int | decimal.Decimal = dataclasses.field(default=0)
def json(self) -> str:
"""Get the JSON representation of the var.
Returns:
The JSON representation of the var.
Raises:
PrimitiveUnserializableToJSONError: If the var is unserializable to JSON.
"""
if isinstance(self._var_value, decimal.Decimal):
return json.dumps(float(self._var_value))
if math.isinf(self._var_value) or math.isnan(self._var_value):
msg = f"No valid JSON representation for {self}"
raise PrimitiveUnserializableToJSONError(msg)
return json.dumps(self._var_value)
def __hash__(self) -> int:
"""Calculate the hash value of the object.
Returns:
int: The hash value of the object.
"""
return hash((type(self).__name__, self._var_value))
@classmethod
def _get_all_var_data_without_creating_var(
cls, value: float | int | decimal.Decimal
) -> VarData | None:
"""Get all the var data without creating the var.
Args:
value: The value of the var.View on GitHub (pinned to 45b8ed5ab7)
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.
Example fix
# before
class State(rx.State):
ratio: float = float("nan")
# after
class State(rx.State):
ratio: float = 0.0
@rx.var
def ratio_display(self) -> str:
return "n/a" if math.isnan(self.ratio) else str(self.ratio) Defensive patterns
Strategy: validation
Validate before calling
import math
def json_safe_number(v: float) -> bool:
return math.isfinite(v) Type guard
import math
def is_finite_number(v) -> bool:
return isinstance(v, (int, float)) and math.isfinite(v) Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The component `{child_name}` can only be a child of the comp
- LiteralVar subclasses must implement the json method.
- Cannot serialize color that contains non-literal vars.
- Color is not a valid color.
- The keys and values of the object must be literal vars to ge
AI-assisted analysis of reflex-dev/reflex@45b8ed5ab7 (2026-08-28).
Data as JSON: /api/errors/be95e34a9c564a4e.
Report an issue: GitHub.