title: Tag Workflows with Searchable Attributes impact: MEDIUM impactDescription: Find workflows by business metadata (customer, tenant, region) without encoding it in workflow IDs tags: workflow, attributes, SetWorkflowAttributes, update_workflow_attributes, list_workflows, schedule_name, metadata
Tag Workflows with Searchable Attributes
Attach a dictionary of JSON-serializable key-value attributes to workflows with SetWorkflowAttributes, then search for them with the attributes= filter.
Incorrect (encoding metadata in workflow IDs and scanning):
import uuid
from dbos import DBOS, SetWorkflowID
# process_order is a workflow defined elsewhere
with SetWorkflowID(f"acme-us-east-1-{uuid.uuid4()}"):
process_order(order)
# Fragile prefix matching, can't query by region alone
acme = DBOS.list_workflows(workflow_id_prefix="acme-")
Correct (attributes):
from dbos import DBOS, SetWorkflowAttributes
# Every workflow started or enqueued inside the block gets these attributes
with SetWorkflowAttributes({"customer": "acme", "region": "us-east-1"}):
process_order(order)
# Matches workflows whose attributes contain ALL given key-value pairs
acme = DBOS.list_workflows(attributes={"customer": "acme"})
queued = DBOS.list_queued_workflows(attributes={"region": "us-east-1"})
for w in acme:
print(w.workflow_id, w.attributes)
- Attributes are recorded at creation time and are not inherited by child workflows
- Filtering by
attributesrequires a Postgres system database (GIN-indexed JSONB); it raisesDBOSExceptionon SQLite - Nested values are matched exactly
Updating Attributes
DBOS.update_workflow_attributes (and _async) replaces the whole dictionary by default; pass None to clear. To add or update individual keys while keeping the rest, pass merge=True, which atomically merges the given top-level keys into the existing attributes (nested dictionaries are replaced, not merged; concurrent merges don’t lose each other’s keys; None with merge=True raises DBOSException). It is safe to call from within a workflow, including on itself (the update is recorded as a step):
Incorrect (replace drops the other keys):
# Attributes were {"customer": "acme", "region": "us-east-1"}; now they are just {"phase": "shipping"}
DBOS.update_workflow_attributes(DBOS.workflow_id, {"phase": "shipping"})
Correct (merge keeps the other keys):
@DBOS.workflow()
def process_order(order):
reserve_inventory(order) # reserve_inventory / ship are steps defined elsewhere
DBOS.update_workflow_attributes(DBOS.workflow_id, {"phase": "shipping"}, merge=True)
ship(order)
From outside the application:
DBOSClienthasclient.update_workflow_attributes(workflow_id, attrs, merge=False),list_workflows(attributes=...), andlist_queued_workflows(attributes=...)- Set attributes at enqueue time with
"attributes": {...}inEnqueueOptions
Schedule Name Tagging
Every workflow enqueued by a schedule is tagged with its schedule name (WorkflowStatus.schedule_name). Retrieve all runs of a schedule with:
runs = DBOS.list_workflows(schedule_name="my-task-schedule") # or a list of names
Reference: Workflow Attributes