Skip to main content

Understanding Workflow Context

Every step in a Sample Workflow has access to a context object (ctx) that provides essential information and utilities for the current workflow run. This context allows steps to communicate with each other, access user information, and interact with the backend system.

The WorkflowRunContext Class

The workflow context is implemented through the WorkflowRunContext class, which provides the following key capabilities:

Properties

  • backend_token: Authentication token for making API calls to the SampleHC backend
  • current_user: Information about the user who initiated the workflow (dict with id, email, and name fields). This is None when the run was not started by a user — for example, runs started by a cron schedule or an event — so guard access with a check like ctx.current_user["name"] if ctx.current_user else "Automated".
  • resume_data: Data used when resuming a suspended workflow
  • scope: Additional scoped data for the step that you’re on. For example, loops will have i and item in scope.
  • workflow_run_id: The ID of the current workflow run. This is useful when you need to look up other tasks from the same run, for example inside a next_task routing override.

Key Methods

get_step_result(step_id: str)

Retrieves the output from a previous step in the workflow by its step ID.
This method is essential for passing data between workflow steps, allowing later steps to build upon the results of earlier ones.

get_start_data()

Retrieves the initial data that was provided when the workflow was started.

Using Context in Workflow Steps

In Automated Steps

Best Practices

  1. Step ID Consistency: Use clear, descriptive step IDs that make it easy to reference them in later steps.
  2. Data Validation: Ensure the output is what you expect from previous steps before using it.
  3. Context Security: The context includes authentication tokens, so be careful when logging or exposing context data.