-
Notifications
You must be signed in to change notification settings - Fork 116
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: TypeError: Object of type datetime is not JSON serializable
#335
base: main
Are you sure you want to change the base?
Conversation
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.
@pnadolny13 Thanks!
@michaelnchin No problem! Happy to help 😄. I dont have merge access, are you able to merge this for me or what is the usual process? |
@pnadolny13 import datetime
import json
def default(o):
if isinstance(o, (datetime.date, datetime.datetime)):
return o.isoformat()
raise TypeError(f'Cannot serialize object of {type(obj)}') |
@3coins I'm with that since it still solves the issue I'm seeing! The downside is that users dont have control over this serialization so they may continue to run into these edge cases. I think their options would then be to either open another PR here to handle their case or first serialize then deserialize their messages to ensure theyre safe before passing them into this library to avoid exceptions. If thats the recommended approach then wouldnt it be better to not accept a list of dicts but instead push the serialization up to the user by accepting a list of message strings? To be fair I'm not very familiar with this code though so this is a bit off the cuff 😄 . I'd defer to you. |
IMO, this would unnecessarily restrict the set of options for dealing with serialization issues. We should let the user decide how their input is modified to compensate when Also, note that brute force converting everything to string is risky, as the input may be modified in a way that the model misinterprets it (relative to what the user actually wants). In this respect, using |
This defaults to string when serializing the input body. In particular it was having issues serializing datetime objects. They now get serialized to a string appropriately instead of causing an exception to be raised.