Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions langfuse/_utils/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ def default(self, obj: Any):
if np is not None and isinstance(obj, np.generic):
return 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()

if isinstance(obj, float) and math.isnan(obj):
return None

Expand Down
26 changes: 26 additions & 0 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,29 @@ def test_numpy_float32():
serializer = EventSerializer()

assert serializer.encode(data) == "1.0"


def test_numpy_arrays():
import numpy as np

serializer = EventSerializer()

# Test 1D array
arr_1d = np.array([1, 2, 3])
assert json.loads(serializer.encode(arr_1d)) == [1, 2, 3]

# Test 2D array
arr_2d = np.array([[1, 2], [3, 4]])
assert json.loads(serializer.encode(arr_2d)) == [[1, 2], [3, 4]]

# Test float array
arr_float = np.array([1.1, 2.2, 3.3])
assert json.loads(serializer.encode(arr_float)) == [1.1, 2.2, 3.3]

# Test empty array
arr_empty = np.array([])
assert json.loads(serializer.encode(arr_empty)) == []

# Test mixed types that numpy can handle
arr_mixed = np.array([1, 2.5, 3])
assert json.loads(serializer.encode(arr_mixed)) == [1.0, 2.5, 3.0]