Skip to content

Commit

Permalink
fix: serialize typing classes assigned to fields
Browse files Browse the repository at this point in the history
  • Loading branch information
matthias-praxipal committed Oct 12, 2024
1 parent a6ef70f commit 79a79c3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions langfuse/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import enum
from json import JSONEncoder
from typing import Any
import typing
from uuid import UUID
from collections.abc import Sequence
from langfuse.api.core import serialize_datetime
Expand Down Expand Up @@ -107,6 +108,10 @@ def default(self, obj: Any):
if isinstance(obj, Sequence):
return [self.default(item) for item in obj]

# Check if obj is a type or a generic alias
if isinstance(obj, type) or typing.get_origin(obj) is not None:
return f"<{obj.__name__}>"

if hasattr(obj, "__slots__"):
return self.default(
{slot: getattr(obj, slot, None) for slot in obj.__slots__}
Expand Down
12 changes: 12 additions & 0 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime, date, timezone
import typing
from uuid import UUID
from enum import Enum
from dataclasses import dataclass
Expand Down Expand Up @@ -182,6 +183,17 @@ def __init__(self):
assert json.loads(serializer.encode(obj)) == {"field": "value"}


def test_slots_types():
class SlotClass:
def __init__(self):
self.something = typing.Sequence[int]

obj = SlotClass()

serializer = EventSerializer()
assert json.loads(serializer.encode(obj)) == {"something": "<Sequence>"}


def test_numpy_float32():
import numpy as np

Expand Down

0 comments on commit 79a79c3

Please sign in to comment.