Skip to main content

What is Step Context?

Step Context provides a way to share workflow-level data with steps executing on remote Celery workers. Unlike the workflow context which is process-local, Step Context is serialized and passed to workers, making it accessible in distributed step execution.

Key Characteristics

Type-Safe

Define typed context fields using Pydantic models with full IDE support.

Read-Only in Steps

Context is read-only during step execution to prevent race conditions.

Distributed

Context is automatically serialized and passed to Celery workers.

Event-Sourced

Context changes are recorded as events for deterministic replay.

Why Read-Only in Steps?

When steps execute in parallel on different workers, allowing them to modify shared context would cause race conditions:
By making context read-only in steps, PyWorkflow follows the same pattern used by Temporal and Prefect - activities/tasks are stateless, and state mutations happen through return values in the workflow.
If you need to update context based on step results, do it in the workflow code after the step returns.

Defining a Context Class

Create a context class by extending StepContext:

Immutable by Design

Step Context is immutable (frozen). To update values, use with_updates() which creates a new instance:
Direct attribute assignment raises an error:

Using Step Context

Setting Context (Workflow Only)

Use set_step_context() to set or update the context. This can only be called from workflow code:
set_step_context() is an async function because it persists the context to storage.

Reading Context (Workflow and Steps)

Use get_step_context() to access the current context from anywhere:

Checking Context Availability

Use has_step_context() to check if context is available:

Read-Only Enforcement

Attempting to set context from within a step raises a RuntimeError:
To update context based on step results, return data from the step and update in the workflow:

Context Persistence and Replay

Step Context is persisted for durability:
  1. Persistence: When you call set_step_context(), the context is stored in the WorkflowRun.context field.
  2. Replay: When a workflow resumes after suspension:
    • Context is restored from WorkflowRun.context
    • Steps receive the same context they had during original execution

Complex Context Types

Step Context supports complex nested types:
All context fields must be JSON-serializable. Avoid storing non-serializable objects like database connections or file handles.

Best Practices

Store only essential cross-cutting data like IDs, user info, and configuration. Don’t use context as a data store - pass large data as step arguments instead.
Step Context is ideal for data needed by many steps: auth info, workspace IDs, correlation IDs, feature flags.
Set up context at the beginning of your workflow before calling any steps.
Context is persisted to storage. Use secret managers or environment variables for sensitive data.

API Reference

StepContext Methods

Next Steps

Steps

Learn about steps - the building blocks that use context.

Events

Understand how context changes are event-sourced.

Fault Tolerance

See how context survives crashes and restarts.

Configuration

Configure storage backends for context persistence.