Skip to content

fix(serializer): robust __slots__ serialization and cycle detection - #2015

Open
passionworkeer wants to merge 1 commit into
langfuse:mainfrom
passionworkeer:fix/serializer-slots-robustness
Open

passionworkeer wants to merge 1 commit into
langfuse:mainfrom
passionworkeer:fix/serializer-slots-robustness

Conversation

@passionworkeer

@passionworkeer passionworkeer commented Oct 16, 2026 •

Copy link
Copy Markdown

What does this PR do?

Fixes two defects in EventSerializer handling of __slots__:

  1. Handle single-string __slots__:
    Per Python Data Model §3.3.2.4, __slots__ can be a single str naming an attribute (e.g., __slots__ = "query"). Iterating directly over obj.__slots__ when it is a string iterates over characters, dropping the actual attribute value. This PR normalizes string __slots__ to (obj.__slots__,).

  2. Detect cycles in __slots__ objects:
    Aligns __slots__ with the existing __dict__ branch by tracking obj_id in self.seen to immediately break self-referential cycles, wrapped in try...finally to ensure clean exit.

Closes #2014.

Verification

Ran unit tests and static quality checks:

  • pytest tests/unit/test_serializer.py: 41 passed (39 existing + 2 new regression tests)
  • ruff check langfuse/_utils/serializer.py tests/unit/test_serializer.py: All checks passed
  • ruff format --check langfuse/_utils/serializer.py tests/unit/test_serializer.py: 2 files already formatted
  • mypy langfuse/_utils/serializer.py: Success: no issues found

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge.

Summary

EventSerializer now treats a string __slots__ declaration as one attribute and stops cycles through slotted objects.

  • Objects with one string slot name now serialize that field correctly.
  • Self-referential slot objects now serialize their repeated link as a type name.

Reviews (1) · Last reviewed commit: "fix(serializer): robust __slots__ serial..." · Reviewed by Greptile

According to Python Language Reference (Data Model §3.3.2.4), __slots__
can be defined as a single string naming a single attribute. In that
case, iterating directly over __slots__ iterates over individual
characters, causing the actual attribute to be dropped and replaced
with null keys. Normalize string __slots__ to a single-element tuple.

Additionally, add self.seen cycle detection (with try/finally cleanup)
to the __slots__ branch matching the existing __dict__ behavior,
preventing redundant deep recursion on self-referential slotted objects.

Add unit tests for both single-string __slots__ and circular __slots__.
Copilot AI balanced review requested due to automatic review settings October 16, 2026 08:26

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants