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

[Core feature] Flytekit should support using output with Non-Any type as the input with Any type. #5366

Open
2 tasks done
Mecoli1219 opened this issue May 15, 2024 · 5 comments
Assignees
Labels
enhancement New feature or request flytekit FlyteKit Python related issue

Comments

@Mecoli1219
Copy link

Mecoli1219 commented May 15, 2024

Motivation: Why do you think this is important?

Intuitively, Any type should take every possible type as the input. Flytekit today supports using all Python types as the input, but it doesn't support

  1. the non-Any output from another task.
  2. the non-Any input from the workflow.

Goal: What should the final outcome look like, ideally?

The following scenarios should succeed:

Scenario 1:

@task()
def foo(a: Any) -> int:
    if type(a) == int:
        return a + 1
    return 0

@workflow
def wf(a: int) -> int:
    return foo(a=a)

Scenario 2:

@workflow
def wf1(a: Any) -> int:
    a1 = foo(a=a)
    return foo(a=a1)

Describe alternatives you've considered

Adding some tags as type hints for the transformer?

Propose: Link/Inline OR Additional context

No response

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

  • Yes

Have you read the Code of Conduct?

  • Yes
@Mecoli1219 Mecoli1219 added enhancement New feature or request untriaged This issues has not yet been looked at by the Maintainers labels May 15, 2024
Copy link

welcome bot commented May 15, 2024

Thank you for opening your first issue here! 🛠

Copy link

runllm bot commented May 15, 2024

To support using output with Non-Any type as the input with Any type in Flytekit, you can leverage custom type transformers. This allows Flytekit to handle various types dynamically. Here's how you can achieve this:

  1. Define a Custom Type Transformer: Create a type transformer that can handle Any type and register it with Flytekit.
  2. Implement the Workflow and Task: Use the custom transformer in your workflow and task definitions.

Here's an example implementation:

from flytekit import task, workflow, TypeEngine, context_manager
from flytekit.types.structured import StructuredDataset
from flytekit.models import types as _type_models
from flytekit.models.literals import Literal, Scalar, Blob, BlobMetadata

class AnyTypeTransformer(TypeEngine.TypeTransformer):
    def __init__(self):
        super().__init__(name="AnyType", t=object)

    def get_literal_type(self, t):
        return _type_models.LiteralType(simple=_type_models.SimpleType.STRUCT)

    def to_literal(self, ctx, python_val, python_type, expected):
        local_file = ctx.file_access.get_random_local_path() + ".json"
        with open(local_file, "w") as f:
            json.dump(python_val, f)
        remote_file = ctx.file_access.get_random_remote_path()
        ctx.file_access.upload(local_file, remote_file)
        return Literal(scalar=Scalar(blob=Blob(uri=remote_file, metadata=BlobMetadata(type=self.get_literal_type(python_type)))))

    def to_python_value(self, ctx, lv, expected_python_type):
        local_file = ctx.file_access.get_random_local_path() + ".json"
        ctx.file_access.download(lv.scalar.blob.uri, local_file)
        with open(local_file, "r") as f:
            return json.load(f)

TypeEngine.register(AnyTypeTransformer())

@task
def foo(a: Any) -> int:
    if isinstance(a, int):
        return a + 1
    return 0

@workflow
def wf(a: int) -> int:
    return foo(a=a)

@workflow
def wf1(a: Any) -> int:
    a1 = foo(a=a)
    return foo(a=a1)

This code defines a custom type transformer for Any type and registers it with Flytekit. The workflows wf and wf1 demonstrate how to use this transformer.

Would you like more details on any specific part?

Sources:


This is an AI-generated response and your feedback is appreciated! Please leave a 👍 if this is helpful and 👎 if it is not.

Mention @RunLLM to ask follow-up questions. See runllm.com for more!

@eapolinario
Copy link
Contributor

Related to #5319.

@eapolinario eapolinario added flytekit FlyteKit Python related issue and removed untriaged This issues has not yet been looked at by the Maintainers labels May 16, 2024
@Future-Outlier Future-Outlier self-assigned this May 20, 2024
@Future-Outlier
Copy link
Member

This is important to my LangChain Agent PR, so I will take over it.

@Mecoli1219
Copy link
Author

NP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request flytekit FlyteKit Python related issue
Projects
None yet
Development

No branches or pull requests

3 participants