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

[BUG] Tasks and workflows with default pydantic models cannot be launched from pyflyte. #6186

Closed
2 tasks done
Tom-Newton opened this issue Jan 23, 2025 · 3 comments
Closed
2 tasks done
Assignees
Labels
bug Something isn't working

Comments

@Tom-Newton
Copy link
Contributor

Tom-Newton commented Jan 23, 2025

Describe the bug

The to_click_option used in the pyflyte CLI cannot convert a pydantic.BaseModel default value to a click compatible default. This causes an error when trying to run any task or workflow that takes a pydantic.BaseModel from CLI arguments. @dataclass by comparison works fine.

import pydantic
from flytekit import task, workflow


class Model(pydantic.BaseModel):
    value: int


DEFAULT_MODEL = Model(value=1)


@task
def print_model(model: Model = DEFAULT_MODEL):
    print(model)


@workflow
def print_model_workflow(model: Model = DEFAULT_MODEL):
    print_model(model=model)
$ pyflyte run test_pydantic_cli.py print_model_workflow
UnserializableField: Field "" of type Model in __root__ is not serializable
$ pyflyte run test_pydantic_cli.py print_model
UnserializableField: Field "" of type Model in __root__ is not serializable

The problem is the same when running locally and remotely.

Expected behavior

I would expect the default pydantic.BaseModels to work without error like defaults of other types.

Thoughts on the solution

I can make pydantic.BaseModel work with a simple change

diff --git a/flytekit/clis/sdk_in_container/run.py b/flytekit/clis/sdk_in_container/run.py
index 7d661c3ff..3eb04e278 100644
--- a/flytekit/clis/sdk_in_container/run.py
+++ b/flytekit/clis/sdk_in_container/run.py
@@ -475,8 +475,9 @@ def to_click_option(
                 If no custom logic exists, fall back to json.dumps.
             """
             with FlyteContextManager.with_context(flyte_ctx.new_builder()):
-                encoder = JSONEncoder(python_type)
-                default_val = encoder.encode(default_val)
+                default_val = default_val.json()
         if literal_var.type.metadata:
             description_extra = f": {json.dumps(literal_var.type.metadata)}"

I guess we just need some logic here to so that both @dataclass and pydantic.BaseModel can be serialised to JSON.

Screenshots

No response

Are you sure this issue hasn't been raised already?

  • Yes

Have you read the Code of Conduct?

  • Yes
@Tom-Newton Tom-Newton added bug Something isn't working untriaged This issues has not yet been looked at by the Maintainers labels Jan 23, 2025
@Tom-Newton Tom-Newton changed the title [BUG] Tasks and workflows with default pydantic models cannot be launched from pyflyte. [BUG] Tasks and workflows with default pydantic models cannot be launched from pyflyte. Jan 23, 2025
@Future-Outlier Future-Outlier self-assigned this Jan 24, 2025
@Future-Outlier
Copy link
Member

this is fixed here:
flyteorg/flytekit#3013

please try the release.
v1.15.0b1

@Future-Outlier
Copy link
Member

thank you!

@Tom-Newton
Copy link
Contributor Author

Thanks for the fix 🙌

@eapolinario eapolinario removed the untriaged This issues has not yet been looked at by the Maintainers label Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

No branches or pull requests

3 participants