Encountering Runtime.MarshalError in Zapier with Python code - Help needed

Hey everyone, I’m stuck with a weird error in Zapier when using some Python code. I’m getting a Runtime.MarshalError saying the response isn’t JSON serializable. Here’s what I’m working with:

def check_days_left(year, month, day):
    from datetime import datetime, timedelta

    target_date = datetime(int(year), int(month), int(day))
    days_left = (target_date.replace(day=1) + timedelta(days=32)).replace(day=1) - target_date

    if days_left.days < 14:
        return 'Not enough time'
    else:
        return 'Plenty of time'

result = check_days_left(input['year'], input['month'], input['day'])
output = {result}

When I run this, it complains about {'Not enough time'} or {'Plenty of time'} not being JSON serializable. If I remove the curly braces, it says ‘unicode’ object has no attribute ‘copy’. Any ideas what I’m doing wrong here? Thanks in advance!

hey there, i’ve dealt with this before. zapier wants a dictionary, not a set. try changing ur last line to:

output = {‘result’: result}

this should fix the serialization error. also, double check ur input values are correct. if ur still having trouble, lemme know and we can dig deeper!

I’ve encountered this issue in Zapier before. The problem lies in how you’re constructing the output. Zapier expects a dictionary, not a set. Modify your code like this:

result = check_days_left(input['year'], input['month'], input['day'])
output = {'status': result}

This creates a proper dictionary that Zapier can serialize. Also, ensure your input variables are correctly passed and consider adding error handling with try-except blocks. This can catch potential issues and return meaningful error messages, saving you debugging time.

Lastly, if you’re still having trouble, double-check your Zapier action configuration. Sometimes the issue can be in how the action is set up rather than in the code itself.

I’ve run into similar issues with Zapier and Python before. The problem is likely in how you’re structuring your output. Zapier expects a dictionary, not a set.

Try modifying your last two lines to:

result = check_days_left(input['year'], input['month'], input['day'])
output = {'result': result}

This creates a proper dictionary with a key ‘result’ and the value being your function’s return. Zapier should be able to handle this format without throwing the MarshalError.

Also, make sure your input variables (year, month, day) are being passed correctly. If any of them are None or improperly formatted, it could cause issues.

Lastly, consider adding some error handling. Wrap your main code in a try-except block to catch any potential issues and return a meaningful error message. This can save you a lot of debugging time in the long run.