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

Fix pydantic basemodel default input #3013

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Future-Outlier
Copy link
Member

@Future-Outlier Future-Outlier commented Dec 18, 2024

Tracking issue

flyteorg/flyte#5318

Why are the changes needed?

We should make default input for pydantic basemodel works.

What changes were proposed in this pull request?

add default input logic handling for pydantic basemodel in to_click_option.

How was this patch tested?

local execution, remote execution, and integration test.

import os
from flytekit import map_task
from typing import  List
from flytekit import task, workflow, ImageSpec

from pydantic import BaseModel

flytekit_hash = "b3428e7d38cc1e834799bc8ea07f7dc7ebdab527"
flytekit = f"git+https://github.com/flyteorg/flytekit.git@{flytekit_hash}"
image = ImageSpec(
    packages=[flytekit, "pydantic>2"],
    apt_packages=["git"],
    registry="localhost:30000",
)

class MyBaseModel(BaseModel):
    my_floats: List[float] = [1.0, 2.0, 5.0, 10.0]

@task(container_image=image)
def print_float(my_float: float):
    print(f"my_float: {my_float}")


@workflow
def wf(bm: MyBaseModel = MyBaseModel()):
    map_task(print_float)(my_float=bm.my_floats)

if __name__ == "__main__":
    from flytekit.clis.sdk_in_container import pyflyte
    from click.testing import CliRunner

    # wf()

    runner = CliRunner()
    path = os.path.realpath(__file__)
    result = runner.invoke(pyflyte.main, ["run", path, "wf"])
    print("Local Execution: ", result.output)

    result = runner.invoke(pyflyte.main, ["run", "--remote", path, "wf"])
    print("Local Execution: ", result.output)

Setup process

Screenshots

local execution
image

remote execution
image

Check all the applicable boxes

  • I updated the documentation accordingly.
  • All new and existing tests passed.
  • All commits are signed-off.

Related PRs

Docs link

Signed-off-by: Future-Outlier <[email protected]>
Signed-off-by: Future-Outlier <[email protected]>
Comment on lines +479 to +490
if is_imported("pydantic"):
try:
from pydantic import BaseModel as BaseModelV2
from pydantic.v1 import BaseModel as BaseModelV1

if issubclass(python_type, BaseModelV2):
default_val = default_val.model_dump_json()
elif issubclass(python_type, BaseModelV1):
default_val = default_val.json()
except ImportError:
# Pydantic BaseModel v1
default_val = default_val.json()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it enough to duck type here?

if hasattr(default_val, "model_dump_json"):
    # pydantic v2
    default_val = default_val.model_dump_json()
elif hasattr(default_val, "json"):
    # pydantic v1
    default_val = default_val.json()
else:
    encoder = JSONEncoder(python_type)
    default_val = encoder.encode(default_val)

Copy link

codecov bot commented Dec 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.54%. Comparing base (f99d50e) to head (d46d292).
Report is 9 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##           master    #3013       +/-   ##
===========================================
+ Coverage   51.08%   96.54%   +45.45%     
===========================================
  Files         201       11      -190     
  Lines       21231      723    -20508     
  Branches     2731        0     -2731     
===========================================
- Hits        10846      698    -10148     
+ Misses       9787       25     -9762     
+ Partials      598        0      -598     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants