Tags provide a flexible way to organize and categorize your Metaflow runs. Use tags to mark production deployments, track experiments, or group related runs.
# Single tagpython myflow.py run --tag experiment# Multiple tagspython myflow.py run --tag production --tag v1.2.3# Tags with spaces (use quotes)python myflow.py run --tag "model training"
from metaflow import Flow# Get the latest runrun = Flow('MyFlow').latest_run# Add a single tagrun.add_tag('production')# Add multiple tagsrun.add_tags(['deployed', 'verified', 'v2.0'])
Use the current singleton to tag runs from within a flow:
from metaflow import FlowSpec, step, currentclass TaggedFlow(FlowSpec): @step def start(self): # Add tag during execution current.run.add_tag('automated') current.run.add_tag(f"date-{current.run.created_at.date()}") self.next(self.end) @step def end(self): # Add tag based on results if self.accuracy > 0.95: current.run.add_tag('high-accuracy')
from metaflow import Flow# Find all production runsproduction_runs = [run for run in Flow('MyFlow') if 'production' in run.tags]# Find runs with multiple tagsvalidated_prod = [run for run in Flow('MyFlow') if 'production' in run.tags and 'validated' in run.tags]# Find latest run with tagfor run in Flow('MyFlow'): if 'production' in run.tags: latest_prod = run break
from metaflow import Flowrun = Flow('MyFlow')['1234']# Remove a single tagrun.remove_tag('experimental')# Remove multiple tagsrun.remove_tags(['test', 'debug'])# Remove all user tagsfor tag in run.tags: run.remove_tag(tag)
You can only modify tags on runs that haven’t been finalized by the metadata service.
from metaflow import FlowSpec, step, currentimport osclass TeamFlow(FlowSpec): @step def start(self): # Tag by team and project current.run.add_tag('team:data-science') current.run.add_tag('project:recommendation-engine') current.run.add_tag(f"user:{os.environ.get('USER')}") self.next(self.end) @step def end(self): pass
from metaflow import Flowimport reflow = Flow('MyFlow')# Find all version 2.x runsv2_runs = [run for run in flow if any(re.match(r'version:2\.\d+\.\d+', tag) for tag in run.tags)]# Find experiment runs from specific monthmonth_runs = [run for run in flow if any(tag.startswith('month:2024-03') for tag in run.tags)]# Find runs by teamteam_runs = [run for run in flow if 'team:data-science' in run.tags]
from metaflow import Flowimport pandas as pdflow = Flow('MLFlow')# Collect metrics from production runsprod_metrics = []for run in flow: if 'production' in run.tags: try: end_task = run['end'].task prod_metrics.append({ 'run_id': run.id, 'accuracy': end_task.data.accuracy, 'version': next((t.split(':')[1] for t in run.tags if t.startswith('version:')), None), 'deployed_at': run.created_at }) except: passdf = pd.DataFrame(prod_metrics)print(df)