Repository navigation
fix(serializer): recurse converted values through default() to prevent invalid JSON #1877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,13 +70,17 @@ def _default_inner(self, obj: Any) -> Any: | |
|
|
||
| # Check if numpy is available and if the object is a numpy scalar | ||
| # If so, convert it to a Python scalar using the item() method | ||
| # and recurse through default() so non-finite floats and unsafe | ||
| # integers are normalized | ||
| if np is not None and isinstance(obj, np.generic): | ||
| return obj.item() | ||
| return self.default(obj.item()) | ||
|
|
||
| # Check if numpy is available and if the object is a numpy array | ||
| # If so, convert it to a Python list using the tolist() method | ||
| # and recurse through default() so nested non-finite floats and | ||
| # unsafe integers are normalized | ||
| if np is not None and isinstance(obj, np.ndarray): | ||
| return obj.tolist() | ||
| return self.default(obj.tolist()) | ||
|
|
||
| if isinstance(obj, float) and math.isnan(obj): | ||
| return "NaN" | ||
|
|
@@ -93,7 +97,7 @@ def _default_inner(self, obj: Any) -> Any: | |
| return str(obj) | ||
|
|
||
| if isinstance(obj, enum.Enum): | ||
| return obj.value | ||
| return self.default(obj.value) | ||
|
|
||
| if isinstance(obj, Queue): | ||
| return type(obj).__name__ | ||
|
|
@@ -127,7 +131,9 @@ def _default_inner(self, obj: Any) -> Any: | |
| return f"<{type(obj).__name__}>" | ||
|
|
||
| if is_dataclass(obj): | ||
| return asdict(obj) # type: ignore | ||
| # Recurse the dict produced by asdict() back through default() | ||
| # so nested non-finite floats and unsafe integers are normalized | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
A dataclass field containing a mapping with tuple keys is now recursively processed before the standard encoder can apply Prompt To Fix With AIThis is a comment left during a code review.
Path: langfuse/_utils/serializer.py
Line: 135
Comment:
**Dataclass Keys Defeat Skipkeys**
A dataclass field containing a mapping with tuple keys is now recursively processed before the standard encoder can apply `skipkeys=True`. Each tuple key becomes an unhashable list while rebuilding the dictionary, so the entire mapping is replaced by the serializer's error marker instead of omitting the unsupported key.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly. |
||
| return self.default(asdict(obj)) # type: ignore | ||
|
|
||
| if isinstance(obj, BaseModel): | ||
| obj.model_rebuild() | ||
|
|
@@ -144,10 +150,10 @@ def _default_inner(self, obj: Any) -> Any: | |
|
|
||
| # if langchain is not available, the Serializable type is NoneType | ||
| if Serializable is not type(None) and isinstance(obj, Serializable): # type: ignore | ||
| return obj.to_json() | ||
| return self.default(obj.to_json()) | ||
|
|
||
| if isinstance(obj, (tuple, set, frozenset)): | ||
| return list(obj) | ||
| return [self.default(item) for item in obj] | ||
|
|
||
| if isinstance(obj, dict): | ||
| return {self.default(k): self.default(v) for k, v in obj.items()} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extended-precision scalars such as
np.longdoubleandnp.clongdoublemay return themselves from.item(). Passing that result back toself.default()repeatedly re-enters this branch without reaching the later depth guard, eventually hitting Python's recursion limit instead of producing a useful serialized value.Prompt To Fix With AI