Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

fix(serializer): handle single-string __slots__ and detect cycles in slotted objects

未关闭
#2,014 0 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

维护者通常 1 天内回复

@hassiebp 已经在做这个了。

开始于 2026年10月16日。

  • #2015 来自 @passionworkeer —— 未关闭

评估

这个 Issue 还没有评估数据。

描述

bug sdk-python
Describe the bug

In langfuse._utils.serializer.EventSerializer, slotted objects are handled by iterating over obj.__slots__:

if hasattr(obj, "__slots__"):
    return {
        slot: self.default(getattr(obj, slot, None))
        for slot in obj.__slots__
    }

Two distinct defects exist in this logic:

  1. Single string __slots__ attribute loss:
    According to the Python Language Reference (Data Model §3.3.2.4: __slots__), __slots__ can be defined as a single string naming a single attribute (e.g. __slots__ = "query"). When __slots__ is a string, iterating directly over it yields individual characters (q, u, e, r, y). getattr(obj, "q", None) returns None, resulting in {"q": null, "u": null, ...} while the actual attribute query is lost.

  2. Missing circular reference detection:
    Unlike the __dict__ branch which tracks obj_id in self.seen to immediately break reference cycles, the __slots__ branch lacks self.seen protection. When slotted objects contain reference cycles, each branch expands down to _MAX_DEPTH = 20, causing redundant recursion and bloated payloads.

To Reproduce
import json
from langfuse._utils.serializer import EventSerializer

# Case 1: Single string slot
class SingleSlot:
    __slots__ = "query"
    def __init__(self):
        self.query = "SELECT 1"

res = json.loads(EventSerializer().encode(SingleSlot()))
print(res)
# Current output: {"q": null, "u": null, "e": null, "r": null, "y": null}
# Expected: {"query": "SELECT 1"}

# Case 2: Self-referential slotted node
class SlotNode:
    __slots__ = ("next",)

node = SlotNode()
node.next = node
res = json.loads(EventSerializer().encode(node))
# Current output: recurses 20 layers deep
# Expected: {"next": "SlotNode"}
Proposed Fix
  1. Normalize string __slots__ to a 1-tuple (obj.__slots__,) if isinstance(obj.__slots__, str).
  2. Add self.seen tracking with try...finally to __slots__ (matching __dict__).
主要语言
Python
星标
500
派生
367
平均合并
16 小时 18 分钟
30 天内合并 PR
51

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

langfuse/langfuse-python 的其他 Issue

查看 langfuse/langfuse-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。