Joe Blauer
07/10/2025, 8:25 PMMarvin
07/10/2025, 8:25 PMMarvin
07/10/2025, 8:25 PM@materialize
decorator in several ways. Let me show you with some examples:
python
from prefect import materialize
# Basic usage with static parameters
@materialize(asset="my-asset")
def my_function():
return "result"
# Using variables for asset names
asset_name = "dynamic-asset"
@materialize(asset=asset_name)
def dynamic_asset_function():
return "result"
# Multiple assets
asset_list = ["asset1", "asset2"]
@materialize(*asset_list)
def multi_asset_function():
return "result"
# With additional task configuration
task_config = {"retries": 3, "retry_delay_seconds": 60}
@materialize(asset="my-asset", **task_config)
def configured_function():
return "result"
The @materialize
decorator accepts:
1. Asset names as strings or Asset
objects
2. Any valid task configuration parameters (like retries, tags, etc.)
You can mix and match these parameters as needed, and they can all be variables rather than static values. This makes it very flexible for dynamic asset creation and configuration.
For more details about working with assets, you can check out the assets documentation.Joe Blauer
07/10/2025, 8:30 PMMarvin
07/10/2025, 8:30 PMMarvin
07/10/2025, 8:31 PMpython
from prefect import materialize
from functools import partial
def create_asset(location):
# Create a new decorator with the runtime location
runtime_materialize = materialize(asset=location)
# Use this decorator on your function
@runtime_materialize
def my_function():
return "some result"
return my_function()
# Use it like this:
result = create_asset("my/dynamic/location")
Or, if you prefer a class-based approach:
python
from prefect import materialize
class DynamicAssetCreator:
def process_data(self, location):
# Create and apply the decorator at runtime
@materialize(asset=location)
def _process():
return "processed data"
return _process()
# Use it like this:
processor = DynamicAssetCreator()
result = processor.process_data("path/to/my/asset")
The key point is that instead of using the decorator syntax (@materialize
), we're creating the decorator at runtime when we know the location and then immediately applying it to our function.
This pattern is particularly useful when:
- The asset location depends on runtime parameters
- You're creating assets programmatically based on user input
- You need to generate multiple assets with different locations in a loop
Just remember that each time you call the function, it will create a new materialized asset at the specified location.Nate
07/10/2025, 8:58 PMwith_options
Nate
07/10/2025, 8:59 PM