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

Merged
merged 5 commits into from
Jan 3, 2025

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 are being proposed in this pull request?

  1. Support default input handling for Pydantic BaseModel:
    Updated the to_click_option function to properly handle default values for Pydantic BaseModel inputs.

  2. Adopted Duck Typing for cleaner and more Pythonic code:
    Replaced explicit type checks with Duck Typing to improve code readability and align with Python's philosophy of emphasizing behavior over specific types.

referecne: PEP-0020, the Zen of Python: https://peps.python.org/pep-0020/#the-zen-of-python

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

Summary by Bito

This PR implements enhanced support for Pydantic BaseModel default inputs in Flytekit by adding proper serialization handling for both Pydantic v1 and v2 versions. The changes focus on improving default value handling in CLI options through duck typing and best practices, with comprehensive test coverage.

Unit tests added: True

Estimated effort to review (1-5, lower is better): 1

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
Member Author

Choose a reason for hiding this comment

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

yes it is, you are amazing Thomas, always learn something from you!

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.

Future-Outlier and others added 3 commits January 2, 2025 10:40
@flyte-bot
Copy link
Contributor

flyte-bot commented Jan 2, 2025

Code Review Agent Run #c862a3

Actionable Suggestions - 1
  • flytekit/clis/sdk_in_container/run.py - 1
    • Consider consolidating JSON serialization logic · Line 478-486
Review Details
  • Files reviewed - 3 · Commit Range: 90450d1..e35e334
    • flytekit/clis/sdk_in_container/run.py
    • tests/flytekit/integration/remote/test_remote.py
    • tests/flytekit/integration/remote/workflows/basic/pydantic_wf.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

AI Code Review powered by Bito Logo

@flyte-bot
Copy link
Contributor

Changelist by Bito

This pull request implements the following key changes.

Key Change Files Impacted
Feature Improvement - Enhanced Pydantic Support for Default Inputs

run.py - Added support for both Pydantic v1 and v2 default value serialization

test_remote.py - Added integration test for Pydantic default input with map task

pydantic_wf.py - Created test workflow demonstrating Pydantic BaseModel default input functionality

Comment on lines +478 to +486
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
Contributor

Choose a reason for hiding this comment

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

Consider consolidating JSON serialization logic

Consider consolidating the JSON serialization logic into a single helper function to handle both Pydantic v1 and v2 cases along with the default case using JSONEncoder. This would improve maintainability and reduce code duplication.

Code suggestion
Check the AI-generated fix before applying
 @@ -478,9 +478,15 @@
 -                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)
 +                default_val = _serialize_to_json(default_val, python_type)
 +
 def _serialize_to_json(val, python_type):
 +    if hasattr(val, "model_dump_json"):
 +        # pydantic v2
 +        return val.model_dump_json()
 +    elif hasattr(val, "json"):
 +        # pydantic v1
 +        return val.json()
 +    else:
 +        encoder = JSONEncoder(python_type)
 +        return encoder.encode(val)

Code Review Run #c862a3


Is this a valid issue, or was it incorrectly flagged by the Agent?

  • it was incorrectly flagged

@Future-Outlier Future-Outlier merged commit 8ad89c9 into master Jan 3, 2025
102 checks passed
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.

4 participants