Use this file to discover all available pages before exploring further.
The Step Context provides access to runtime information, metadata, and utilities within your step functions. It allows steps to access information about the current pipeline run, interact with the stack, and retrieve artifacts from previous runs.
from zenml import step, get_step_context@stepdef stack_aware_step() -> dict: """Access active stack components.""" context = get_step_context() # Get artifact store artifact_store = context.stack.artifact_store print(f"Artifact store: {artifact_store.name}") print(f"Artifact store path: {artifact_store.path}") # Get orchestrator orchestrator = context.stack.orchestrator print(f"Orchestrator: {orchestrator.name}") # Check for optional components if context.stack.experiment_tracker: print(f"Experiment tracker: {context.stack.experiment_tracker.name}") return { "artifact_store": artifact_store.name, "orchestrator": orchestrator.name, }
7
Step 3: Retrieve Previous Artifacts
8
Load artifacts from previous pipeline runs:
9
from zenml import step, get_step_contextfrom zenml.client import Client@stepdef compare_with_baseline(current_metrics: dict) -> dict: """Compare current metrics with baseline from previous run.""" context = get_step_context() client = Client() # Get the current pipeline pipeline_name = context.pipeline.name # Find previous successful runs previous_runs = client.list_pipeline_runs( pipeline_name=pipeline_name, status="completed", size=2 # Get last 2 runs (current + previous) ) if len(previous_runs.items) > 1: # Get the previous run (index 1, since 0 is current) previous_run = previous_runs.items[1] # Load artifact from previous run baseline_metrics = previous_run.steps["evaluate_model"].output.load() comparison = { "current_accuracy": current_metrics["accuracy"], "baseline_accuracy": baseline_metrics["accuracy"], "improvement": current_metrics["accuracy"] - baseline_metrics["accuracy"] } print(f"Improvement: {comparison['improvement']:.3f}") return comparison else: print("No baseline available, this is the first run") return {"current_accuracy": current_metrics["accuracy"]}
10
Step 4: Access Model Context
11
Work with ZenML model registry:
12
from zenml import step, get_step_context, Model@step(model=Model(name="my_model"))def save_to_model_registry(model: object, metrics: dict) -> None: """Save model and associate with model registry.""" context = get_step_context() # Access the model context model_context = context.model print(f"Model name: {model_context.name}") print(f"Model version: {model_context.version}") # The model and metrics are automatically tracked # in the model registry through the context
@stepdef environment_aware_step(data: dict) -> dict: """Adjust behavior based on environment.""" context = get_step_context() # Check if running with specific orchestrator if context.stack.orchestrator.flavor == "kubernetes": print("Running on Kubernetes, using distributed processing") return process_distributed(data) else: print("Running locally, using single-node processing") return process_local(data)
Create artifact names based on runtime information:
from datetime import datetime@stepdef save_with_dynamic_name(data: dict) -> dict: """Save data with timestamp in artifact name.""" context = get_step_context() # Create dynamic name using context timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") run_name = context.pipeline_run.name artifact_name = f"data_{run_name}_{timestamp}" print(f"Saving as: {artifact_name}") # Your save logic here return data