Skip to content

Commit ac43d59

Browse files
authored
fix(serializer): sanitize NaN/Inf nested in converted containers (#1878)
1 parent 6c3842a commit ac43d59

2 files changed

Lines changed: 110 additions & 14 deletions

File tree

‎langfuse/_utils/serializer.py‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ def _default_inner(self, obj: Any) -> Any:
7474
# Check if numpy is available and if the object is a numpy scalar
7575
# If so, convert it to a Python scalar using the item() method
7676
if np is not None and isinstance(obj, np.generic):
77-
return obj.item()
77+
return self.default(obj.item())
7878

7979
# Check if numpy is available and if the object is a numpy array
8080
# If so, convert it to a Python list using the tolist() method
8181
if np is not None and isinstance(obj, np.ndarray):
82-
return obj.tolist()
82+
return self.default(obj.tolist())
8383

8484
if isinstance(obj, float) and math.isnan(obj):
8585
return "NaN"
@@ -96,7 +96,7 @@ def _default_inner(self, obj: Any) -> Any:
9696
return str(obj)
9797

9898
if isinstance(obj, enum.Enum):
99-
return obj.value
99+
return self.default(obj.value)
100100

101101
if isinstance(obj, Queue):
102102
return type(obj).__name__
@@ -130,7 +130,7 @@ def _default_inner(self, obj: Any) -> Any:
130130
return f"<{type(obj).__name__}>"
131131

132132
if is_dataclass(obj):
133-
return asdict(obj) # type: ignore
133+
return self.default(asdict(obj)) # type: ignore
134134

135135
if isinstance(obj, BaseModel):
136136
obj.model_rebuild()
@@ -147,10 +147,10 @@ def _default_inner(self, obj: Any) -> Any:
147147

148148
# if langchain is not available, the Serializable type is NoneType
149149
if Serializable is not type(None) and isinstance(obj, Serializable): # type: ignore
150-
return obj.to_json()
150+
return self.default(obj.to_json())
151151

152152
if isinstance(obj, (tuple, set, frozenset)):
153-
return list(obj)
153+
return [self.default(item) for item in obj]
154154

155155
if isinstance(obj, dict):
156156
return {self.default(k): self.default(v) for k, v in obj.items()}

‎tests/unit/test_serializer.py‎

Lines changed: 104 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,16 @@ def test_infinity_floats():
227227
assert serializer.encode(float("-inf")) == '"-Infinity"'
228228

229229

230+
def _reject_json_constant(token):
231+
# json.loads accepts bare NaN/Infinity by default; reject them the way
232+
# the ingestion server's strict parser does.
233+
raise ValueError(f"invalid JSON constant emitted: {token}")
234+
235+
236+
def _strict_loads(encoded: str):
237+
return json.loads(encoded, parse_constant=_reject_json_constant)
238+
239+
230240
def test_pydantic_model_with_non_finite_floats():
231241
# Non-finite floats nested inside a pydantic model must be converted to
232242
# safe string tokens rather than emitted as bare NaN/Infinity, which are
@@ -244,14 +254,7 @@ class ModelWithFloats(BaseModel):
244254
finite=1.5,
245255
)
246256
serializer = EventSerializer()
247-
encoded = serializer.encode(model)
248-
249-
# Must be strict JSON: json.loads accepts bare NaN/Infinity by default, so
250-
# use parse_constant to reject them the way the ingestion server does.
251-
def _reject(token):
252-
raise ValueError(f"invalid JSON constant emitted: {token}")
253-
254-
parsed = json.loads(encoded, parse_constant=_reject)
257+
parsed = _strict_loads(serializer.encode(model))
255258
assert parsed == {
256259
"nan": "NaN",
257260
"inf": "Infinity",
@@ -260,6 +263,99 @@ def _reject(token):
260263
}
261264

262265

266+
def test_tuple_set_frozenset_with_non_finite_floats():
267+
serializer = EventSerializer()
268+
269+
assert _strict_loads(serializer.encode((float("nan"), 1.0, float("inf")))) == [
270+
"NaN",
271+
1.0,
272+
"Infinity",
273+
]
274+
assert _strict_loads(serializer.encode({float("nan")})) == ["NaN"]
275+
assert _strict_loads(serializer.encode(frozenset([float("-inf")]))) == ["-Infinity"]
276+
277+
278+
def test_dataclass_with_non_finite_floats():
279+
@dataclass
280+
class Point:
281+
x: float
282+
y: float
283+
284+
serializer = EventSerializer()
285+
parsed = _strict_loads(serializer.encode(Point(float("nan"), float("inf"))))
286+
assert parsed == {"x": "NaN", "y": "Infinity"}
287+
288+
289+
def test_enum_with_non_finite_float_value():
290+
class NonFiniteEnum(Enum):
291+
NAN = float("nan")
292+
INF = float("inf")
293+
294+
serializer = EventSerializer()
295+
assert _strict_loads(serializer.encode(NonFiniteEnum.NAN)) == "NaN"
296+
assert _strict_loads(serializer.encode(NonFiniteEnum.INF)) == "Infinity"
297+
298+
299+
def test_numpy_array_and_generic_with_non_finite_floats(monkeypatch):
300+
# Numpy is an optional runtime dependency; fake the types EventSerializer
301+
# checks for so the ndarray / generic branches are covered without numpy.
302+
class FakeGeneric:
303+
def __init__(self, value):
304+
self._value = value
305+
306+
def item(self):
307+
return self._value
308+
309+
class FakeNdarray:
310+
def __init__(self, data):
311+
self._data = data
312+
313+
def tolist(self):
314+
return self._data
315+
316+
class FakeNP:
317+
generic = FakeGeneric
318+
ndarray = FakeNdarray
319+
320+
from langfuse._utils import serializer as serializer_mod
321+
322+
monkeypatch.setattr(serializer_mod, "np", FakeNP)
323+
324+
serializer = EventSerializer()
325+
assert _strict_loads(
326+
serializer.encode(FakeNdarray([float("nan"), 1.0, float("inf")]))
327+
) == ["NaN", 1.0, "Infinity"]
328+
assert _strict_loads(
329+
serializer.encode(FakeNdarray([[float("nan"), 1.0], [float("-inf"), 2.0]]))
330+
) == [["NaN", 1.0], ["-Infinity", 2.0]]
331+
assert _strict_loads(serializer.encode(FakeGeneric(float("nan")))) == "NaN"
332+
assert _strict_loads(serializer.encode(FakeGeneric(float("inf")))) == "Infinity"
333+
334+
335+
def test_langchain_serializable_to_json_with_non_finite_floats(monkeypatch):
336+
# langchain_core.Serializable is a BaseModel, so real messages take the
337+
# pydantic path. Patch the type EventSerializer checks so the to_json()
338+
# branch is covered independently.
339+
class FakeSerializable:
340+
def to_json(self):
341+
return {"score": float("nan"), "ok": 1.5}
342+
343+
from langfuse._utils import serializer as serializer_mod
344+
345+
monkeypatch.setattr(serializer_mod, "Serializable", FakeSerializable)
346+
347+
parsed = _strict_loads(EventSerializer().encode(FakeSerializable()))
348+
assert parsed == {"score": "NaN", "ok": 1.5}
349+
350+
351+
def test_tuple_with_js_unsafe_integer():
352+
# The same conversion branches that leaked non-finite floats also skip
353+
# JS-safe integer coercion unless values are routed back through default().
354+
unsafe = 2**53
355+
serializer = EventSerializer()
356+
assert _strict_loads(serializer.encode((unsafe,))) == [str(unsafe)]
357+
358+
263359
def test_slots():
264360
class SlotClass:
265361
__slots__ = ["field"]

0 commit comments

Comments
 (0)