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

[PoC]: Declarative Workflows in V2 SDK #292

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

Conversation

mrkaye97
Copy link
Contributor

@mrkaye97 mrkaye97 commented Jan 3, 2025

This is an initial PoC for declarative workflows in the V2 SDK. We're seeking to accomplish a couple of things here:

  1. Allow the user to define a function, wrap it in a decorator, and have it just work (the original V2 implementation handles this)
  2. Allow for 1) in conjunction with opt-in type checking throughout the codebase, which is what this change seeks to achieve.

I added Pydantic support to the V2 SDK here, and then added a DeclarativeWorkflow (we can rename this to step, function, etc. if we want) generic-typed class that lets the user declaratively define their workflows and share the definitions across the codebase. The example provided should hopefully give a sense of what's possible with this approach.

The TL;DR is that with just a few lines of code in the workflow.py in the example, we get type checking for workflow inputs, both when accessing them with << workflow >>.workflow_input() and when spawning them with << workflow >>.spawn() or << workflow >>.run(). We also get "name-inference", meaning we don't need to pass a string value for the name of the workflow (or step or function) when we spawn it, which is another possible cause of errors in the current SDK.

Here are a few results from running the example here:

First greet run:

Screenshot 2025-01-03 at 4 43 07 PM Screenshot 2025-01-03 at 4 40 35 PM Screenshot 2025-01-03 at 4 40 43 PM

First child (counter) run

Screenshot 2025-01-03 at 4 41 06 PM Screenshot 2025-01-03 at 4 41 14 PM

Second greet run:

Screenshot 2025-01-03 at 4 41 37 PM Screenshot 2025-01-03 at 4 41 30 PM

Second child run:

Screenshot 2025-01-03 at 4 41 55 PM

Pydantic support:

Screenshot 2025-01-03 at 4 42 25 PM Screenshot 2025-01-03 at 4 42 36 PM

Comment on lines +26 to +27
workflow_input = greet_workflow.workflow_input(ctx)
greeting = workflow_input.greeting
Copy link
Contributor Author

Choose a reason for hiding this comment

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

these type check correctly, so no need to cast or parse from a dict to an ExampleInput

Comment on lines +31 to +33
input=language_counter_workflow.construct_spawn_workflow_input(
input=workflow_input
),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

types are enforced here, so you know the shape of input in your IDE + at type checking time

Comment on lines +24 to +25
@greet_workflow.declare()
async def greet(ctx: Context) -> dict[Literal["message"], str]:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

single decorator to register a function in Hatchet

workflow_input = greet_workflow.workflow_input(ctx)
greeting = workflow_input.greeting

await language_counter_workflow.spawn(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

refer to the workflow as an object instead of passing a string with the name

Comment on lines +47 to +49
greeting = language_counter_workflow.workflow_input(ctx).greeting

match greeting:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

type checking here for greeting

@mrkaye97 mrkaye97 requested review from abelanger5 and removed request for abelanger5 January 8, 2025 19:12
Copy link
Contributor

@reillyse reillyse left a comment

Choose a reason for hiding this comment

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

not super experienced in Python so probably don't understand the nuances here, but looks good

@mrkaye97
Copy link
Contributor Author

@grutt Think we should merge this or wait for a couple weeks till we have the real V2 hopefully done?

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.

3 participants