@materialize and asset_deps accept either a string referencing an asset key or a full Asset class instance. Using the Asset class is the way to provide additional metadata like names, descriptions, and ownership information beyond just the key.
The Asset class
While you can use simple string keys with the@materialize decorator, the Asset class provides more control over asset properties and metadata. Create Asset instances to add organizational context and improve discoverability.
Asset initialization fields
TheAsset class accepts the following parameters:
-
key(required): A valid URI that uniquely identifies your asset. This is the only required field. -
properties(optional): AnAssetPropertiesinstance containing metadata about the asset.
Interactive fieldsThe owners field can optionally reference user emails, as well as user and team handles within your Prefect Cloud workspace.
The URL field becomes a clickable link when this asset is displayed.
Using assets in materializations
Once you’ve defined yourAsset instances, use them directly with the @materialize decorator:
Adding runtime metadata
Beyond static properties, you can add dynamic metadata during task execution. This is useful for tracking runtime information like row counts, processing times, or data quality metrics.Using Asset.add_metadata()
The preferred approach is to use the add_metadata() method on your Asset instances. This prevents typos in asset keys and provides better IDE support:
Using add_asset_metadata() utility
Alternatively, you can use the add_asset_metadata() function, which requires specifying the asset key:
Accumulating metadata
You can call metadata methods multiple times to accumulate information:Explicit asset dependencies
While Prefect automatically infers dependencies from your task graph, you can explicitly declare asset relationships using theasset_deps parameter. This is useful when:
- The task graph doesn’t fully capture your data dependencies due to dynamic execution rules
- You need to reference assets that aren’t directly passed between tasks
- You want to be explicit about critical dependencies for documentation purposes
Hard-coding dependencies
Useasset_deps to explicitly declare which assets your materialization depends on. You can reference assets by key string or by full Asset instance:
Mixing inferred and explicit dependencies
You can combine task graph inference with explicit dependencies:Best practices for asset_deps references
Use string keys when referencing assets that are materialized by other Prefect workflows. This avoids duplicate metadata definitions and lets the materializing workflow be the source of truth:
Asset instances when referencing assets that are completely external to Prefect. This provides metadata about external systems that Prefect wouldn’t otherwise know about: