-
Notifications
You must be signed in to change notification settings - Fork 78
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
Fix serialization of pydantic.BaseModel
fields with pathlib.Path
dict keys
#1157
Open
brendanator
wants to merge
7
commits into
langchain-ai:main
Choose a base branch
from
brendanator:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ce68109
Fix orjson serialization of dict keys
brendanator 5e37fe0
Make it work for pydantic classes too
brendanator 97b098d
add test and clean up _serde
brendanator 586eb4f
remove dead code
brendanator 4c6545e
Merge branch 'main' into patch-1
brendanator 604affc
Merge branch 'main' into patch-1
brendanator 8432823
update tests
brendanator File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,9 +10,7 @@ | |
import pathlib | ||
import re | ||
import uuid | ||
from typing import ( | ||
Any, | ||
) | ||
from typing import Any | ||
|
||
import orjson | ||
|
||
|
@@ -33,14 +31,8 @@ def _simple_default(obj): | |
# https://github.com/ijl/orjson#serialize | ||
if isinstance(obj, datetime.datetime): | ||
return obj.isoformat() | ||
if isinstance(obj, uuid.UUID): | ||
elif isinstance(obj, uuid.UUID): | ||
return str(obj) | ||
if hasattr(obj, "model_dump") and callable(obj.model_dump): | ||
return obj.model_dump() | ||
elif hasattr(obj, "dict") and callable(obj.dict): | ||
return obj.dict() | ||
elif hasattr(obj, "_asdict") and callable(obj._asdict): | ||
return obj._asdict() | ||
elif isinstance(obj, BaseException): | ||
return {"error": type(obj).__name__, "message": str(obj)} | ||
elif isinstance(obj, (set, frozenset, collections.deque)): | ||
|
@@ -86,21 +78,22 @@ def _serialize_json(obj: Any) -> Any: | |
return list(obj) | ||
|
||
serialization_methods = [ | ||
("model_dump", True), # Pydantic V2 with non-serializable fields | ||
("dict", False), # Pydantic V1 with non-serializable field | ||
("to_dict", False), # dataclasses-json | ||
( | ||
"model_dump", | ||
{"exclude_none": True, "mode": "json"}, | ||
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. mode="json" convert |
||
), # Pydantic V2 with non-serializable fields | ||
("dict", {}), # Pydantic V1 with non-serializable field | ||
("to_dict", {}), # dataclasses-json | ||
] | ||
for attr, exclude_none in serialization_methods: | ||
for attr, kwargs in serialization_methods: | ||
if ( | ||
hasattr(obj, attr) | ||
and callable(getattr(obj, attr)) | ||
and not isinstance(obj, type) | ||
): | ||
try: | ||
method = getattr(obj, attr) | ||
response = ( | ||
method(exclude_none=exclude_none) if exclude_none else method() | ||
) | ||
response = method(**kwargs) | ||
if not isinstance(response, dict): | ||
return str(response) | ||
return response | ||
|
This file contains 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
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.
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.
removed these are they are dead code - namedtuple, and pydantic.BaseModel are handled in
_serialize_json