Skip to content

Commit

Permalink
Check attr dict
Browse files Browse the repository at this point in the history
  • Loading branch information
hinthornw committed Jan 28, 2024
1 parent d93f00a commit fe82dff
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 31 deletions.
64 changes: 34 additions & 30 deletions python/langsmith/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

import collections
import dataclasses
import datetime
import functools
import importlib
Expand Down Expand Up @@ -146,39 +147,42 @@ def _default_retry_config() -> Retry:


def _serialize_json(obj: Any) -> Any:
if obj is None:
return None

basic_types = (bool, int, float, str, datetime.datetime, uuid.UUID)
if isinstance(obj, basic_types):
return obj.isoformat() if isinstance(obj, datetime.datetime) else str(obj)

serialization_methods = [
("model_dump_json", True), # Base models, V2
("json", True), # Base models, V1
("to_json", False), # dataclass_json
("dict", False), # dataclass
]
if isinstance(obj, datetime.datetime):
return obj.isoformat()
if isinstance(obj, uuid.UUID):
return str(obj)
try:
serialization_methods = [
("model_dump_json", True), # Pydantic V2
("json", True), # Pydantic V1
("to_json", False), # dataclass_json
]

for attr, exclude_none in serialization_methods:
if hasattr(obj, attr) and callable(getattr(obj, attr)):
try:
method = getattr(obj, attr)
json_str = (
method(exclude_none=exclude_none) if exclude_none else method()
)
return json.loads(json_str)
except Exception as e:
logger.debug(f"Failed to serialize {type(obj)} to JSON: {e}")
return repr(obj)
for attr, exclude_none in serialization_methods:
if hasattr(obj, attr) and callable(getattr(obj, attr)):
try:
method = getattr(obj, attr)
json_str = (
method(exclude_none=exclude_none) if exclude_none else method()
)
return json.loads(json_str)
except Exception as e:
logger.debug(f"Failed to serialize {type(obj)} to JSON: {e}")
return repr(obj)

try:
if hasattr(obj, "__slots__"):
return {slot: getattr(obj, slot) for slot in obj.__slots__}
else:
return vars(obj)
if dataclasses.is_dataclass(obj):
# Regular dataclass
return dataclasses.asdict(obj)
try:
if hasattr(obj, "__slots__"):
return {slot: getattr(obj, slot) for slot in obj.__slots__}
else:
return vars(obj)
except Exception as e:
logger.debug(f"Failed to serialize {type(obj)} to JSON using vars: {e}")
return repr(obj)
except Exception as e:
logger.debug(f"Failed to serialize {type(obj)} to JSON using vars: {e}")
logger.debug(f"Failed to serialize {type(obj)} to JSON: {e}")
return repr(obj)


Expand Down
19 changes: 18 additions & 1 deletion python/tests/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
from datetime import datetime
from enum import Enum
from io import BytesIO
from typing import Optional
from typing import NamedTuple, Optional
from unittest import mock
from unittest.mock import patch

import attr
import dataclasses_json
import pytest
from pydantic import BaseModel
Expand Down Expand Up @@ -302,6 +303,9 @@ class MyDataclass:
foo: str
bar: int

def something(self) -> None:
pass

class MyEnum(str, Enum):
FOO = "foo"
BAR = "bar"
Expand All @@ -311,6 +315,11 @@ class MyEnum(str, Enum):
class Person:
name: str

@attr.dataclass
class AttrDict:
foo: str = attr.ib()
bar: int

uid = uuid.uuid4()
current_time = datetime.now()

Expand All @@ -320,6 +329,10 @@ class NestedClass:
def __init__(self) -> None:
self.person = Person(name="foo")

class MyNamedTuple(NamedTuple):
foo: str
bar: int

to_serialize = {
"uid": uid,
"time": current_time,
Expand All @@ -335,6 +348,8 @@ def __init__(self) -> None:
"an_int": 1,
"a_float": 1.1,
"nested_class": NestedClass(),
"attr_dict": AttrDict(foo="foo", bar=1),
"named_tuple": MyNamedTuple(foo="foo", bar=1),
}

res = json.loads(json.dumps(to_serialize, default=_serialize_json))
Expand All @@ -353,6 +368,8 @@ def __init__(self) -> None:
"an_int": 1,
"a_float": 1.1,
"nested_class": {"person": {"name": "foo"}},
"attr_dict": {"foo": "foo", "bar": 1},
"named_tuple": ["foo", 1],
}
assert res == expected

Expand Down

0 comments on commit fe82dff

Please sign in to comment.