Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

pnadolny13
Copy link

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.

Copy link
Collaborator

@michaelnchin michaelnchin left a comment

Choose a reason for hiding this comment

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

@pnadolny13 Thanks!

@pnadolny13
Copy link
Author

pnadolny13 commented Jan 22, 2025

@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?

@michaelnchin michaelnchin added this to the Milestone Jan 2025 milestone Jan 23, 2025
@3coins
Copy link
Collaborator

3coins commented Jan 23, 2025

@pnadolny13
Thanks for submitting this fix. Would it be better to limit the default to serializing datetime objects, rather than converting everything to string, some of which cannot be deserialized to JSON?

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)}')

@pnadolny13
Copy link
Author

Would it be better to limit the default to serializing datetime objects

@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.

@michaelnchin
Copy link
Collaborator

...wouldn't 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?

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 json.dumps() fails. Converting everything to string would be one of several possible solutions here.

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 default=str to perform conversions on behalf of the user could potentially cause confusion.

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.

3 participants