Repository navigation
fix(serializer): robust __slots__ serialization and cycle detection - #2015
Open
passionworkeer wants to merge 1 commit into
Open
passionworkeer wants to merge 1 commit into
passionworkeer wants to merge 1 commit into
Conversation
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__.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes two defects in
EventSerializerhandling of__slots__:Handle single-string
__slots__:Per Python Data Model §3.3.2.4,
__slots__can be a singlestrnaming an attribute (e.g.,__slots__ = "query"). Iterating directly overobj.__slots__when it is a string iterates over characters, dropping the actual attribute value. This PR normalizes string__slots__to(obj.__slots__,).Detect cycles in
__slots__objects:Aligns
__slots__with the existing__dict__branch by trackingobj_id in self.seento immediately break self-referential cycles, wrapped intry...finallyto 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 passedruff format --check langfuse/_utils/serializer.py tests/unit/test_serializer.py: 2 files already formattedmypy langfuse/_utils/serializer.py: Success: no issues found

Confidence Score: 5/5
The PR appears safe to merge.Summary
EventSerializernow treats a string__slots__declaration as one attribute and stops cycles through slotted objects.Reviews (1) · Last reviewed commit: "fix(serializer): robust __slots__ serial..." · Reviewed by Greptile