I am seeing a weird issue with tasks and pydantic ...
# ask-community
b
I am seeing a weird issue with tasks and pydantic models. This is my example. The exclude_unset=True is not working once when the task is called. If I use user_json.fn() it works as excepted. Does anyone know why the @task decorator would be messing with the pydantic model and if a known workaround is available?
Copy code
class User(BaseModel):
    """Pydantic model for the User."""

    email: Optional[str]
    firstName: Optional[str]
    lastName: Optional[str]


@task()
def user_json(user: User):
    """Should return a JSON representation of only the set fields in the User model."""
    print(user)
    # Outputs: email=None firstName='John' lastName=None
    print(user.json(exclude_unset=True))
    # Outputs: {"email": null, "firstName": "John", "lastName": null}
    return user.json(exclude_unset=True)


@flow()
def fl_create_user():
    """Creates a new user with only the first_name set and calls the user_json task."""
    user = User(firstName="John")
    print(user)
    # Outputs: email=None firstName='John' lastName=None
    print(user.json(exclude_unset=True))
    # Outputs: {"firstName": "John"}
    json_user = user_json(user)
    print(json_user)
    # Outputs: {"email": null, "firstName": "John", "lastName": null}
👀 1
a
Hey @Brian Newman! Thanks for flagging this. We just kicked off some Pydantic work as we transition to V2 and V1 compatibility, so this is top of mind for us. We're happy to update you here on Slack as we look at this, or if you wanted to file a Github issue that would let you subscribe to updates. Either way happy to help and wanted to let you know we're actively looking at this.
There might be a workaround, so if other community members want to chime in don't let me stop you. Just wanted to give you an update here Brian!
u
Thanks for flagging this Brian! I was in this part of the codebase today and took some time to look into what's going on. What's happening is that when we go to run a task that has a pydantic object as its arg we deconstruct the object (including its
None
s and then construct it again (explicitly setting the
None
we found during deconstruction). As you found, one of the pieces of info we lose during re-construction is the fields that were unset. I have a fix in the works that'll keep track of which fields were unset during reconstruction.
🙌 4
Tracking the fix in PR 10876