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: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 extendingStepContext:
Immutable by Design
Step Context is immutable (frozen). To update values, usewith_updates() which creates a new instance:
Using Step Context
Setting Context (Workflow Only)
Useset_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)
Useget_step_context() to access the current context from anywhere:
Checking Context Availability
Usehas_step_context() to check if context is available:
Read-Only Enforcement
Attempting to set context from within a step raises aRuntimeError:
Context Persistence and Replay
Step Context is persisted for durability:-
Persistence: When you call
set_step_context(), the context is stored in theWorkflowRun.contextfield. -
Replay: When a workflow resumes after suspension:
- Context is restored from
WorkflowRun.context - Steps receive the same context they had during original execution
- Context is restored from
Complex Context Types
Step Context supports complex nested types:Best Practices
Keep context small
Keep context small
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.
Use context for cross-cutting concerns
Use context for cross-cutting concerns
Step Context is ideal for data needed by many steps: auth info, workspace IDs, correlation IDs, feature flags.
Initialize context early
Initialize context early
Set up context at the beginning of your workflow before calling any steps.
Don't store secrets in context
Don't store secrets in context
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.