Accessing Metadata within Steps
How to use step fixtures to access the active ZenML stack from within a step
Aside from artifacts and step parameters, you can also pass a parameter with the type StepContext
to the input signature of your step. This object will provide additional context inside your step function, and it will give you access the related artifacts, materializers, and stack components directly from within the step.
The name of the argument can be anything, only the type hint is important.
I.e., you don’t necessarily need to call your context
.
Defining Steps with Step Contexts
Unlike BaseParameters
, you do not need to create a StepContext
object yourself and pass it when creating the step. As long as you specify a parameter of type StepContext
in the signature of your step function or class, ZenML will automatically create the StepContext
and take care of passing it to your step at runtime.
When using a StepContext
inside a step, ZenML disables caching for this step
by default as the context provides access to external resources which might
influence the result of your step execution. To enable caching anyway,
explicitly enable it in the @step
decorator with @step(enable_cache=True)
or when initializing your custom step class.
Using Step Contexts
Within a step, there are many things that you can use the StepContext
object for. For example, to access materializers, artifact locations, etc:
You can also use it to get access to your stack and the actual components within your stack:
See the API
Docs for more
information on which attributes and methods the StepContext
provides.
How to access run names and other global data from within a step
In addition to Step Fixtures, ZenML provides another interface where ZenML data can be accessed from within a step, the Environment
, which can be used to get further information about the environment where the step is executed, such as the system it is running on, the Python version, the name of the current step, pipeline, and run, and more.
As an example, this is how you could use the Environment
to find out the name of the current step, pipeline, and run:
To explore all possible operations that can be performed via the
Environment
, please consult the API docs section on
Environment.