Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 14 additions & 2 deletions langfuse/_utils/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import math
from asyncio import Queue
from collections.abc import Sequence
from dataclasses import asdict, is_dataclass
from dataclasses import fields, is_dataclass
from datetime import date, datetime
from json import JSONEncoder
from logging import getLogger
Expand Down Expand Up @@ -127,7 +127,19 @@ def _default_inner(self, obj: Any) -> Any:
return f"<{type(obj).__name__}>"

if is_dataclass(obj):
return asdict(obj) # type: ignore
obj_id = id(obj)

if obj_id in self.seen:
return type(obj).__name__

self.seen.add(obj_id)
try:
return {
field.name: self.default(getattr(obj, field.name))
Comment on lines +137 to +138

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Container cycles escape guard

When a cyclic dataclass reference passes through a tuple, set, or frozenset field, that container returns the repeated dataclass as a raw element and the enclosing finally removes it from seen before JSONEncoder encounters it, causing serialization to recurse instead of emitting the type marker and preserving the structured fields.

Knowledge Base Used: Shared models and data serialization

Prompt To Fix With AI
This is a comment left during a code review.
Path: langfuse/_utils/serializer.py
Line: 137-138

Comment:
**Container cycles escape guard**

When a cyclic dataclass reference passes through a tuple, set, or frozenset field, that container returns the repeated dataclass as a raw element and the enclosing `finally` removes it from `seen` before `JSONEncoder` encounters it, causing serialization to recurse instead of emitting the type marker and preserving the structured fields.

**Knowledge Base Used:** [Shared models and data serialization](https://app.greptile.com/personal-org-4986/-/custom-context/knowledge-base/langfuse/langfuse-python/-/docs/shared-models-and-data-serialization.md)

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

for field in fields(obj)
}
finally:
self.seen.remove(obj_id)

if isinstance(obj, BaseModel):
obj.model_rebuild()
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,23 @@ def __init__(self):
assert result == {"next": {"next": "Node"}}


def test_circular_dataclass_reference():
@dataclass
class Node:
name: str
next: "Node | None" = None

node1 = Node("first")
node2 = Node("second")
node1.next = node2
node2.next = node1

serializer = EventSerializer()
result = json.loads(serializer.encode(node1))

assert result == {"name": "first", "next": {"name": "second", "next": "Node"}}


def test_not_serializable():
class NotSerializable:
def __init__(self):
Expand Down