Python: 'TypeError: Object of type datetime is not JSON serializable'
@devsingh05▲ 13 net votes
json.dumps handles only dict, list, str, int, float, bool and None. For datetimes, Decimals, UUIDs, sets or numpy types, pass a default: json.dumps(obj, default=str) for a quick fix, or a function that returns o.isoformat() for datetimes. numpy int64/float32 fail the same way; convert with .item() or .tolist(). In FastAPI, return the object and let jsonable_encoder handle it, or use a Pydantic model.