From 653c06c52a89839fc28796189f6b67e699a08c1f Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 14 Sep 2026 15:38:59 +0000 Subject: [PATCH] fix(serializer): sanitize NaN/Inf nested in converted containers EventSerializer already converted non-finite floats and JS-unsafe integers for values that reached _default_inner directly, and for Pydantic model_dump() output. tuple/set/frozenset, numpy scalars and arrays, dataclasses, enum values, and LangChain to_json() returned converted containers without routing them back through default(), so nested NaN/Infinity leaked as invalid JSON tokens. Route those branches through default() the same way Pydantic dumps already do. Co-authored-by: hassieb.pakzad --- langfuse/_utils/serializer.py | 12 ++-- tests/unit/test_serializer.py | 112 +++++++++++++++++++++++++++++++--- 2 files changed, 110 insertions(+), 14 deletions(-) diff --git a/langfuse/_utils/serializer.py b/langfuse/_utils/serializer.py index 46188fce5..998915d69 100644 --- a/langfuse/_utils/serializer.py +++ b/langfuse/_utils/serializer.py @@ -71,12 +71,12 @@ def _default_inner(self, obj: Any) -> Any: # Check if numpy is available and if the object is a numpy scalar # If so, convert it to a Python scalar using the item() method if np is not None and isinstance(obj, np.generic): - return obj.item() + return self.default(obj.item()) # Check if numpy is available and if the object is a numpy array # If so, convert it to a Python list using the tolist() method if np is not None and isinstance(obj, np.ndarray): - return obj.tolist() + return self.default(obj.tolist()) if isinstance(obj, float) and math.isnan(obj): return "NaN" @@ -93,7 +93,7 @@ def _default_inner(self, obj: Any) -> Any: return str(obj) if isinstance(obj, enum.Enum): - return obj.value + return self.default(obj.value) if isinstance(obj, Queue): return type(obj).__name__ @@ -127,7 +127,7 @@ def _default_inner(self, obj: Any) -> Any: return f"<{type(obj).__name__}>" if is_dataclass(obj): - return asdict(obj) # type: ignore + return self.default(asdict(obj)) # type: ignore if isinstance(obj, BaseModel): obj.model_rebuild() @@ -144,10 +144,10 @@ def _default_inner(self, obj: Any) -> Any: # if langchain is not available, the Serializable type is NoneType if Serializable is not type(None) and isinstance(obj, Serializable): # type: ignore - return obj.to_json() + return self.default(obj.to_json()) if isinstance(obj, (tuple, set, frozenset)): - return list(obj) + return [self.default(item) for item in obj] if isinstance(obj, dict): return {self.default(k): self.default(v) for k, v in obj.items()} diff --git a/tests/unit/test_serializer.py b/tests/unit/test_serializer.py index ce5798f67..0d8b54b04 100644 --- a/tests/unit/test_serializer.py +++ b/tests/unit/test_serializer.py @@ -196,6 +196,16 @@ def test_infinity_floats(): assert serializer.encode(float("-inf")) == '"-Infinity"' +def _reject_json_constant(token): + # json.loads accepts bare NaN/Infinity by default; reject them the way + # the ingestion server's strict parser does. + raise ValueError(f"invalid JSON constant emitted: {token}") + + +def _strict_loads(encoded: str): + return json.loads(encoded, parse_constant=_reject_json_constant) + + def test_pydantic_model_with_non_finite_floats(): # Non-finite floats nested inside a pydantic model must be converted to # safe string tokens rather than emitted as bare NaN/Infinity, which are @@ -213,14 +223,7 @@ class ModelWithFloats(BaseModel): finite=1.5, ) serializer = EventSerializer() - encoded = serializer.encode(model) - - # Must be strict JSON: json.loads accepts bare NaN/Infinity by default, so - # use parse_constant to reject them the way the ingestion server does. - def _reject(token): - raise ValueError(f"invalid JSON constant emitted: {token}") - - parsed = json.loads(encoded, parse_constant=_reject) + parsed = _strict_loads(serializer.encode(model)) assert parsed == { "nan": "NaN", "inf": "Infinity", @@ -229,6 +232,99 @@ def _reject(token): } +def test_tuple_set_frozenset_with_non_finite_floats(): + serializer = EventSerializer() + + assert _strict_loads(serializer.encode((float("nan"), 1.0, float("inf")))) == [ + "NaN", + 1.0, + "Infinity", + ] + assert _strict_loads(serializer.encode({float("nan")})) == ["NaN"] + assert _strict_loads(serializer.encode(frozenset([float("-inf")]))) == ["-Infinity"] + + +def test_dataclass_with_non_finite_floats(): + @dataclass + class Point: + x: float + y: float + + serializer = EventSerializer() + parsed = _strict_loads(serializer.encode(Point(float("nan"), float("inf")))) + assert parsed == {"x": "NaN", "y": "Infinity"} + + +def test_enum_with_non_finite_float_value(): + class NonFiniteEnum(Enum): + NAN = float("nan") + INF = float("inf") + + serializer = EventSerializer() + assert _strict_loads(serializer.encode(NonFiniteEnum.NAN)) == "NaN" + assert _strict_loads(serializer.encode(NonFiniteEnum.INF)) == "Infinity" + + +def test_numpy_array_and_generic_with_non_finite_floats(monkeypatch): + # Numpy is an optional runtime dependency; fake the types EventSerializer + # checks for so the ndarray / generic branches are covered without numpy. + class FakeGeneric: + def __init__(self, value): + self._value = value + + def item(self): + return self._value + + class FakeNdarray: + def __init__(self, data): + self._data = data + + def tolist(self): + return self._data + + class FakeNP: + generic = FakeGeneric + ndarray = FakeNdarray + + from langfuse._utils import serializer as serializer_mod + + monkeypatch.setattr(serializer_mod, "np", FakeNP) + + serializer = EventSerializer() + assert _strict_loads( + serializer.encode(FakeNdarray([float("nan"), 1.0, float("inf")])) + ) == ["NaN", 1.0, "Infinity"] + assert _strict_loads( + serializer.encode(FakeNdarray([[float("nan"), 1.0], [float("-inf"), 2.0]])) + ) == [["NaN", 1.0], ["-Infinity", 2.0]] + assert _strict_loads(serializer.encode(FakeGeneric(float("nan")))) == "NaN" + assert _strict_loads(serializer.encode(FakeGeneric(float("inf")))) == "Infinity" + + +def test_langchain_serializable_to_json_with_non_finite_floats(monkeypatch): + # langchain_core.Serializable is a BaseModel, so real messages take the + # pydantic path. Patch the type EventSerializer checks so the to_json() + # branch is covered independently. + class FakeSerializable: + def to_json(self): + return {"score": float("nan"), "ok": 1.5} + + from langfuse._utils import serializer as serializer_mod + + monkeypatch.setattr(serializer_mod, "Serializable", FakeSerializable) + + parsed = _strict_loads(EventSerializer().encode(FakeSerializable())) + assert parsed == {"score": "NaN", "ok": 1.5} + + +def test_tuple_with_js_unsafe_integer(): + # The same conversion branches that leaked non-finite floats also skip + # JS-safe integer coercion unless values are routed back through default(). + unsafe = 2**53 + serializer = EventSerializer() + assert _strict_loads(serializer.encode((unsafe,))) == [str(unsafe)] + + def test_slots(): class SlotClass: __slots__ = ["field"]