Normal Steps

You can use Step to define a normal step. Normal steps takes a body function that is called when the step is reached.

from workflows_py.workflow import Step, Workflow

def add_five(ctx):
    return ctx.get_step_result("multiply_by_two") + 5

workflow = Workflow()
workflow.then(
    Step("multiply_by_two", lambda ctx: return ctx.get_start_data()["input_number"] * 2),
).then(
    Step("add_five", add_five)
)

This example shows a workflow with two automated steps. The body function of each step can either be defined inline (using the lambda keyword) or as a separate function.

Body functions can use the context (ctx) to access data from previous steps or external systems. Read more in the context section. The return value of each function can be accessed by subsequent steps using ctx.get_step_result("step-id").

Screen Steps

Screen steps are steps that present a UI to the user for input or review. To define a screen step, you need to both define

  • a React component (a .tsx file)
  • a ScreenStep in the workflow that points to the React component by file path
from workflows_py.workflow import ScreenStep, Step, Workflow

workflow = Workflow()
workflow.then(
    Step("prepare-greeting", lambda ctx: {"user_name": "John"})
).then(
    ScreenStep("greeting", screen_path="./screens/greeting-screen.tsx", get_props=lambda ctx: {"name": ctx.get_step_result("prepare-greeting")["user_name"]})
)

The return value of screen steps is the value passed into the result field of the handleComplete function. In the example above (see greeting-screen.tsx), the return value will be the string "confirmed".

See the screens section for more information on how to create and customize screens.

Step Outputs

Every step in a workflow can return a value. This value can be accessed by subsequent steps using ctx.get_step_result("step-id").

IMPORTANT: All outputs from workflow steps must be JSON serializable. This is critical for Sample Workflows to function properly, as step outputs are stored and passed between steps in a serialized format.

JSON Serializable Types

Step outputs can include:

  • Primitive types: string, number, boolean, null
  • Arrays: Lists of JSON-serializable values
  • Objects: Plain objects with JSON-serializable properties
  • Nested structures: Any combination of the above

Example: Correct Step Output

# ✅ Good - JSON serializable
def process_data(ctx):
    return {
        "user_id": 123,
        "name": "John Doe",
        "scores": [85, 92, 78],
        "metadata": {
            "processed_at": "2024-01-15T10:30:00Z",
            "status": "completed"
        }
    }

Example: Incorrect Step Output

# ❌ Bad - Not JSON serializable
def process_data(ctx):
    return {
        "user": User(id=123, name="John"),  # Class instance
        "callback": lambda x: x * 2,        # Function
        "created_at": datetime.now()        # Date object
    }

If you need to work with non-serializable data, convert it to a serializable format before returning from the step.

Version Control and Deployment

Sample Workflows are version controlled through Git, making it easy to track changes, collaborate with team members, and maintain different versions of your workflows.

Git-Based Version Control

  • Workflow definitions (Python files) and screen components (TypeScript files) are stored in a Git repository
  • This provides full history tracking and the ability to revert to previous versions if needed

Automatic Deployment

When changes are made to workflow files:

  1. Automatic Detection: Sample automatically detects changes to workflow files
  2. Deployment: Updated workflows are deployed and become available for use
  3. Version Management: Previous versions remain accessible, ensuring running workflows can complete even after updates