How to make a Prefect flow where a keyword argument is not JSON serializable

I would like to pass a Callable (i.e. a function) or even a Task as a keyword argument to a Task. This can’t be done by default because it is not JSON serializable, and Pydantic throws an error when creating the model parameters.

Are there any ways I can force Prefect to use a different serialization method? I see some documentation about a pickle serializer, but that only applies to the results and not the parameter creation.

from prefect import task, flow

def add(x, y):
    return x + y

def workflow(f=add):
    return f(1, 2)

The above example runs if add is a positional argument — just not as a keyword argument!

I’m not sure if @flow or @task can receive a Callable, if you are calling add(), don’t use it with @task.

Here is a working version of your code

def add(x, y):
    return x + y

def test_add(f=add):
    return f(1, 2)

def workflow():
    data = test_add()

if __name__ == "__main__":
    my_table_deploy = workflow.to_deployment(name="callable-function")