File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11from datetime import date , datetime
2+ from dataclasses import is_dataclass , asdict
23from json import JSONEncoder
34from typing import Any
5+ from uuid import UUID
46
57from langfuse .api .core import serialize_datetime
68
@@ -19,6 +21,12 @@ def default(self, obj: Any):
1921 if isinstance (obj , (datetime )):
2022 # Timezone-awareness check
2123 return serialize_datetime (obj )
24+ if is_dataclass (obj ):
25+ return asdict (obj )
26+ if isinstance (obj , UUID ):
27+ return str (obj )
28+ if isinstance (obj , bytes ):
29+ return obj .decode ("utf-8" )
2230 if isinstance (obj , (date )):
2331 return obj .isoformat ()
2432 if isinstance (obj , BaseModel ):
Original file line number Diff line number Diff line change 11import builtins
2+ from dataclasses import dataclass
23import importlib
34import json
45from datetime import datetime , timezone , date
56from unittest .mock import patch
7+ import uuid
68
79import pytest
810from langchain .schema .messages import HumanMessage
@@ -97,3 +99,27 @@ def test_json_decoder_without_langchain_serializer_with_none():
9799 default = json .dumps (None )
98100 assert result == "null"
99101 assert result == default
102+
103+
104+ def test_data_class ():
105+ @dataclass
106+ class InventoryItem :
107+ """Class for keeping track of an item in inventory."""
108+
109+ name : str
110+ unit_price : float
111+ quantity_on_hand : int = 0
112+
113+ item = InventoryItem ("widget" , 3.0 , 10 )
114+
115+ result = json .dumps (item , cls = EventSerializer )
116+
117+ assert result == '{"name": "widget", "unit_price": 3.0, "quantity_on_hand": 10}'
118+
119+
120+ def test_data_uuid ():
121+ test_id = uuid .uuid4 ()
122+
123+ result = json .dumps (test_id , cls = EventSerializer )
124+
125+ assert result == f'"{ str (test_id )} "'
You can’t perform that action at this time.
0 commit comments