How to Create a Mock Metaflow Flow in Python

Build a minimal Metaflow flow with two sequential steps that pass data between them using instance attributes.

Easy Python 3.9+ Aug 9, 2026 ML engineering pipelines 15 views 0 copies

Requires third-party packages — install first
pip install metaflow

Python code

25 lines
Python 3.9+
from metaflow import FlowSpec, step, current


class MockFlow(FlowSpec):
    """A minimal Metaflow flow to demonstrate basic steps and branching."""

    @step
    def start(self):
        self.category = "mock"
        print(f"Start step for {self.category} flow")
        self.next(self.process)

    @step
    def process(self):
        self.result = 42 * 2
        print(f"Processed value: {self.result}")
        self.next(self.end)

    @step
    def end(self):
        print(f"Finished. Final result is {self.result} for {self.category} flow")


if __name__ == "__main__":
    MockFlow()

Output

stdout
Start step for mock flow
Processed value: 84
Finished. Final result is 84 for mock flow

How it works

Metaflow uses decorators (@step) to define flow steps, and self.next() to chain them. Data persists between steps because Metaflow serializes instance attributes automatically at each step boundary. The if __name__ == "__main__" guard is required so the flow only runs when executed directly, avoiding recursive execution during Metaflow's internal orchestration. This pattern is the foundation for more complex ML pipelines with branches and parameter passing.

Common mistakes

  • Forgetting the `if __name__ == "__main__"` guard, causing recursion errors
  • Trying to pass data via return values instead of instance attributes
  • Missing the `@step` decorator on every method that should be a step
  • Calling `self.next()` more than once in a step

Variations

  1. Add `@parameter` decorators to pass inputs via command line
  2. Use `self.next(self.branch_a, self.branch_b)` for parallel branching

Real-world use cases

  • Prototyping an ML pipeline structure before implementing heavy model training logic.
  • Smoke-testing Metaflow deployments in CI/CD to validate step graph correctness.
  • Creating a reproducible template that teammates copy when starting new data-processing flows.

Sponsored

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from ML engineering pipelines

Related tutorials and quizzes for this topic.