Python SDK
The Python SDK is a thin, async-first client for a self-hosted Durable Workflow Server or Durable Workflow Cloud namespace runtime. It lets Python processes start, observe, signal, and cancel workflows through the runtime's control-plane API, and register as workers that execute workflow tasks and activities.
Try the local Sample App playground
For the shortest no-Cloud authoring journey, open the current Sample App
main branch in GitHub Codespaces
and run:
scripts/playground python
The local playground generates caller-owned workflow and activity source, selects the current stable artifacts, and starts the published Server and Waterline. It waits for a worker registration whose identity, workflow type, activity type, and task queue match the generated contract before starting the workflow. Success requires the expected completed result and history; the terminal then prints the exact local Waterline run link and the path to structured JSON evidence.
The package installation, inline quickstart, API reference, and repository example below remain the direct paths for users who do not want Sample App.
The SDK targets the same durable model as the PHP package — instance IDs, run IDs, history events, task queues, and type keys are shared across languages. A Python worker can serve activities for a PHP-authored workflow, and vice versa.
For a capability comparison across the Python SDK, PHP SDK, Rust SDK, and
dw, see
Client and Worker Capabilities.
For constructor signatures, return types, exception classes, and metric names, see the generated Python SDK API reference.
Python SDK 2.5.0 adds an optional bounded history cache for sticky execution. Use Server 2.5.10 or newer and enable it on each worker. See Portable Worker Affinity and the Python guide and runnable example.
Cloud customers use the provisioned namespace's runtime URL and namespace with separate client and worker credentials. See Cloud Managed Runtime for that connection boundary; the quickstart below uses local self-hosted values.
Requirements
- Python 3.10 or later
- Docker (for the local Server used in this quickstart), an existing self-hosted Server, or a provisioned Cloud namespace runtime
Installation
Install the stable Python SDK release. The exact requirement is generated from the same tuple as the Server quickstart, and a lock or constraints file can retain it for reproducibility.
pip install durable-workflow==2.24.0
The SDK depends on httpx for HTTP and on the
fastavro Python package to encode the Apache Avro wire format used by the
only public v2 payload codec. Prometheus metrics support is optional.
Upgrading workers
Python SDK 2.14.1 fixes wait_condition() fingerprints that changed across
Python versions or worker restarts. Upgrade every worker sharing a task queue
together. Once a run records a v2 wait fingerprint, keep all workers that can
replay it on 2.14.1 or later. Do not roll the workers on that queue back to an
SDK version below 2.14.1.
Legacy v1 fingerprints that no longer match are accepted as unverifiable, so
that compatibility path cannot detect predicate changes. wait_condition()
lambdas on the same line that contain list, dict or set comprehensions
share one combined fingerprint. Put those predicates on separate lines in new
workflow definitions to detect switching between them. See the
tagged 2.14.1 compatibility notes.
Python SDK 2.15 and 2.16 add:
- Sticky history suffixes (2.16). Workers with a sticky cache
(
sticky_cache_capacity > 0) advertisesticky_history_suffix. On Server 3.5.0 or later, a sticky hit receives only the history after the sequence the worker cached. A suffix that doesn't continue the cached history exactly is dropped and the full history is fetched. - Typed workflow values (2.15).
@workflow.defn(..., typed=True)decodesrunand handler arguments to their annotations, andresult_type=decodes child and client results. Untyped workflows receive plain values as before, so in-flight histories replay unchanged. - Lazy pages (2.15).
Client.iter_workflows,Client.iter_historyandWorkflowHandle.iter_historyfollow page tokens while holding one page. See workflow list and history pages for traversal, resume and error handling. - Run activities (2.15).
Client.get_run_activitiesandWorkflowHandle.get_activitiesreturn typed activity and attempt metadata, andActivityInfo.activity_execution_idexposes the logical activity identity. - Local activity backoff (2.15.1). Retries past the end of the backoff list repeat the final entry instead of retrying immediately.
See the 2.16.0 changelog for the complete list.
Python SDK 2.17 adds Client.update_outcome and
Client.update_outcome_receipt to read an accepted update later without
submitting it again. These methods require
capabilities.update_outcome_reads (native Server 3.6.0 or later).
update_outcome waits for completion by default and raises UpdateStillPending
if the update is unfinished. A returned None is a completed null result.
update_outcome_receipt defaults to the accepted stage and preserves the
explicit update_status. See the
Python SDK update-outcome guide
for language-specific examples.
Python SDK 2.18 adds:
- Capped generated retries.
ActivityRetryPolicy.exponential(...)generates activity retry delays with a 60-second default maximum interval. See activity retries and timeouts. - History-budget refusals fail the run. When the Server refuses a
workflow-task completion over its history-transaction budget, the worker
fails the run once with a
structural_limitfailure (WorkflowTaskHistoryLimitExceeded) instead of leaving the run replay-blocked.
Quickstart
Here is a complete Python program that defines a workflow with one activity, starts it against a local Durable Workflow server, and waits for the result:
import asyncio
import uuid
from durable_workflow import Client, Worker, workflow, activity
@activity.defn(name="greet")
async def greet(name: str) -> dict:
return {"greeting": f"Hello, {name}!", "length": len(name)}
@workflow.defn(name="greeter")
class GreeterWorkflow:
def run(self, ctx, *args):
result = yield ctx.schedule_activity("greet", list(args))
return result
async def main():
workflow_id = f"greeting-{uuid.uuid4().hex}"
async with Client("http://localhost:8080", token="dev-token", namespace="default") as client:
handle = await client.start_workflow(
workflow_type="greeter",
task_queue="default",
workflow_id=workflow_id,
input=["world"],
)
worker = Worker(
client,
task_queue="default",
workflows=[GreeterWorkflow],
activities=[greet],
)
await worker.run_until(workflow_id=workflow_id, timeout=30.0)
result = await handle.result(timeout=10.0)
print(result) # {"greeting": "Hello, world!", "length": 5}
asyncio.run(main())
Running against a local server
The program above assumes a Durable Workflow server reachable at
http://localhost:8080 with the local dev-token. If you don't have one yet,
the fastest source-free path is the published server image:
export DW_SERVER_IMAGE=durableworkflow/server:3.31.2
export DW_AUTH_TOKEN=dev-token
docker volume create durable-workflow-python-quickstart
docker rm -f durable-workflow-server >/dev/null 2>&1 || true
docker run -d --name durable-workflow-server \
-p 127.0.0.1:8080:8080 \
-v durable-workflow-python-quickstart:/app/database \
-e DW_AUTH_TOKEN="$DW_AUTH_TOKEN" \
"$DW_SERVER_IMAGE"
until curl -sf http://localhost:8080/api/ready > /dev/null; do sleep 1; done
The native Server creates its SQLite schema and the default namespace on
first start, so there is no separate bootstrap step. For production
deployment — authentication, database config, TLS — see the server setup guide.
For a larger example, the SDK repository includes examples/order_processing, a Docker Compose stack that runs a Python worker through an order workflow end to end.
Client API Reference
The async durable_workflow.Client is the public entry point for control-plane
and worker-plane HTTP calls. Use it as an async context manager so the
underlying httpx.AsyncClient connection pool closes cleanly.
from durable_workflow import Client
async with Client(
"https://workflow.example.com",
token="shared-token",
namespace="default",
timeout=60.0,
) as client:
info = await client.get_cluster_info()
Constructor
| Argument | Type | Default | Use when |
|---|---|---|---|
base_url | str | required | Server origin, without /api. |
token | str | None | None | One bearer token should authorize both control-plane and worker-plane calls. |
control_token | str | None | None | Control-plane calls need a different bearer token than worker polling. |
worker_token | str | None | None | Worker-plane calls need a different bearer token than operator calls. |
namespace | str | "default" | Target a server namespace through X-Namespace. |
timeout | float | 60.0 | Override the default HTTP timeout. |
retry_policy | TransportRetryPolicy | None | default policy | Tune transport retries for transient HTTP failures. |
metrics | MetricsRecorder | None | no-op | Emit client and worker metrics to a custom recorder. |
payload_size_limit_bytes | int | SDK default | Match the server's max payload-byte contract. |
payload_size_warning_threshold_percent | int | SDK default | Warn before a payload reaches the configured limit. |
payload_size_warnings | bool | True | Disable local payload-size warnings in tests or controlled scripts. |
failure_report_limit_bytes | int | 1048576 | Lower the complete encoded failure-report budget. See bounded failure reports. |
token is the simplest option. If both control_token and worker_token are
set, control-plane methods use control_token and worker-plane methods use
worker_token.
Bounded failure reports
Python SDK 2.11.0 introduced bounded outgoing failure reports. Defaults are 62 KiB UTF-8 for the message, 128 KiB for the stack, 64 KiB for combined serialized optional details and diagnostics, and 1 MiB for the complete HTTPX-encoded body. Markers, JSON escaping, claim identity and other commands in the batch count toward that body limit. A lower configured or discovered transport limit takes precedence. Failure type, class, code, retry policy and claim identity are preserved.
Set a smaller body budget when needed. Payload-size warning settings remain advisory.
from durable_workflow import Client
async with Client(
"https://workflow.example.com",
worker_token="worker-token",
control_token="operator-token",
failure_report_limit_bytes=512 * 1024,
) as client:
info = await client.get_cluster_info()
A visible truncation marker and original byte counts identify shortened fields. Avro details remain intact or are omitted whole. An unrepresentable required envelope raises FailureReportTooLarge before HTTP submission. Local failures are normalized before workflow code observes them, so first execution and cold replay see the persisted message. An already observed local message remains immutable during final batch fitting.
External Payload Storage
Normal Client and Worker operations use Server's authenticated runtime
transport automatically. The SDK discovers namespace policy and transport
limits through GET /api/cluster/info, uploads encoded values above the
threshold or needed to fit the request, and fetches referenced values before
decode. Application workflow and activity code receives the decoded value.
Provider credentials stay on Server. Remote workers do not need boto3, provider
clients or access to Server's local storage directory.
The runtime reference has schema
durable-workflow.v2.runtime-external-payload-reference.v1 and an opaque
reference_id. It has no provider uri. Upload/fetch uses the normal role
credential and namespace, verifies encoded-byte size and SHA-256, and preserves
task-attempt and lease fencing for worker completion. See the
runtime HTTP contract
for routes, limits and typed refusals. SDK support depends on an available
compatible transport, not merely on a configured storage driver.
The following direct provider helpers serve application-managed storage
and test fixtures that deliberately access their own provider. They use a
different provider-URI schema. They are not required for normal Server or Cloud
clients, and a provider external_storage envelope is rejected by Server with
HTTP 422 and external_payload_unsupported.
| API | Role | Failure surface |
|---|---|---|
ExternalStorageDriver | Protocol for put(data, sha256=..., codec=...), get(uri), and delete(uri). | Driver-raised storage errors. |
LocalFilesystemExternalStorage | Dependency-free file:// driver for local development and tests. | ValueError when a referenced URI escapes the configured root. |
S3ExternalStorage | Adapter for a boto3-compatible client. | ValueError for foreign bucket/prefix references or non-byte responses. |
GCSExternalStorage | Adapter for a google-cloud-storage-style client. | ValueError for foreign bucket/prefix references or non-byte responses. |
AzureBlobExternalStorage | Adapter for an Azure container client. | ValueError for foreign container/prefix references or non-byte responses. |
ExternalPayloadReference | Immutable wire reference with uri, sha256, size_bytes, codec, and schema. | ValueError when from_dict() receives an unsupported schema or malformed fields. |
ExternalPayloadCache | Bounded replay cache for already verified external payload bytes. | Constructor rejects non-positive entry or byte limits. |
store_external_payload() | Stores encoded bytes through a driver and returns ExternalPayloadReference. | Driver errors. |
fetch_external_payload() | Fetches referenced bytes, then verifies size and SHA-256 before decode. | ExternalPayloadIntegrityError on size/hash mismatch. |
delete_external_payload() | Deletes the referenced object and evicts any cache entry. | Driver-raised storage errors. |
external_storage_envelope() | Encodes a value inline until the threshold is crossed, then writes bytes through a driver. | ValueError when the threshold is invalid or no driver can resolve a reference. |
external_storage_driver_from_policy() | Builds a direct driver from an explicitly supplied policy and application-owned provider clients. It does not authorize direct provider access on Server or Cloud. | ValueError when the policy is disabled, unsupported, or missing the required client/bucket/container. |
from durable_workflow import (
ExternalPayloadCache,
LocalFilesystemExternalStorage,
external_storage_envelope,
to_avro_payload_value,
)
from durable_workflow.serializer import encode
from durable_workflow.external_storage import fetch_external_payload, store_external_payload
storage = LocalFilesystemExternalStorage("/var/lib/durable-workflow/payloads")
cache = ExternalPayloadCache(max_entries=256, max_bytes=32 * 1024 * 1024)
payload = to_avro_payload_value({"invoice_pdf": "x" * 1_000_000})
envelope = external_storage_envelope(
payload,
external_storage=storage,
threshold_bytes=64 * 1024,
)
reference = store_external_payload(
storage,
encode({"archived": True}).encode("utf-8"),
codec="avro",
)
payload_bytes = fetch_external_payload(storage, reference, cache=cache)
For these direct helpers, the reference schema is EXTERNAL_PAYLOAD_REFERENCE_SCHEMA
(durable-workflow.v2.external-payload-reference.v1). Object-store adapters do
not add cloud SDK dependencies to durable-workflow; applications pass their
already configured S3, GCS, or Azure clients.
Their provider reference has the following fields. This is not the runtime reference accepted by Server HTTP routes:
{
"schema": "durable-workflow.v2.external-payload-reference.v1",
"uri": "s3://dw-payloads/billing/run-001/input.avro",
"sha256": "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
"size_bytes": 1048576,
"codec": "avro",
"expires_at": "2026-05-22T00:00:00Z"
}
ExternalPayloadReference.from_dict() rejects unknown schemas and malformed
fields. fetch_external_payload() verifies size_bytes and sha256 before
returning bytes, and raises ExternalPayloadIntegrityError on size or hash
mismatch. Missing objects can also raise the provider driver's error. Use
ExternalPayloadCache for verified bytes only. Cache eviction does not grant
permission to delete a Server-managed object. delete_external_payload() is a
direct provider operation, not a runtime-reference cleanup API.
Use external_storage_driver_from_policy() in code that deliberately accesses
application-owned provider storage and owns the required client. Reading a
namespace policy does not make provider envelopes valid Server requests.
Provider clients remain application-owned; these optional adapters do not add
boto3, google-cloud-storage or azure-storage-blob as runtime dependencies:
from durable_workflow.external_storage import external_storage_driver_from_policy
namespace = await client.describe_namespace("billing")
driver = external_storage_driver_from_policy(
namespace.external_payload_storage,
s3_client=application_owned_s3_client,
)
The factory raises ValueError when the policy is disabled, when the named
driver is unsupported, or when the matching provider client is missing.
Disabled policies do not return a no-op driver; callers should branch on
policy.enabled before asking for a driver.
Namespaces
Namespaces are the tenancy boundary for workflows, schedules, search
attributes, and external payload storage. The Client targets one namespace at a
time through the namespace= constructor argument, but the operator surface
below applies to any namespace the bearer token is authorized for.
| Method | Returns | Failure surface |
|---|---|---|
await client.list_namespaces() | NamespaceList | Auth/server errors. |
await client.describe_namespace(name) | NamespaceDescription | NamespaceNotFound, auth/server errors. |
await client.create_namespace(name, description=None, retention_days=30) | NamespaceDescription | InvalidArgument for duplicate names or invalid retention, auth/server errors. |
await client.update_namespace(name, description=None, retention_days=None) | NamespaceDescription | NamespaceNotFound, InvalidArgument, auth/server errors. Only provided fields are sent. |
await client.set_namespace_external_storage(name, driver=..., enabled=True, threshold_bytes=None, config=None) | NamespaceDescription | InvalidArgument when the policy fails server validation, auth/server errors. |
await client.test_external_storage(driver=None, small_payload_bytes=None, large_payload_bytes=None) | StorageTestResult | InvalidArgument when the bound policy is missing required fields, auth/server errors. |
set_namespace_external_storage mirrors dw namespace:set-storage-driver. The
first positional argument is the namespace name, matching describe_namespace,
create_namespace, and update_namespace. The deprecated namespace= alias
emits a DeprecationWarning; new code should use the positional name. The
config dict carries driver-specific keys including the optional disk field
on s3, gcs, and azure so credentials stay on the server. The returned
NamespaceDescription reflects the policy the server actually persisted —
including the threshold the server fell back to when no caller value was
provided.
test_external_storage mirrors dw storage:test. The server round-trips a
small and large payload through the bound external storage driver and returns
StorageTestResult, which exposes per-payload StoragePayloadTestResult
records with wrote, read, verified, latency_ms, and bytes fields.
NamespaceDescription carries the namespace's external_payload_storage
policy as documented in External Payload Storage.
Normal clients use the discovered runtime transport automatically. Direct
provider helpers require application-owned provider access; a policy description
does not make provider envelopes valid Server requests.
Cluster and Task Queues
| Method | Returns | Notes |
|---|---|---|
await client.health() | dict[str, Any] | Calls the health endpoint for liveness checks. |
await client.get_cluster_info() | dict[str, Any] | Reads server version, protocol, capability, and compatibility metadata. |
await client.list_task_queues() | TaskQueueList | Lists task queues visible in the namespace. |
await client.describe_task_queue(name) | TaskQueueDescription | Returns worker capacity, current leases, query admission, and dispatch-budget facts. |
await client.list_task_queue_build_ids(task_queue) | TaskQueueBuildIdRollout | Snapshots the per-build-id cohort state for a queue, including unversioned workers under a cohort whose build_id is None. |
await client.drain_task_queue_build_id(task_queue, build_id) | TaskQueueBuildIdRolloutState | Marks a build-id cohort as draining so it stops claiming new tasks. Pass build_id=None to drain unversioned workers. Idempotent. |
await client.resume_task_queue_build_id(task_queue, build_id) | TaskQueueBuildIdRolloutState | Reverts a previous drain so the cohort can claim work again. Pass build_id=None to resume unversioned workers. Idempotent. |
Task queue return types expose nested TaskQueueAdmission,
TaskQueueTaskAdmission, TaskQueueQueryAdmission, TaskQueueBuildIdCohort,
TaskQueueBuildIdRollout, and TaskQueueBuildIdRolloutState dataclasses so
scripts can check server-side capacity and build-id rollout without parsing
prose output. See
Worker Build-Id Rollout for the
end-to-end rollout walkthrough; the CLI mirrors of these methods are
dw task-queue:build-ids, dw task-queue:drain, and dw task-queue:resume.
Workflow Operations
| Method | Returns | Failure surface |
|---|---|---|
await client.start_workflow(...) | WorkflowHandle | WorkflowAlreadyStarted, InvalidArgument, Unauthorized, ServerError |
await client.describe_workflow(workflow_id) | WorkflowExecution | WorkflowNotFound, auth/server errors |
await client.list_workflows(...) | WorkflowList (one page) | Auth/server errors |
client.iter_workflows(...) | AsyncIterator[WorkflowExecution] | Page errors and cancellation propagate during iteration. See pagination. |
await client.list_workflow_runs(workflow_id) | WorkflowRunList | WorkflowNotFound, auth/server errors |
await client.describe_workflow_run(workflow_id, run_id) | WorkflowRun | WorkflowNotFound, auth/server errors |
await client.get_history(workflow_id, run_id, ...) | one page of decoded history | WorkflowNotFound, auth/server errors |
client.iter_history(workflow_id, run_id, ...) | AsyncIterator[dict[str, Any]] | Page errors and cancellation propagate during iteration. |
handle.iter_history(...) | AsyncIterator[dict[str, Any]] | Same history traversal. Requires a handle with an explicit run_id. |
await client.export_history(workflow_id, run_id) | decoded archival history payload | WorkflowNotFound, auth/server errors |
await client.signal_workflow(workflow_id, signal_name, args=None) | admission receipt (dict) | WorkflowNotFound, InvalidArgument, auth/server errors |
await client.query_workflow(workflow_id, query_name, args=None) | query receipt (dict) | QueryFailed, WorkflowNotFound, auth/server errors |
await client.update_workflow(workflow_id, update_name, args=None, ...) | update receipt (dict) | UpdateRejected, UpdateValidationFailed, UpdateFailed, WorkflowNotFound, auth/server errors |
await client.cancel_workflow(workflow_id, reason=None) | None | WorkflowNotFound, auth/server errors |
await client.terminate_workflow(workflow_id, reason=None) | None | WorkflowNotFound, auth/server errors |
await client.repair_workflow(workflow_id) | WorkflowCommandResult | WorkflowNotFound, auth/server errors |
await client.archive_workflow(workflow_id, reason=None) | WorkflowCommandResult | WorkflowNotFound, auth/server errors |
await client.get_result(handle, poll_interval=0.5, timeout=30.0) | decoded workflow output | WorkflowFailed, WorkflowCancelled, WorkflowTerminated, TimeoutError |
signal_workflow returns the admission receipt, including its request_id.
query_workflow and WorkflowHandle.query return a query receipt; read
receipt["result"] for the decoded handler value, including when the handler successfully
returns None. See the Python query receipt guide.
Python SDK 2.17.2 fixes valid Avro query results containing bytes. The
result_envelope stays authoritative; only an optional plain JSON projection
that cannot be encoded is omitted. Published 2.17.2 / native Server 3.9.1
installed controls
(linux/amd64, SQLite) verify nested-byte and None query receipts, cold Worker
replacement and pending-to-completed-null update reads. Query return shapes
remain as described above. This patch does not advertise timeout-cleanup or
parallel-policy capabilities. The earlier 2.17.1 completion-size evidence keeps
its exact version and experiment scope.
update_workflow defaults to wait_for="accepted". Its receipt can have
update_status="accepted" and result=None. With wait_for="completed",
wait_timeout_seconds bounds the wait. On expiry, the receipt stays accepted
and sets wait_timed_out=True. Only update_status="completed" identifies a
finished result, including None. Keep update_id for
later outcome reads.
start_workflow accepts workflow_type, task_queue, optional
workflow_id, optional input, duplicate_policy, memo,
search_attributes, business_key, execution_timeout_seconds, and
run_timeout_seconds. All caller payloads are Avro-enveloped before they cross
the HTTP boundary.
Use client.get_workflow_handle(workflow_id, run_id=None, workflow_type="")
when a script already knows the workflow id and wants handle-style methods.
WorkflowHandle method | Equivalent client method |
|---|---|
await handle.result(...) | client.get_result(handle, ...) |
await handle.describe() | client.describe_workflow(handle.workflow_id) |
await handle.signal(name, args=None) | client.signal_workflow(...) |
await handle.query(name, args=None) | client.query_workflow(...) |
await handle.update(name, args=None, ...) | client.update_workflow(...) |
await handle.cancel(reason=None) | client.cancel_workflow(...) |
await handle.terminate(reason=None) | client.terminate_workflow(...) |
Both operations close the Server run immediately with distinct terminal
outcomes. cancel_workflow does not deliver a cooperative request to workflow
code or run saga/finally cleanup. Use request_cancellation() with cooperating
workers for bounded workflow cleanup. See the
cooperative cancellation guide for operation
policies, supervision and recovery.
Current workflow identity
Python SDK 2.9.0 and later exposes read-only ctx.workflow_id and ctx.run_id
inside workflow code. Both are strings from the current task and stay identical
during replay. After continue-as-new, the workflow ID stays unchanged and the
successor gets its own run ID. Do not replace that ID with a predecessor ID
passed as an argument.
from durable_workflow import workflow
@workflow.defn(name="identity")
class IdentityWorkflow:
def run(self, ctx):
identity = {"workflow_id": ctx.workflow_id, "run_id": ctx.run_id}
yield ctx.schedule_activity("record_identity", [identity])
return identity
These properties read local task identity without an HTTP request. For status
polling outside workflow code, use await client.describe_workflow(workflow_id).
Use await client.query_workflow(workflow_id, query_name) for state exposed by a
registered query handler. Identity properties do not replace either operation.
Schedules
Schedules use ScheduleSpec for calendar/interval rules and ScheduleAction
for the workflow start request issued when a schedule fires.
| Method | Returns | Notes |
|---|---|---|
await client.create_schedule(...) | ScheduleHandle | Creates a schedule and returns a handle. |
await client.list_schedules() | ScheduleList | Lists visible schedules. |
await client.describe_schedule(schedule_id) | ScheduleDescription | Reads schedule status, action, next fire, and counters. |
await client.update_schedule(schedule_id, ...) | None | Updates spec, action, overlap policy, jitter, memo, search attributes, or note. |
await client.pause_schedule(schedule_id, note=None) | None | Pauses future fires. |
await client.resume_schedule(schedule_id, note=None) | None | Resumes a paused schedule. |
await client.trigger_schedule(schedule_id, overlap_policy=None) | ScheduleTriggerResult | Requests an immediate fire. |
await client.backfill_schedule(schedule_id, start_time=..., end_time=..., overlap_policy=None) | ScheduleBackfillResult | Replays missed fire windows. |
await client.get_schedule_history(schedule_id, *, limit=None, after_sequence=None) | ScheduleHistoryPage | Returns one page of the schedule's audit history stream, ordered by sequence ascending. limit is clamped server-side between 1 and 500 (default 100); after_sequence is a non-negative cursor obtained from the previous page's next_cursor. Raises ScheduleNotFound if the schedule id is unknown. History survives a delete_schedule call so operators can audit why a schedule was removed. |
client.iter_schedule_history(schedule_id, *, limit=None, after_sequence=None) | AsyncIterator[ScheduleHistoryEvent] | Yields every audit event for the schedule, paging under the hood until the server reports has_more=False. Same failure surface as get_schedule_history. |
await client.delete_schedule(schedule_id) | None | Deletes the schedule. |
client.get_schedule_handle(schedule_id) returns a ScheduleHandle with
describe, update, pause, resume, trigger, backfill, history,
iter_history, and delete methods that forward to the corresponding client
methods. ScheduleHandle.history(...) returns a ScheduleHistoryPage and
ScheduleHandle.iter_history(...) returns an AsyncIterator[ScheduleHistoryEvent].
A ScheduleHistoryPage carries the ordered events list, a has_more flag,
a next_cursor integer (or None on the final page), the schedule_id, and
the owning namespace. Each ScheduleHistoryEvent carries sequence,
event_type (ScheduleCreated, SchedulePaused, ScheduleResumed,
ScheduleUpdated, ScheduleTriggered, ScheduleTriggerSkipped, or
ScheduleDeleted), recorded_at, optional workflow_instance_id and
workflow_run_id for fired workflows, and the raw payload dictionary the
control plane recorded for the transition.
Bridge Events and Worker-Plane Methods
await client.send_webhook_bridge_event(adapter, action=..., target=..., input=..., idempotency_key=..., correlation=None)
returns BridgeAdapterOutcome, the same machine-readable outcome contract used
by dw bridge:webhook. It is the Python entry point for bounded ingress from
webhook-shaped systems.
The low-level worker-plane methods are public for custom workers and protocol
tests, but normal applications should use Worker:
| Method group | Methods |
|---|---|
| Worker registration | register_worker |
| Workflow tasks | poll_workflow_task, complete_workflow_task, fail_workflow_task, workflow_task_history |
| Query tasks | poll_query_task, complete_query_task, fail_query_task |
| Activity tasks | poll_activity_task, complete_activity_task, fail_activity_task, heartbeat_activity_task |
These methods send X-Durable-Workflow-Protocol-Version and use worker_token
when one is configured. Prefer the higher-level Worker unless you are writing
an SDK adapter or protocol conformance test.
workflow_task_history(...) pages replay history for one already leased
workflow task. Call it only after poll_workflow_task(...) returns
next_history_page_token:
page = await client.workflow_task_history(
task_id="workflow-task-01",
next_history_page_token="history-page-2",
lease_owner="python-worker-1",
workflow_task_attempt=2,
)
The request body uses next_history_page_token, lease_owner, and
workflow_task_attempt. The decoded response uses the worker-protocol field
names history_events, total_history_events, and
next_history_page_token; it does not use the control-plane run-history names
events or next_page_token.
Workers
The worker registry exposes which workers the server has seen recently and what each worker can run. Use these methods to drive build-id rollouts, find stale workers to deregister, and reconcile fleet capacity from operator scripts.
| Method | Returns | Failure surface |
|---|---|---|
await client.list_workers(task_queue=None, status=None) | WorkerList | Auth/server errors. |
await client.describe_worker(worker_id) | WorkerDescription | WorkerNotFound, auth/server errors. |
await client.deregister_worker(worker_id) | dict[str, Any] | WorkerNotFound, auth/server errors. |
list_workers filters server-side: pass task_queue to scope to one queue and
status to a single status string the server recognizes. The default returns
every registered worker for the namespace.
WorkerDescription carries the worker's runtime, SDK version, build id,
declared workflow and activity types, last heartbeat, and current task
admission so a script can decide which cohort to drain or which workers to
remove from the roster.
deregister_worker is idempotent on the server side; it removes a worker that
no longer heartbeats so capacity accounting and list_workers stay clean.
It does not interrupt in-flight leases.
Search Attributes
Search attributes are typed namespace metadata that appear on workflow executions for filtering and indexing. The Python client mirrors the same control-plane surface as the CLI.
| Method | Returns | Failure surface |
|---|---|---|
await client.list_search_attributes() | SearchAttributeList | Auth/server errors. |
await client.create_search_attribute(name, attribute_type) | dict[str, Any] | InvalidArgument for duplicate names or unsupported types, auth/server errors. |
await client.delete_search_attribute(name) | dict[str, Any] | SearchAttributeNotFound, InvalidArgument when the attribute is system-defined, auth/server errors. |
SearchAttributeList separates system and custom attribute definitions so
operator scripts can verify that the engine-defined keys (workflow id, status,
type, start time, etc.) are present before refusing to create a clashing custom
key. Supported attribute_type values are keyword, text, int, double,
bool, datetime, and keyword_list.
System Maintenance
Operator scripts and on-call automation drive the same maintenance loops as the
dw system:* commands through the Client. Each method requires the bearer
token to carry admin scope; without it the server returns Unauthorized.
| Method | Returns | CLI mirror |
|---|---|---|
await client.repair_status() | dict[str, Any] | dw system:repair-status |
await client.repair_pass(run_ids=None, instance_id=None) | dict[str, Any] | dw system:repair-pass |
await client.retention_status() | dict[str, Any] | dw system:retention-status |
await client.retention_pass(run_ids=None, limit=None) | dict[str, Any] | dw system:retention-pass |
await client.activity_timeout_status() | dict[str, Any] | dw system:activity-timeout-status |
await client.activity_timeout_pass(execution_ids=None, limit=None) | dict[str, Any] | dw system:activity-timeout-pass |
repair_pass runs one task-repair sweep. With no filters the server runs a
full-scope pass over the namespace; pass run_ids to narrow the sweep to a
specific list of workflow runs, or instance_id to bound it to a single
running instance.
With no run_ids, retention_pass asks the PHP Server to prune expired
terminal runs under the current namespace policy, up to its scan limit.
Explicit run_ids directly target closed runs and bypass that age scan;
they can prune history younger than the bounded retention window. Forever
retention and archived-run protections still apply. The companion
retention_status() reports the current policy, cutoff and bounded eligible
list without deleting history. Review the retention procedure
before shortening a policy or submitting explicit targets.
activity_timeout_pass enforces start-to-close and schedule-to-close deadlines
on activity executions that have already passed their deadline. With no
filters the server processes any expired activity executions up to its scan
limit; pass execution_ids to target a specific list, or limit to bound a
single pass.
These methods do not raise on empty work — repeated calls during a quiet
period return the same passes / repaired / pruned counters with zero
deltas, so they are safe to call from cron-driven operator scripts.
Defining Workflows
Workflows are Python classes decorated with @workflow.defn. The run method is an ordinary synchronous function or a synchronous generator that yields durable commands to the server. Use the generator form when the workflow needs durable work; async def run is not supported.
from durable_workflow import workflow
@workflow.defn(name="order-processing")
class OrderWorkflow:
def run(self, ctx, *args):
order = args[0] if args else {}
# Schedule an activity and wait for the result
validated = yield ctx.schedule_activity(
"validate_order", [order]
)
# Start a timer (durable sleep)
yield ctx.start_timer(seconds=60)
# Schedule another activity
receipt = yield ctx.schedule_activity(
"process_payment", [validated]
)
return receipt
The name in @workflow.defn(name="...") is the type key used across all languages. It must be a plain string — not a Python module path or class reference.
Signals, Queries, and Updates
Signals are recorded in durable history and dispatched to Python handlers during workflow replay:
@workflow.defn(name="approval")
class ApprovalWorkflow:
def __init__(self) -> None:
self.approved = False
self.approved_by = None
@workflow.signal("approve")
def approve(self, by: str) -> None:
self.approved = True
self.approved_by = by
def run(self, ctx, *args):
ready = yield ctx.wait_condition(
lambda: self.approved, key="approval.approved", timeout=60,
)
return {
"status": "approved" if ready else "timed_out",
"approved": self.approved,
"approved_by": self.approved_by,
}
The wait belongs to the workflow, not an activity. Its predicate reads replayed signal-handler state; the worker does not hold an activity slot for the approval. The condition's timeout is separate from the workflow's finite run and execution deadlines. For external-job completion, use a short submit-and-return activity and a correlated callback.
Query and update receiver decorators are available so workflow classes can publish stable handler names. Python workers execute server-routed query tasks by replaying the committed workflow history and invoking the registered query handler against that replayed state. They also apply accepted updates delivered through workflow tasks:
@workflow.defn(name="approval")
class ApprovalWorkflow:
def __init__(self) -> None:
self.approved = False
@workflow.query("status")
def status(self) -> dict:
return {"approved": self.approved}
@workflow.update("set_approval")
def set_approval(self, approved: bool) -> dict:
self.approved = approved
return {"approved": self.approved}
@set_approval.validator
def validate_set_approval(self, approved: bool) -> None:
if not isinstance(approved, bool):
raise ValueError("approved must be boolean")
Python workers complete server-routed queries by returning a query-task result to the server. For a declared update validator, the worker instead receives a dedicated validation task, replays authoritative workflow state, and invokes only the validator. The server records no accepted update and dispatches no update handler until that validation succeeds. Validator-bearing workers refuse to register unless discovery advertises the synchronous pre-accept contract; rejection, missing or incompatible workers, worker loss, timeout, and fenced duplicate or stale completion remain explicit typed outcomes. PHP and Rust do not currently expose update-validator authoring APIs and advertise that absence in their workflow contracts rather than claiming validator parity.
Handler lifecycle
Released validation. Python SDK 2.6.2 introduced the handler guards below. Server-routed asynchronous queries and update validators remain supported. See SDK #177 for the corrected scope.
Every replay constructs a fresh workflow instance before executing run(). Signals already recorded at workflow start are applied after run() reaches its first yield, so constructor and pre-yield initialization happen first. Signal and update handlers mutate that replayed state. Queries inspect it without changing durable state; update validators inspect a disposable replayed state before acceptance. Put durable activities, timers and child workflow commands in run(), not in a handler.
| Handler | Supported form and failure outcome |
|---|---|
@workflow.signal | Synchronous function. An exception while applying the signal during replay fails the workflow run. |
@workflow.update | Synchronous function. A handler exception fails that update. |
@workflow.update_validator or @update_method.validator | Synchronous function or async def under the Worker. An exception rejects the update before acceptance. |
@workflow.query | Synchronous function or async def under the Worker. A handler exception fails the query, not the workflow run. |
The validation rejects coroutine functions for signals and updates, and rejects generator or async-generator functions for all decorated handler kinds, including queries and validators. It raises InvalidWorkflowDefinition at definition, registration or replay, including inherited handlers and offline tools. Synchronous signal and update wrappers cannot bypass the guards by returning asynchronous values. Returning generator or async-generator values is refused for every handler kind. These restrictions do not forbid the synchronous generator used by run().
For a server-routed query, the Worker explicitly awaits an asynchronous result under the original query deadline and claim/cancellation authority. Awaiting does not grant a fresh budget or permit a result after authority is lost. For pre-accept validation, the Worker also awaits an asynchronous validator result against disposable replayed state.
The local synchronous helpers cannot await. workflow.query_state() refuses an asynchronous result with QueryFailed, and workflow.validate_update() refuses it with UpdateRejected. They close returned coroutines instead of leaking them as operation results.
Workflow Context
The WorkflowContext passed to run provides deterministic operations:
| Method | Description |
|---|---|
ctx.schedule_activity(type, args) | Schedule an activity task, optionally with per-call retry and timeout options |
ctx.start_timer(seconds) | Durable sleep |
ctx.start_child_workflow(type, args) | Start a child workflow, optionally with per-call retry and workflow timeout options |
yield [command, [...]] | Join a nested deterministic activity, child-workflow, timer, or mixed group in input order |
yield ctx.select({key: command, ...}) | Resume with the first durably committed member while retaining handles for every non-winner |
ctx.saga() | Register and run reverse-order durable activity compensations |
ctx.side_effect(fn) | Capture a non-deterministic value |
ctx.get_version(change_id, min, max) | Safe workflow code versioning |
ctx.upsert_search_attributes(attrs) | Update search attributes |
ctx.continue_as_new(*args) | Restart the workflow with new input |
ctx.now() | Persisted workflow-start time, fixed for this run |
ctx.replay_now() | Advancing replay-safe UTC time: the run's start at entry, then each consumed durable step's recorded time |
ctx.random() | Seeded random generator |
ctx.uuid4() | Deterministic UUID |
ctx.logger | Logger that is silent during replay |
Before raising min in ctx.get_version(...) or removing a legacy branch, use
the version-usage check to
find open runs that still depend on it.
Entry compatibility and recovery
Released support. Python SDK 2.6.2 introduced the entry safeguard below, with routing and recovery support in Server 2.9.9. These are minimum feature versions; install current stable releases for security fixes.
Before calling run(ctx, *start_input), the worker binds the recorded arguments against the entry signature. A missing required argument or incompatible arity raises WorkflowEntryArgumentsMismatchError with reason workflow_entry_arguments_mismatch before the body runs. Defaults fill omitted arguments only. Python annotations remain unenforced: changing an annotation does not validate or convert recorded data. A TypeError raised by a successfully entered ordinary or generator body remains an application failure.
The worker automatically advertises workflow_entry_arguments_validation. With a qualified Server, the failed task becomes replay-blocked while the original run remains nonterminal. Older runtimes may retry that task instead; SDK-side refusal alone does not establish the complete recovery contract.
Inspect the exact run with await client.describe_workflow_run(workflow_id, run_id) and the selected-run debug endpoint. After restoring a compatible entry method, use the selected-run HTTP repair route in the recovery procedure. await client.repair_workflow(workflow_id) and handle.repair() instead resolve the instance's current run; neither accepts a run-id argument. Preserve the original input, history, namespace, queue, build and deadlines. For command changes, put ctx.get_version inside that compatible method and qualify cold replay through definition routing.
Workflow time and durable waits
During worker replay, ctx.now() returns the run's persisted start time.
It does not advance after an activity, timer, signal, or other durable step.
Repeated cold replays of the same history return that same start value. A
continue-as-new successor has its own start time; it does not keep the
predecessor's clock anchor.
The published Python implementation
uses WorkflowStarted.payload.timestamp when present, otherwise the canonical
WorkflowStarted.recorded_at. Missing or invalid persisted replay time is an
explicit clock error, not permission to read the worker's wall clock. This is
the fixed-start contract settled in SDK #146.
Wait for a durable operation instead of looping until ctx.now() changes:
from durable_workflow import workflow
@workflow.defn(name="wait_without_clock_loop")
class WaitWithoutClockLoop:
def run(self, ctx):
started_at = ctx.now()
yield ctx.start_timer(30)
return {
"run_started_at": started_at.isoformat(),
"after_wait": ctx.now().isoformat(),
}
The timer's durable completion resumes the workflow. Both returned timestamps
are the same start time. A loop such as while ctx.now() < deadline will not
become false merely because timers fire.
For a relative delay, yield ctx.start_timer(seconds) or ctx.sleep(seconds).
Use first-completion selection when work should
race a durable timer. Set execution_timeout_seconds and run_timeout_seconds
on client.start_workflow(...) when Server must enforce a workflow deadline,
including while workers are stopped. An HTTP or get_result() waiting timeout
only limits that local request; it does not replace the workflow's persisted
deadline. Keep an application deadline across continue-as-new by explicitly
passing it as input, without replacing the Server's original execution deadline.
For measured elapsed time within one activity invocation, read a monotonic
clock there and return the duration as the activity result. For an external
UTC deadline, let an activity check the authoritative external clock and
return its decision; workflow code replays that recorded result. Never compare
UTC timestamps with monotonic values, or subtract monotonic readings from
different processes or hosts. ctx.now() subtraction measures neither active
processing time nor elapsed wall time in Python.
Advancing replay clock
ctx.replay_now() is an additive UTC clock that moves with consumed history.
The Rust (WorkflowContext::replay_now()) and PHP service-mode
(WorkflowContext::replayNow()) SDKs provide the same clock.
- At ordinary workflow entry, before any durable step, it returns the run's canonical start (for a redrive successor, the original run's start). Server 3.6.0 and later sends that instant on each workflow
task as
replay_clock. History insertion, dispatch and lease times are never used instead. - After a supported durable step it returns that step's recorded time: activities, timers and child workflows in every SDK, signal and condition waits where the SDK supports them, and validated groups.
- Repeated reads between durable steps return the same value, and every replay of the run returns the same values. The clock is not guaranteed to be monotonic: a later proved boundary can carry an earlier recorded time.
In Python, a completed parallel group containing only plain durable timers uses the latest recorded firing time at millisecond precision. On a tie, the clock returns the first authored timer's recorded time at its original precision.
A deadline loop around a durable timer therefore terminates, and the difference between reads after two durable steps measures their recorded elapsed time:
from datetime import timedelta
from durable_workflow import workflow
@workflow.defn(name="poll_until_deadline")
class PollUntilDeadline:
def run(self, ctx):
deadline = ctx.replay_now() + timedelta(minutes=10)
while ctx.replay_now() < deadline:
yield ctx.schedule_activity("check_status", [])
yield ctx.start_timer(60)
return "deadline reached"
Where the SDK cannot prove the time, ctx.replay_now() raises
ReplayClockUnavailable rather than using any other time:
- at entry on a task without the carrier: Servers before 3.6.0, or a run whose start Server cannot prove;
- after inline and local operations, such as side effects, version markers, memo and search-attribute updates, and local activities. These stay unavailable until Core defines a persisted time for those steps;
- after other group modes and in cancellation scopes;
- in query handlers: always in PHP, and in Python and Rust unless the queried history ends at a supported scalar boundary (grouped boundaries give no query time).
An unhandled ReplayClockUnavailable fails the workflow like any other
exception.
Clock reads after inline steps are not portable
Inline steps are side effects, version markers, memo and search-attribute updates, and local activities. Until Core defines a persisted time for them, a clock read immediately after one is not portable across runners or mixed fleets:
- The service-mode SDKs (Python, Rust and the PHP SDK) report the clock as unavailable there.
- On the embedded engine,
Workflow::now()moves to the step's recorded time. - On the external-worker runner (the embedded package's worker-protocol
WorkflowFiberRunner, which external PHP workers use through the bridge), side effects, memo and search-attribute updates, and version markers behave alike: a first execution keeps the previous boundary time, while a replay reads the step's recorded time. For version markers this divergence is kept deliberately: superseding the pinnedrepeated-version-legacy-clock-boundariesreplay fixture would change the replay output of runs already in flight. - The read therefore differs whenever the first execution ran on the external-worker runner, including in a fleet of only external workers, and whenever a run may move between runners, because embedded and external first executions read different times.
Do not rely on a clock read immediately after an inline step: read the clock after the next durable step.
Python and Rust rebuild the workflow from history to answer a query or a
pre-accept update validation. If the rebuilt workflow reads the clock at entry
and the task has no replay_clock, which Servers before 3.30.0 never send
on those tasks, the query or validation fails rather than
answering with state the workflow task never computed. The PHP SDK never
rebuilds the workflow body for queries or updates.
Offline replay (replay(), Replayer.replay(), replay_history()) takes the task's object as replay_clock_entry. The in-memory WorkflowEnvironment supplies no carrier, so entry reads there are unavailable.
Release status. The entry value (all three SDKs) and the query and validation rule (Python and Rust) ship in Python SDK 2.24.0, Rust SDK 4.25.0 and PHP SDK 3.32.0 and need Server 3.6.0 or later. The
carrier on query and validation tasks ships in Server 3.30.0. The embedded
package's worker-protocol runner (WorkflowFiberRunner) accepts the carrier
from Workflow 2.42.0: once the code that builds the runner passes it,
Workflow::now() at entry returns the run's canonical start.
Earlier SDK releases report entry as unavailable.
Upgrading. A running workflow whose code branches on the entry read being
unavailable, for example by catching ReplayClockUnavailable, takes the other branch on replay after the upgrade, during a rolling deploy that mixes old and new workers, or after a Server upgrade to 3.6.0. Put such code behind ctx.get_version(...)
or drain those runs first. ctx.now() keeps its fixed start time, so runs that
read it replay unchanged.
Clock differences across SDKs
These APIs are deterministic where provided, but they do not share one clock:
| Authoring surface | Repeated calls and durable steps | Continue-as-new successor |
|---|---|---|
Python ctx.now() | Fixed to this run's persisted start time, including after a timer or activity. | Uses the successor run's start anchor. |
Python ctx.replay_now() | Starts at the run's start and advances to each supported consumed step's recorded time. See Advancing replay clock. | Starts at the successor run's own start. |
Embedded PHP Workflow::now() | Starts at the run's start time and advances to the recorded event time consumed by replay. See workflow time. | Starts with the new run, then advances through that run's consumed events. |
PHP service-mode WorkflowContext::replayNow() | Same semantics as Python ctx.replay_now(). | Starts at the successor run's own start. |
Rust service-mode WorkflowContext::replay_now() | Same semantics as Python ctx.replay_now(). | Starts at the successor run's own start. |
The service-mode clocks share one contract. Embedded Workflow::now() also moves to the recorded time of an inline step such as a
side effect, where the service-mode clocks report the time as unavailable, so
such a read is not portable (see
Clock reads after inline steps are not portable). Port
the durable operation and deadline policy rather than assuming that every clock
helper behaves identically.
Order emitted events with sequence values
A workflow timestamp is not an event ordering key. Python events stamped with
ctx.now() can all have the same timestamp. Use
Workflow Stream offsets, the applicable
embedded outbox sequence, or a deterministic
workflow-local counter. Pair a run-local counter with run_id; carry it through
continue-as-new explicitly if the application needs one logical sequence.
Independent streams or runs do not gain a global order from their timestamps.
Repeated ctx.replay_now() reads between durable steps also return one value, so do not use it to order events either.
Activity Retries and Timeouts
Use ActivityRetryPolicy and timeout keyword arguments on ctx.schedule_activity(...)
when one activity call needs a different retry budget or deadline than the
default single-attempt behavior:
from durable_workflow import ActivityRetryPolicy
receipt = yield ctx.schedule_activity(
"process_payment",
[validated],
retry_policy=ActivityRetryPolicy.exponential(
max_attempts=4,
initial_interval_seconds=1,
backoff_coefficient=2,
maximum_interval_seconds=30,
non_retryable_error_types=["ValidationError"],
),
start_to_close_timeout=120,
schedule_to_close_timeout=300,
heartbeat_timeout=15,
)
ActivityRetryPolicy.exponential(...) (Python SDK 2.18.0 and later) generates
the delays with a 60-second default maximum interval. For nine total attempts,
the retries wait 1, 2, 4, 8, 16, 32, 60, 60 seconds. Pass a finite
maximum_interval_seconds to choose another cap; None, infinities and NaN
are refused. The ActivityRetryPolicy(...) constructor keeps its earlier
behavior: omitting maximum_interval_seconds, or passing None, applies no
cap. Generated delays are recorded with the scheduled activity, so histories
recorded by earlier SDK versions replay with their original delays. For a local
activity, pass ActivityRetryPolicy.exponential(...).to_dict() as
retry_policy.
start_to_close_timeout caps one attempt after a worker starts it.
schedule_to_close_timeout caps the whole activity execution across all
attempts. heartbeat_timeout requires long-running activities to call
activity.context().heartbeat(...) before the interval expires. The retry
policy is snapped onto the durable activity execution when it is scheduled, so
later deploys do not change already-running attempts.
ctx.schedule_activity(..., queue="payments-v2") selects the queue for a newly scheduled activity. An already scheduled execution, its retries and repaired tasks keep their recorded queue. Changing the worker's task_queue does not reroute them or change an existing run's workflow queue. Keep compatible old-queue workers until the remaining dependencies are accounted for. Follow recorded activity queues and recovery.
Determinism Rules
Workflow code is replayed from history. It must not perform any non-deterministic operations directly:
- No I/O (HTTP calls, file reads, database queries)
- No
datetime.now()ortime.time()— usectx.now()only for the fixed run-start anchor; use durable timers for waits - No
random.random()— usectx.random() - No
uuid.uuid4()— usectx.uuid4()
All non-determinism must flow through the context or be captured with ctx.side_effect().
Finally blocks and context managers
SDK 2.12.2 and later. A suspension is not a workflow exit. Python may execute
finally blocks and with exits while disposing of a suspended replay. The SDK
discards durable commands and ctx.logger messages produced only by disposal;
queries see the state at the pending command. Actual completion, failure or
cooperative cancellation runs cleanup through the normal history, and cold
replay uses its recorded results.
Keep direct I/O out of these hooks. Schedule external cleanup as an Activity
with yield ctx.schedule_activity(...), and wrap that Activity in
with ctx.cancellation_shield(): if it must finish after cooperative cancellation.
Fan-Out
Yield a list of commands to run them concurrently:
@workflow.defn(name="fan-out-example")
class FanOutWorkflow:
def run(self, ctx, *args):
items = args[0]
# Nested lists may mix activities, children, and timers.
results = yield [
ctx.schedule_activity("process_item", [items[0]]),
[
ctx.start_child_workflow("process-batch", [items[1:]]),
ctx.start_timer(1),
],
]
return results # same nested shape and input order
The worker emits one ordinary command per durable leaf and attaches the shared
stable parallel_group_* fields plus the full outer-to-inner path. History can
close members in any order; replay still binds results and failures by durable
input position. Exact duplicate terminal delivery is ignored. Pending history
after a worker restart and fully completed history reconstruct the same group
without rescheduling completed work.
First-completion selection
Use ctx.select() when independent work should start together but the workflow
can make progress after one member completes:
selected = yield ctx.select({
"resolver": ctx.schedule_activity("resolve-request", [request_id]),
"input": ctx.wait_condition(lambda: self.resolution is not None, key="resolution-ready"),
"deadline": ctx.start_timer(2),
})
if selected.key == "deadline":
yield selected.handles["resolver"].cancel()
return {"status": "timed_out"}
yield selected.handles["deadline"].cancel()
return {"status": "resolved", "value": selected.result()}
The SelectionResult includes the winner's stable key, input index, operation
kind and durable identity, typed outcome, and all DurableOperationHandle
instances. yield handle.await_result() waits for a non-winner later; yield handle.cancel() records an explicit void request. Only committed
SelectionOperationCancelled history proves cancellation won; replay advances
past an unmarked request, and await_result() still returns a completion that
committed first. Replay consumes the persisted winner even if later history
contains another completion first. Duplicate and out-of-order external
delivery cannot replace an already committed winner.
Saga Compensation
def forward(saga):
flight = yield ctx.schedule_activity("trip.reserve-flight", [])
saga.add_compensation("trip.cancel-flight", [flight])
hotel = yield ctx.schedule_activity("trip.reserve-hotel", [])
saga.add_compensation("trip.cancel-hotel", [hotel])
yield ctx.schedule_activity("trip.charge", [])
return {"status": "booked"}
return (yield from ctx.saga().run(forward))
The saga executes ordinary activity commands sequentially in reverse
registration order after a failure. Terminal cancel_workflow does not
resume the workflow to run compensations. The helper stops on the
first compensation failure. SagaCompensationFailed preserves the initiating
failure, compensation failure, compensation activity type, and deterministic
registration order as structured diagnostics.
Child Workflows
from durable_workflow import ChildWorkflowRetryPolicy
result = yield ctx.start_child_workflow(
"child-workflow-type",
[{"input": "data"}],
task_queue="child-queue",
parent_close_policy="terminate",
retry_policy=ChildWorkflowRetryPolicy(
max_attempts=3,
initial_interval_seconds=2,
backoff_coefficient=2,
non_retryable_error_types=["ValidationError"],
),
execution_timeout_seconds=600,
run_timeout_seconds=120,
)
execution_timeout_seconds caps the logical child workflow execution across
retries and continue-as-new runs. run_timeout_seconds caps each child run
attempt. Retry backoff is applied after a child run fails; invalid child start
commands are protocol errors and are not retried as child attempts.
Continue-as-New
For long-running workflows, use continue-as-new to reset the history:
from durable_workflow import ContinueAsNew
@workflow.defn(name="polling-workflow")
class PollingWorkflow:
def run(self, ctx, *args):
iteration = args[0] if args else 0
result = yield ctx.schedule_activity("poll_source", [])
if result.get("done"):
return result
# Continue with incremented iteration
return ContinueAsNew(arguments=[iteration + 1])
Defining Activities
Activities are async Python functions decorated with @activity.defn. Unlike workflows, activities can perform I/O freely.
from durable_workflow import activity
@activity.defn(name="send_email")
async def send_email(to: str, subject: str, body: str) -> dict:
# Activities can do I/O: HTTP calls, database queries, etc.
response = await some_email_client.send(to=to, subject=subject, body=body)
return {"message_id": response.id, "sent": True}
The name is the type key shared across languages. A PHP workflow can schedule an activity named "send_email" and a Python worker will pick it up, and vice versa.
Activity Context
Inside an activity, access execution metadata and heartbeat via activity.context():
@activity.defn(name="long_running_task")
async def long_running_task(items: list) -> dict:
ctx = activity.context()
print(f"Attempt #{ctx.info.attempt_number}")
print(f"Task queue: {ctx.info.task_queue}")
for i, item in enumerate(items):
# Check for cancellation
if ctx.is_cancelled:
return {"partial": True, "processed": i}
await process(item)
# Heartbeat to keep the task alive
await ctx.heartbeat({"progress": i + 1, "total": len(items)})
return {"processed": len(items)}
Logical activity execution identity
Python SDK 2.15.0 and later expose the read-only optional
activity.context().info.activity_execution_id. It identifies one logical
activity execution across retries. info.workflow_id and info.run_id
identify its owning workflow instance and run. Use the logical execution ID
for an external service's idempotency key. Task IDs and attempt IDs change
on retry and must not replace it.
Missing, invalid or blank execution IDs remain None, including on older
claims. Nonblank received strings retain their exact spelling. If an operation
requires this key, refuse it before performing the external effect when the
ID is unavailable. Session-routed activities use their ordinary claim identity.
Prepared local activities use their validated admission receipt's execution
ID. Inline local activities have no admitted execution ID and expose None,
while retaining parent workflow/run IDs. Server-free mocks do not create
Server activity identity.
Use the SDK's idempotency-key helper and activity identity guide for the service-mode example. Give different external operations stable operation suffixes, and ensure the downstream service honors the key. An execution ID is public metadata, not proof of a live claim or authorization for an external effect. Keep credentials, current-attempt fencing and outcome reconciliation. The published-package ordinary retry receipt binds Python SDK 2.17.2 to native Server 3.9.2. It proves a stable logical execution/workflow/run identity and distinct task/attempt IDs across attempts 1 and 2. Local and session behavior retain their separately defined scope. See execution guarantees for the shared idempotency contract.
Received retry policy and original attempt
Requires Python SDK 2.12.0 or later. info = activity.context().info
exposes the original attempt_number and an immutable retry_info snapshot.
info.retry_info.policy() returns a fresh copy, including unknown fields.
A missing policy and root retry_policy: null both have an unknown retry
limit. policy_provided distinguishes them (False and True).
A received policy object with max_attempts: null explicitly means unlimited:
is_unlimited() is True, while max_attempts() remains None.
For a positive integer limit, max_attempts() returns that limit and
is_last_configured_attempt() compares it with the original attempt.
An unknown limit returns None for that last-attempt check.
This check describes the received budget. Cancellation, deadlines, workflow closure and non-retryable failures can end execution earlier. Ordinary and supervised remote callbacks receive the original snapshot. Inline and prepared local callbacks do not inherit the parent's remote retry policy.
from durable_workflow import activity
@activity.defn(name="inspect_retry_budget")
async def inspect_retry_budget() -> dict:
retry = activity.context().info.retry_info
return {
"attempt": retry.attempt_number,
"max_attempts": retry.max_attempts(),
"last_configured_attempt": retry.is_last_configured_attempt(),
}
Signal the originating run
info.workflow_id and info.run_id preserve the original received identities
and can be None. Local callbacks inherit only known parent identities.
The SDK never substitutes a task ID. Pass an authorized application client
to this helper; the handle pins the original run:
from durable_workflow import Client, activity
async def signal_originating_run(client: Client, business_event_id: str) -> dict:
info = activity.context().info
if info.workflow_id is None or info.run_id is None:
raise RuntimeError("This activity has no known originating workflow run")
handle = client.get_workflow_handle(info.workflow_id, run_id=info.run_id)
return await handle.signal(
"payment_recorded",
[business_event_id],
request_id=f"payment-recorded:{business_event_id}",
)
Keep business_event_id stable across retries, and keep the whole key within
255 Unicode characters. On native Server 3.9.2 and later, a retry with the same
key and arguments returns the original receipt instead of recording another
signal. The same key with different arguments raises SignalFailed with
reason == "request_id_conflict", and nothing is recorded. Older Servers do
not retain these keys for every signal path. Make business-event processing
idempotent and reconcile uncertain delivery. See the
canonical activity reference
and signal and update command keys.
Heartbeat requests
For remote activities, call await ctx.heartbeat(...) during bounded work, well before heartbeat_timeout. Its first window starts at runtime claim, before Python receives the task. Each recorded heartbeat uses the runtime's recording time, not the time its response reaches Python. Pauses, request latency and transport retries consume the budget. Follow the cadence guidance.
The direct Client.heartbeat_activity_task() call uses the configured TransportRetryPolicy. After that policy, transport errors can propagate as httpx.TransportError, and HTTP refusals become mapped SDK exceptions such as ServerError. This is HTTP retry behavior, not another activity attempt or more heartbeat time. ActivityContext.heartbeat() can raise ActivityCancelled when the worker tells the activity to stop. When cooperative process supervision is enabled, failed heartbeat or ownership checks can stop or abandon the callback. Do not assume every failure returns to a catch block or that cleanup code always runs.
Do not suppress heartbeat/ownership failures and continue external work or report success.
Resume from an acknowledged heartbeat
When the worker and Server support heartbeat resume version 1, a heartbeat with application details can commit a checkpoint for the logical activity execution. At the start of a retry, use ctx.last_heartbeat_checkpoint() to read the fixed snapshot and its original acknowledgement time. A supported claim with no recorded details returns None. A context without the contract reports ActivityHeartbeatResumeUnavailable.
If a heartbeat acknowledgement is lost, resume from what Server committed. Activity retry policy and deadlines still apply. Use stable idempotency keys and check the provider’s receipt before repeating an external operation. The external operation has its own commit boundary. Activity retries and idempotency. SDK checkpoint example.
When an external operation requires exclusive ownership, have the provider reject stale fencing tokens.
Heartbeat refusals and callback exit
In Python SDK 2.9.0, ctx.heartbeat() and ctx.heartbeat_sync() raise ActivityLeaseAuthorityLost when a heartbeat response says can_continue=false. The exception retains the original Server reason, such as activity_deadline_elapsed, attempt_closed or run_closed. A cancellation request instead raises ActivityCancelled. See the published SDK qualification.
The heartbeat that observes the refusal is a cooperative exit point. Stop external work and propagate the exception; do not catch it and return success. Code that never heartbeats can keep running until it returns. A helper heartbeat cannot forcibly interrupt a blocked library call: use finite I/O deadlines and the call's own cancellation mechanism.
The physical activity slot stays occupied until the callback/thread exits. Losing attempt authority does not make that thread available for replacement work, and the worker does not publish a late result for the closed attempt. Runtime timeout enforcement and stopping application code are separate boundaries. See activity timeouts and the helper-thread guidance.
Non-Retryable Errors
Raise NonRetryableError to fail the activity without retries:
from durable_workflow import NonRetryableError
@activity.defn(name="validate")
async def validate(data: dict) -> dict:
if "required_field" not in data:
raise NonRetryableError("Missing required_field")
return data
Terminal business errors
non_retryable_error_types matches the outermost exception's short name (exception_type) or fully qualified class name (exception_class) exactly. For example, ValidationError does not match SpecificValidationError, even if it is a subclass. A qualified entry such as app.errors.ValidationError must match that recorded class name. The policy does not walk subclasses or exception causes. raise RuntimeError(...) from NonRetryableError(...) reports the outer RuntimeError and can still be retried.
When converting a known business exception, make NonRetryableError the outer exception and keep the original Python cause with cause=error. This marks the activity failure non-retryable without relying on a name list. It does not make the entire workflow fail automatically: workflow code receives ActivityFailed when the activity has no retries left, either because it is non-retryable or because its retry budget is exhausted.
from durable_workflow import ActivityFailed, NonRetryableError, activity, workflow
@activity.defn(name="validate_order")
async def validate_order(order: dict) -> dict:
try:
if order.get("amount", 0) <= 0:
raise ValueError("amount must be positive")
except ValueError as error:
raise NonRetryableError("Order validation failed", cause=error) from error
return order
@workflow.defn(name="place_order")
class PlaceOrderWorkflow:
def run(self, ctx, order: dict):
try:
yield ctx.schedule_activity("validate_order", [order])
except ActivityFailed as failure:
if (
failure.activity_type != "validate_order"
or failure.exception_type != "NonRetryableError"
or not failure.non_retryable
):
raise
return {"order_id": order["id"], "status": "rejected"}
return {"order_id": order["id"], "status": "accepted"}
For input {"id": "order-123", "amount": 0}, the activity fails once and the workflow completes normally with status: rejected. Handle only the business failure you intend to handle; the example rethrows other failures. Continue with further durable steps if your business flow requires them. An uncaught ActivityFailed fails the workflow. Preserving a Python cause does not mean retry policy examines that chain or that every SDK exposes an identical cause object.
Synchronous and asynchronous activities
Ordinary def remote activities run in a dedicated thread pool bounded by
max_concurrent_activity_tasks. Each callback keeps its activity context.
Polling, workflow tasks and worker heartbeats can progress while a callback
blocks. async def activities run on the event loop and must use nonblocking
I/O; a blocking call inside an async function still stalls that loop.
From a synchronous callback, call
activity.context().heartbeat_sync(details) to send progress through the owner
event loop with the original task, attempt and worker identity. Async callbacks
use await activity.context().heartbeat(details).
Cancelling an await does not stop a running Python thread. Its physical slot
stays occupied until the callback returns, and a late return does not submit a
result after cancellation. If a callback is still running after
shutdown_timeout, shutdown raises RuntimeError and retains the worker
registration. Set timeouts on downstream I/O and use separately isolated worker
processes for CPU-heavy work. See the SDK's
execution and shutdown reference.
Heartbeating from a helper thread
For one long blocking call, capture ctx = activity.context() inside the
synchronous activity and pass it to the helper. A new thread does not
automatically inherit the activity context. Use ctx.heartbeat_sync(details)
from the helper, never from the worker event loop. See the SDK's
complete helper-thread recipe.
Stop and join the helper when the call returns or raises. If a heartbeat raises
ActivityCancelled or ActivityLeaseAuthorityLost, signal cooperative
cancellation to the call and re-raise the original exception before returning
a result. A helper exception cannot forcibly interrupt a blocked library call.
Give that call its own finite deadline and cancellation mechanism, and bound
heartbeat request timeouts and retries. Helper heartbeats can otherwise keep
reporting liveness while the work is hung.
Worker
The Worker registers with the server, polls for tasks, and dispatches them to your workflow and activity implementations.
For independent workflow and activity processes, follow the Python SDK's runnable process-split guide. It covers separate registrations, shared or distinct task queues, retries, role-specific slots and graceful shutdown.
from durable_workflow import Client, Worker
async with Client("http://localhost:8080", token="secret") as client:
worker = Worker(
client,
task_queue="default",
workflows=[GreeterWorkflow, OrderWorkflow],
activities=[greet, send_email, validate],
max_concurrent_workflow_tasks=10,
max_concurrent_activity_tasks=10,
)
await worker.run() # blocks until worker.stop() is called
For smoke tests and one-workflow examples,
await worker.run_until(workflow_id="...", timeout=60.0) registers the same
worker and drives one workflow to a terminal state with sequential polling. Use
run() for deployed workers that should keep polling.
| Parameter | Default | Description |
|---|---|---|
task_queue | required | The task queue to poll |
workflows | () | Workflow classes to register |
activities | () | Activity functions to register |
worker_id | auto-generated | Unique worker identifier |
poll_timeout | 35.0 | Long-poll timeout in seconds |
max_concurrent_workflow_tasks | 10 | Max parallel workflow tasks |
max_concurrent_activity_tasks | 10 | Max parallel activity tasks |
shutdown_timeout | 30.0 | Seconds to drain in-flight tasks on stop |
metrics | client's recorder | Optional metrics recorder for poll and task counters/histograms |
interceptors | () | Ordered worker task wrappers for instrumentation, tracing, and policy hooks |
The two max_concurrent_* values are advertised to the server during worker
registration and appear in task queue admission diagnostics. Treat them as the
worker's local capacity. Use server-side
task queue admission caps when a
namespace, queue, or downstream budget group needs a hard shared budget across
multiple workers.
Worker API Reference
| Method | Returns | Use when |
|---|---|---|
await worker.run() | None | Long-running process supervised by systemd, Docker, Kubernetes, or a local dev shell. Registers once, then polls workflow, activity, and query tasks until stopped or cancelled. |
await worker.run_until(workflow_id=..., timeout=60.0, poll_interval=0.5) | WorkflowExecution | Smoke tests and examples that start one workflow and want the same process to drive it until a terminal status. |
await worker.stop() | None | Cooperative shutdown. Stops new polls and drains in-flight tasks up to shutdown_timeout. |
run() validates server compatibility before it starts polling. The worker
requires the server's published control_plane.version,
control_plane.request_contract, worker_protocol.version, and
auth_composition_contract to match the SDK's supported contract versions. A
missing or incompatible manifest raises RuntimeError during registration, so
supervisors fail fast instead of running a worker that cannot safely complete
tasks.
run_until() uses the same registration and dispatch path as run(), but
polls sequentially and returns the final WorkflowExecution for the named
workflow. It raises TimeoutError when the workflow is still non-terminal
after the timeout.
During task execution:
- unknown workflow or activity types are reported back to the server as task failures, not hidden in local logs
NonRetryableErrormarks activity failures as non-retryableActivityCancelledpropagates as a cancellation outcome- unhandled activity exceptions are reported as retryable failures unless the activity retry policy or server deadline says otherwise
- query handler exceptions are reported as
QueryFailed
Worker Interceptors
Pass interceptors=[...] when worker execution needs tracing, metrics, audit
logging, or local policy checks around tasks. Interceptors run in the order
provided; the first interceptor is the outer wrapper and should call next to
continue the chain.
from durable_workflow import (
ActivityInterceptorContext,
PassthroughWorkerInterceptor,
)
class AuditInterceptor(PassthroughWorkerInterceptor):
async def execute_activity(self, context: ActivityInterceptorContext, next):
print("activity started", context.activity_type, context.worker_id)
return await next(context)
worker = Worker(
client,
task_queue="orders",
workflows=[OrderWorkflow],
activities=[charge_card],
interceptors=[AuditInterceptor()],
)
| Hook | Context fields | next returns |
|---|---|---|
execute_workflow_task(context, next) | worker_id, task_queue, task | workflow commands, or None |
execute_activity(context, next) | worker_id, task_queue, task, activity_type, args | decoded activity result |
execute_query_task(context, next) | worker_id, task_queue, task | encoded query result string |
Use PassthroughWorkerInterceptor as a base class when you only need one
hook. Implement WorkerInterceptor directly when you want type checkers to
force all hooks to be present.
Worker sessions
Use WorkerSessionOptions when sequential remote activities need one worker's local resource. This example is checked with Python SDK 2.9.0 and Server 2.11.0; see the portable affinity version requirements. Use a unique job_id for each workflow run so a new run does not reuse a terminal session id.
The cache below is disposable. Each activity can rebuild it from the durable business input, so a replacement holder does not depend on a previous process's memory:
from durable_workflow import ActivityFailed, WorkerSessionOptions, activity, workflow
# Disposable process-local state. Rebuild it from durable business input.
documents = {}
def ensure_document(job_id):
rebuilt = job_id not in documents
if rebuilt:
documents[job_id] = {"text": "document:" + job_id}
return documents[job_id], rebuilt
@activity.defn(name="prepare_document")
async def prepare_document(job_id):
document, rebuilt = ensure_document(job_id)
return {"holder": activity.context().info.worker_id, "rebuilt": rebuilt}
@activity.defn(name="inspect_document")
async def inspect_document(job_id):
document, rebuilt = ensure_document(job_id)
return {"holder": activity.context().info.worker_id,
"rebuilt": rebuilt, "text": document["text"]}
@workflow.defn(name="inspect_document_workflow")
class InspectDocumentWorkflow:
def run(self, ctx, job_id):
session = WorkerSessionOptions(
session_id="documents:" + job_id,
requirements=("fs:documents",),
lease_seconds=5,
ttl_seconds=60,
max_concurrent_activities=1,
allow_reacquire_after_failure=True,
)
options = dict(worker_session=session,
start_to_close_timeout=5,
schedule_to_start_timeout=12,
schedule_to_close_timeout=20,
retry_policy={"max_attempts": 1})
try:
prepared = yield ctx.schedule_activity("prepare_document", [job_id], **options)
inspected = yield ctx.schedule_activity("inspect_document", [job_id], **options)
except ActivityFailed as error:
if error.activity_type not in {"prepare_document", "inspect_document"}:
raise
return {"status": "resource_unavailable", "job_id": job_id}
return {"status": "inspected", "prepared": prepared, "inspected": inspected}
Register both activities and declare the required capability on the worker:
from durable_workflow import Worker
worker = Worker(
client,
task_queue="documents",
workflows=[InspectDocumentWorkflow],
activities=[prepare_document, inspect_document],
capabilities=["fs:documents"],
)
await worker.run()
Start inspect_document_workflow on the documents task queue with [job_id] as input. The first activity lazily acquires the session; the second uses the same holder while its lease remains valid. max_concurrent_activities=1 bounds simultaneous activities in that session. Queue routing and capability checks still apply. These short leases/timeouts demonstrate bounded behavior; size them for your real work and heartbeat well before expiry.
After holder loss or lease expiry, allow_reacquire_after_failure=True permits another eligible worker to reacquire the session. Its local resource must be rebuilt. Already committed activity results replay, while uncommitted effects can repeat: use durable resource identifiers and idempotent external operations. A session does not migrate memory, files or open handles.
Explicit close and absolute TTL expiry are terminal for that session id. A later activity cannot reopen it and can remain unadmitted until its configured timeout is enforced. The native Server enforces activity timeouts in its own maintenance loop, which the published image enables. On PHP Server 2.x, run the scheduler/maintenance role from the PHP deployment recipe; an API-only PHP process is insufficient for timeout enforcement. The workflow handles exhausted activity failure with ActivityFailed and returns resource_unavailable; the application can then choose a fresh session and rebuild from the business input. This example does not promise an immediate session-closed exception or automatically reopen a terminal session.
For explicit holder lifecycle operations, use worker.worker_session(options) and its create(), renew() and close(reason) methods. Graceful worker shutdown closes its held sessions. See the shared lifecycle contract.
Logging
The SDK uses Python's standard logging module with structured logger names:
| Logger Name | What It Logs |
|---|---|
durable_workflow.worker | Worker registration, task polls, task completion, errors |
durable_workflow.workflow.replay | Workflow replay events (silent during replay) |
Configuring Logging
Set the logging level in your application's entry point:
import logging
# Show INFO-level worker events (registration, task completion)
logging.basicConfig(level=logging.INFO)
# Or configure specific loggers
logging.getLogger("durable_workflow.worker").setLevel(logging.DEBUG)
logging.getLogger("durable_workflow.workflow.replay").setLevel(logging.INFO)
Log Levels
- INFO: Worker registration, task starts/completions, workflow completion
- DEBUG: Detailed task payloads (truncated), poll cycles
- WARNING: Retryable errors (failed API calls, unknown workflow types)
- ERROR: Non-retryable failures, replay crashes
Replay-Aware Logging
Inside workflows, use ctx.logger for replay-aware logging:
@workflow.defn(name="order_processor")
class OrderProcessor:
def run(self, ctx, order_id: str):
ctx.logger.info("Processing order %s", order_id) # Only logs during execution, not replay
result = yield ctx.schedule_activity("process_order", [order_id])
ctx.logger.info("Order processed: %s", result)
return result
Log statements are silent during replay to avoid duplicate log spam when workflows recover or continue execution.
Structured Logging
For JSON-structured logs, configure your application's root logger with a JSON formatter:
import logging
import json
class JSONFormatter(logging.Formatter):
def format(self, record):
return json.dumps({
"timestamp": self.formatTime(record),
"level": record.levelname,
"logger": record.name,
"message": record.getMessage(),
})
handler = logging.StreamHandler()
handler.setFormatter(JSONFormatter())
logging.getLogger("durable_workflow").addHandler(handler)
logging.getLogger("durable_workflow").setLevel(logging.INFO)
Metrics
Client(metrics=...) and Worker(metrics=...) accept any recorder with two methods:
def increment(name: str, value: float = 1.0, tags: dict[str, str] | None = None) -> None: ...
def record(name: str, value: float, tags: dict[str, str] | None = None) -> None: ...
The default recorder is no-op, so metrics collection has no setup cost. Use InMemoryMetrics for tests or custom exporter loops:
from durable_workflow import Client, InMemoryMetrics, Worker
metrics = InMemoryMetrics()
async with Client("http://localhost:8080", token="secret", metrics=metrics) as client:
worker = Worker(client, task_queue="default", workflows=[GreeterWorkflow], activities=[greet])
For Prometheus, install the optional extra and pass PrometheusMetrics:
pip install 'durable-workflow[prometheus]'
from durable_workflow import Client, PrometheusMetrics
metrics = PrometheusMetrics()
client = Client("http://localhost:8080", token="secret", metrics=metrics)
The SDK records:
| Metric | Type | Tags |
|---|---|---|
durable_workflow_client_requests | Counter | method, route, plane, status_code, outcome |
durable_workflow_client_request_duration_seconds | Histogram | method, route, plane, status_code, outcome |
durable_workflow_worker_polls | Counter | task_kind, task_queue, outcome |
durable_workflow_worker_poll_duration_seconds | Histogram | task_kind, task_queue, outcome |
durable_workflow_worker_tasks | Counter | task_kind, task_queue, outcome |
durable_workflow_worker_task_duration_seconds | Histogram | task_kind, task_queue, outcome |
The separate durable_workflow_worker_task_schedule_to_start_seconds histogram
is not in the selected stable package. Its SDK source hook
is merged but unreleased, and Server publication plus installed/exported
acceptance remain pending in #188.
The six metrics above do not provide that measurement. Use the
operator guide's interim queue-age signals
while that delivery is pending.
Schedules
Create and manage scheduled workflows through the client:
from durable_workflow import ScheduleSpec, ScheduleAction
# Create a schedule
handle = await client.create_schedule(
schedule_id="hourly-report",
spec=ScheduleSpec(cron_expressions=["0 * * * *"]),
action=ScheduleAction(
workflow_type="generate-report",
task_queue="default",
input=[{"format": "pdf"}],
),
overlap_policy="skip",
jitter_seconds=30,
)
# List all schedules
schedule_list = await client.list_schedules()
# Describe a schedule
desc = await handle.describe()
print(f"Next fire: {desc.next_fire_at}")
print(f"Total fires: {desc.fires_count}")
# Pause and resume
await handle.pause(note="maintenance window")
await handle.resume(note="maintenance complete")
# Trigger immediately
result = await handle.trigger()
# Backfill missed runs
backfill = await handle.backfill(
start_time="2024-01-01T00:00:00Z",
end_time="2024-01-02T00:00:00Z",
)
# Update the schedule
await handle.update(
spec=ScheduleSpec(cron_expressions=["*/30 * * * *"]),
note="Changed to every 30 minutes",
)
# Read the audit history stream (paused/resumed/triggered/updated events)
page = await handle.history(limit=100)
for event in page.events:
print(event.sequence, event.event_type, event.recorded_at)
while page.has_more and page.next_cursor is not None:
page = await handle.history(limit=100, after_sequence=page.next_cursor)
for event in page.events:
print(event.sequence, event.event_type, event.recorded_at)
# Or iterate every event without managing cursors
async for event in handle.iter_history():
print(event.sequence, event.event_type)
# Delete the schedule (history survives the delete for audit)
await handle.delete()
Synchronous Client
For scripts, notebooks, and non-async contexts, use the synchronous wrapper:
import uuid
from durable_workflow.sync import Client as SyncClient
client = SyncClient("http://localhost:8080", token="secret")
workflow_id = f"sync-greeting-{uuid.uuid4().hex}"
handle = client.start_workflow(
workflow_type="greeter",
task_queue="default",
workflow_id=workflow_id,
input=["world"],
)
execution = client.describe_workflow(workflow_id)
print(execution.status)
The synchronous client mirrors the async API but wraps each call with asyncio.run.
Error Handling
The SDK maps server error codes to typed Python exceptions:
| Exception | When |
|---|---|
WorkflowNotFound | Workflow ID does not exist |
WorkflowAlreadyStarted | Duplicate workflow ID with conflicting policy |
WorkflowFailed | Workflow execution failed |
WorkflowCancelled | Workflow was cancelled (inherits from BaseException, not Exception) |
WorkflowTerminated | Workflow was terminated |
ActivityCancelled | Activity was cancelled during execution (inherits from BaseException, not Exception) |
ChildWorkflowFailed | A child workflow failed |
QueryFailed | Query handler returned an error |
UpdateRejected | Update was rejected by the workflow |
ScheduleNotFound | Schedule ID does not exist |
ScheduleAlreadyExists | Duplicate schedule ID |
NamespaceNotFound | Namespace does not exist |
InvalidArgument | Invalid request parameters |
Unauthorized | Authentication failed |
ServerError | Server returned an unexpected error |
from durable_workflow import WorkflowNotFound, WorkflowAlreadyStarted
try:
handle = await client.start_workflow(
workflow_type="greeter",
task_queue="default",
workflow_id="existing-id",
input=["world"],
)
except WorkflowAlreadyStarted:
handle = client.get_workflow_handle("existing-id")
except WorkflowNotFound:
print("Workflow type not registered on any worker")
Cancellation is intentionally uncatchable by except Exception
WorkflowCancelled and ActivityCancelled inherit from BaseException, not Exception. A generic except Exception: block in an activity body — or in code that awaits client.get_result() — will not catch them. This is deliberate: cancellation is a control-plane outcome, and silently swallowing it in a catch-all would let an activity report success after its workflow asked it to stop.
If an activity needs local cleanup when its task is cancelled, catch the class by name and re-raise. This does not resume the closed workflow for compensation:
from durable_workflow import ActivityCancelled, activity
@activity.defn(name="long_task")
async def long_task(items: list) -> dict:
ctx = activity.context()
try:
for i, item in enumerate(items):
await process(item)
await ctx.heartbeat({"progress": i + 1})
return {"done": True}
except ActivityCancelled:
await cleanup_partial_state()
raise
This mirrors the standard-library precedent set by asyncio.CancelledError and KeyboardInterrupt.
Testing
Workflow authors should be able to test workflow code without a running server
or worker. The durable_workflow.testing module ships two entry points:
WorkflowEnvironmentdrives a workflow to completion in a single Python process against user-registered activity mocks.replay_historyandreplay_history_filereplay a captured production history against current workflow code and raise on any non-determinism.
Both entry points reuse the same durable_workflow.workflow.replay machinery
the worker uses at runtime, so a workflow that passes its test harness behaves
the same way under a real worker.
WorkflowEnvironment
WorkflowEnvironment dispatches yielded workflow commands against registered
mocks and auto-fires timers, side effects, and search-attribute upserts. Tests
do not need a real clock, Redis, or server.
from durable_workflow import workflow
from durable_workflow.testing import WorkflowEnvironment
@workflow.defn(name="greeter")
class Greeter:
def run(self, ctx, name: str):
greeting = yield ctx.schedule_activity("greet", [name])
return greeting
def test_greeter_returns_activity_result() -> None:
env = WorkflowEnvironment()
env.register_activity_result("greet", "hello, world")
result = env.execute_workflow(Greeter, "world")
assert result == "hello, world"
| Method | Purpose |
|---|---|
register_activity_result(name, result) | Return result for every call to activity name. Use this when the test does not care about arguments. |
register_activity(name, fn) | Call fn(*arguments) for each scheduled invocation of activity name. Use this when the mock must vary with arguments or capture invocations. |
register_child_workflow_result(workflow_type, result) | Return result when the workflow starts a child of type workflow_type. |
signal(name, args=None, run=None) | Queue a signal to be delivered before the next replay iteration. The harness injects a SignalReceived event and dispatches it to the registered @workflow.signal handler. Pass run=N to target link N of a continue-as-new chain. |
register_workflow(workflow_cls) | Make an additional workflow class resolvable by name. Required when a chain calls continue_as_new(workflow_type=...) with a type other than the starting workflow. |
execute_workflow(workflow_cls, *args, run_id="test-run") | Drive the workflow to a terminal state and return its result. Follows continue_as_new links to the final run and returns that run's result. Raises WorkflowFailed when the workflow ends in the failed state. |
runs / run_count | After execute_workflow returns, expose one WorkflowRunRecord per link in the chain (input, workflow type, history events, terminal command) so tests can assert on the full continuation chain. |
The harness fails loudly on missing fixtures:
- scheduling an activity that has no registered mock raises
KeyError - starting a child workflow that has no registered mock raises
KeyError - a workflow that never reaches a terminal state within the iteration limit
(default
1000) raisesRuntimeError
Pass iteration_limit=... to WorkflowEnvironment(...) to tune the cap for
workflows that legitimately iterate more than the default.
Callable activity mocks
Use register_activity when the mock needs to respond based on arguments or
record calls:
def test_callable_mock_captures_arguments() -> None:
captured: list[str] = []
def record_greet(name: str) -> str:
captured.append(name)
return f"greeted:{name}"
env = WorkflowEnvironment()
env.register_activity("greet", record_greet)
assert env.execute_workflow(Greeter, "alice") == "greeted:alice"
assert captured == ["alice"]
Signals
Signals queued with env.signal(...) are drained before the next replay
iteration. The signal payload is wrapped in the same {codec, blob} envelope
the worker sees at runtime and dispatched to the workflow's registered
@workflow.signal handler:
@workflow.defn(name="approval")
class Approval:
def __init__(self) -> None:
self.approved_by: str | None = None
@workflow.signal("approve")
def on_approve(self, by: str) -> None:
self.approved_by = by
def run(self, ctx):
yield ctx.schedule_activity("wait", [])
return {"approved_by": self.approved_by}
def test_signal_is_delivered_before_run_returns() -> None:
env = WorkflowEnvironment()
env.register_activity_result("wait", None)
env.signal("approve", ["alice"])
result = env.execute_workflow(Approval)
assert result == {"approved_by": "alice"}
Timers, side effects, and search attributes
The harness auto-fires the corresponding history event for each of these commands, so workflows do not block on wall-clock time inside tests:
ctx.sleep(seconds)→TimerFiredctx.side_effect(...)→SideEffectRecordedctx.upsert_search_attributes(...)→SearchAttributesUpsertedworkflow.version(...)markers →VersionMarkerRecorded
Continue-as-new chains
When a workflow returns ctx.continue_as_new(...), the harness appends a
WorkflowContinuedAsNew event to the completing run, resets history, and
starts a new run with the command's arguments. The return value of
execute_workflow is the terminal result of the final link.
@workflow.defn(name="countdown")
class Countdown:
def run(self, ctx, counter: int):
yield ctx.schedule_activity("emit", [counter])
if counter > 0:
return ctx.continue_as_new(counter - 1)
return {"final_counter": counter}
def test_chain_returns_final_run_result() -> None:
env = WorkflowEnvironment()
env.register_activity_result("emit", None)
result = env.execute_workflow(Countdown, 3)
assert result == {"final_counter": 0}
assert env.run_count == 4
assert [r.input for r in env.runs] == [[3], [2], [1], [0]]
When the chain switches workflow types, register the follow-on class first:
@workflow.defn(name="stage-one")
class StageOne:
def run(self, ctx):
yield ctx.schedule_activity("stage_one", [])
return ctx.continue_as_new(workflow_type="stage-two")
@workflow.defn(name="stage-two")
class StageTwo:
def run(self, ctx):
return (yield ctx.schedule_activity("stage_two", []))
def test_chain_can_switch_workflow_type() -> None:
env = WorkflowEnvironment()
env.register_workflow(StageTwo)
env.register_activity_result("stage_one", None)
env.register_activity_result("stage_two", "done")
assert env.execute_workflow(StageOne) == "done"
assert [r.workflow_type for r in env.runs] == ["stage-one", "stage-two"]
Target a signal at a specific link in the chain with run=N:
env.signal("approve", ["alice"], run=2) # delivered to the second run
The chain length is capped by continue_as_new_limit (default 50).
Exceeding the limit raises RuntimeError so tests catch runaway
continuations instead of spinning forever; tune the limit with
WorkflowEnvironment(continue_as_new_limit=...) when a chain legitimately
runs longer.
Failure assertions
Workflows that raise a Python exception surface as WorkflowFailed:
import pytest
from durable_workflow.errors import WorkflowFailed
@workflow.defn(name="failing")
class Failing:
def run(self, ctx):
yield ctx.schedule_activity("step", [])
raise RuntimeError("boom")
def test_workflow_failure_surfaces_as_workflow_failed() -> None:
env = WorkflowEnvironment()
env.register_activity_result("step", None)
with pytest.raises(WorkflowFailed) as exc_info:
env.execute_workflow(Failing)
assert "boom" in str(exc_info.value)
Replay testing against production history
Use replay_history to regression-test a workflow change against a real
history captured from the server. The replayer runs the current workflow code
against the recorded event sequence and raises if it yields a different
command than the one history recorded — the definition of a non-determinism
bug.
from durable_workflow import Client
from durable_workflow.testing import replay_history
async with Client("http://localhost:8080") as client:
history = await client.get_history("order-42", run_id="...")
replay_history(OrderWorkflow, history["events"], start_input=["order-42"])
A workflow that previously completed must still complete when replayed
against the same history. If the workflow code changed in a way that diverges
from the recorded sequence (reordered activity calls, removed branches,
changed activity types), replay_history raises so the regression is caught
in CI rather than in production.
replay_history_file is a convenience wrapper that reads a JSON file in
either of two shapes: a top-level list of events, or a dict with an events
key matching the get_history response shape:
from durable_workflow.testing import replay_history_file
replay_history_file(
OrderWorkflow,
"tests/histories/order-42.json",
start_input=["order-42"],
)
Both functions accept an optional payload_codec validation hint. Leave it
unset to use the history's recorded tag. An explicit or recorded tag must be
avro; json, unknown, or untagged durable payloads fail before decode.
Class-based replayer
When one test pass replays histories for several different workflow types, or
when the test should let the captured WorkflowStarted event decide which
workflow to replay, use the Replayer class. Register every workflow class
up front and call replay(...) once per captured history:
from durable_workflow import Replayer, ReplayOutcome
replayer = Replayer(workflows=[OrderWorkflow, RefundWorkflow])
# Explicit type and input — equivalent to replay_history(OrderWorkflow, ...).
outcome: ReplayOutcome = replayer.replay(
history["events"],
start_input=["order-42"],
workflow_type="order",
)
# Type and start input inferred from a WorkflowStarted event in the history.
outcome = replayer.replay(history) # history may be an events list or a
# dict with an "events" key
for command in outcome.commands:
# Inspect commands the replayed workflow would have emitted next.
...
Replayer(workflows=[...]) rejects an empty workflow set or a duplicate
registration with ValueError. replay(...) raises ValueError when a
history asks for a workflow type that was not registered, or when multiple
workflows are registered and the caller does not provide workflow_type and
the history does not include a WorkflowStarted event.
ReplayOutcome.commands is the same command list the functional
replay_history helpers return; the class-based entry point exists so a test
suite can share one registration across many histories.
Test Harness Reference
| Symbol | Purpose |
|---|---|
durable_workflow.testing.WorkflowEnvironment | In-process test harness. Drive a workflow to completion against registered activity and child-workflow mocks. |
durable_workflow.testing.replay_history(workflow_cls, events, start_input=None, *, run_id="", payload_codec=None) | Replay a history event sequence against current workflow code. Raises on non-determinism. |
durable_workflow.testing.replay_history_file(workflow_cls, path, start_input=None, *, run_id="", payload_codec=None) | Load a JSON history from disk and replay it. Accepts either a top-level list of events or a dict with an events key. |
durable_workflow.Replayer(*, workflows=[...]) | Class-based replayer. Register one or more workflow classes, then call replay(history, start_input=None, *, workflow_type=None, workflow_id=None, run_id="", payload_codec=None) to replay each captured history. Infers workflow_type and start_input from a WorkflowStarted event when the history contains one. |
durable_workflow.ReplayOutcome | Dataclass returned by Replayer.replay. Carries commands: list[Command] — the commands the replayed workflow would have emitted next. |
durable_workflow.errors.WorkflowFailed | Raised by execute_workflow when the workflow terminates in the failed state. |
durable_workflow.errors.WorkflowCancelled | Terminal state when the workflow was cancelled. Inherits from BaseException. |
durable_workflow.errors.WorkflowTerminated | Terminal state when the workflow was terminated by the server. Inherits from BaseException. |
Payload Codec
Every payload that crosses the worker-protocol boundary is codec-tagged.
Durable Workflow 2.0 has one public payload codec: avro.
The Python SDK rejects JSON-tagged, unknown, and untagged durable payloads
instead of selecting another decoder. See the Avro Value protocol.
Avro support is built in
The pinned 2.0 SDK artifact pulls in fastavro as a runtime dependency, so
every outgoing surface (start_workflow, signal_workflow, query_workflow,
update_workflow, activity result encoding, schedule actions) emits
Avro-tagged payloads through the optimized production path. There is no
optional codec extra to install.
Fixed typed values
The SDK explicitly selects named branches in the shared
durable_workflow.protocol.Value schema. It does not JSON-encode the value
inside Avro. Integers and doubles, strings and bytes, booleans and integers,
and lists and maps therefore remain distinct across SDKs. The wire uses Avro
single-object framing and the schema fingerprint; unknown fingerprints fail
with unsupported_payload_schema.
Every client and worker surface works end-to-end on the Avro default:
- Client starts, signals, queries, updates —
start_workflow,signal_workflow,query_workflow, andupdate_workflowemitpayload_codec = "avro"payloads through the fixed Value schema. A Python client can therefore drive workflows that PHP and other polyglot SDKs will replay, and vice-versa. - Activity worker — Avro-tagged activity arguments decode transparently. The worker encodes activity results as Avro so PHP, Python, and future SDK workers share one payload boundary.
- Activity failures —
fail_activity_task(..., details=...)sendsfailure.detailsas a{codec, blob}envelope. The server records the blob plusdetails_payload_codec, so diagnostic failure data from Python workers remains language-neutral in history exports and observability views. - Workflow worker history replay — Avro-tagged start input and activity result events are decoded during replay, so a Python workflow can participate in an Avro-coded run.
- Workflow query tasks — Server-routed query tasks carry Avro-tagged workflow arguments, query arguments, and replay history. The worker returns the query result as an Avro envelope.
Running a Python activity worker against a v2 run
No codec configuration is needed. The SDK validates payload_codec before
decoding every claim, accepts only avro, runs the activity, and returns an
Avro-tagged result. Any other tag fails closed with
unsupported_payload_codec; the worker never sniffs or guesses the format.
Types that round-trip cleanly across Python and PHP
| Python type | Avro Value branch | PHP type |
|---|---|---|
str | StringValue | string |
bytes | BytesValue | AvroBinaryValue |
int | LongValue | int |
float | DoubleValue | float |
bool | BooleanValue | bool |
None | null | null |
list | ArrayValue | array (list) |
dict[str, ...] | MapValue | array (string-keyed map) |
Typed activity values
The selected stable SDK supports dataclasses and enums at activity boundaries.
It sends a dataclass as a string-keyed map of its fields and an Enum as its
.value, preserving the same Avro bytes as explicit neutral values.
| Boundary | Published opt-in behavior |
|---|---|
| Activity arguments | @activity.defn(..., typed=True) decodes positional arguments using parameter annotations. |
| Activity results | ctx.schedule_activity(..., result_type=T) decodes the completed result at the workflow's yield, including parallel groups and ctx.select(...). |
| Workflow, child, client, signal, query, update and validator values | The broader adapters in SDK #255 are merged but unreleased. Keep explicit neutral-value conversion for these boundaries. |
Supported receiving annotations are dataclasses, Enum (including IntEnum
and StrEnum), Optional[T] / T | None, list[T], dict[str, T], str,
int, float, bool, bytes and Any. Other annotations, including a union
of two non-None types, raise TypeError at declaration or call time.
Decoding remains opt-in. result_type is local and is never sent, compared or
written to history. See the published SDK reference
for defaults, unknown fields and TypedValueError handling.
Sets, tuples, datetime, UUID, Decimal, pydantic and attrs objects still require an explicit adapter to canonical strings, numbers, maps or lists. Changing that durable value shape is a workflow compatibility change.
Running Against a Shared Server
The Quickstart above shows how to bring up a local server with the published Docker image. In a team environment you usually point the Python worker at an existing server (staging, production, or a shared dev instance):
from durable_workflow import Client
client = Client(
"https://workflow.example.internal",
control_token="team-orders-operator-token",
worker_token="team-orders-worker-token",
namespace="team-orders",
)
Set the namespace argument to whichever tenant namespace the shared server has
provisioned for your team, and use the credentials issued for that namespace.
The Server operator manages namespace creation; see the
Server guide for details.
The same constructor supports a Cloud managed runtime. Use the namespace's
Cloud-provided runtime URL and namespace value, set control_token to the
client runtime credential, and set worker_token to the worker runtime
credential. Cloud provisions the namespace; do not substitute a self-hosted
Server address. See
Cloud Managed Runtime.
Workflow list and history pages
The selected stable Python SDK provides lazy Client.iter_workflows,
Client.iter_history and WorkflowHandle.iter_history. Use async for to
consume workflow executions or history event dictionaries in Server order.
See the SDK-owned examples and pagination reference
and published client source.
Each iterator fetches one page at a time without retaining earlier result pages.
It follows empty pages that have a continuation and stops when the continuation
is missing, None or empty. Stopping iteration fetches no subsequent page.
HTTP errors, cancellation, history pages whose events field is not a list of
dictionaries, and malformed or cyclic continuations propagate. Earlier yielded items are not rolled back, so
handle an interrupted traversal as incomplete.
Keep namespace, filters and page size fixed throughout a traversal. History
also requires the same explicit workflow_id and run_id; the handle helper
refuses a handle without run_id. A namespace change is refused before the
next request. Supply next_page_token to resume at an opaque continuation
from the same selection. Do not parse, alter or reuse it for different filters,
namespaces or runs. Traversing a changing workflow list does not create a
snapshot.
The existing await client.list_workflows(...) and
await client.get_history(workflow_id, run_id, ...) still return one page.
For manual paging, pass the returned next_page_token to the next call and
stop at the final missing/null/empty token. A partial page or a sequence of
event dictionaries is not a replay bundle. Use
await client.export_history(workflow_id, run_id) or
await handle.export_history() for offline replay.
The installed SDK 2.17.2 / native Server 3.9.2 pagination check covered filtered lists, selected-run history, opaque resume, early close and namespace refusal, with independent acceptance.