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.
pip install metaflow
Python code
25 linesfrom 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
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
- Add `@parameter` decorators to pass inputs via command line
- 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
More from ML engineering pipelines
- Bayesian Optimization in Python: A Simplified Mock Implementation medium
- Build a Data Helper Class in Python for ML Pipelines easy
- Build a Mock Random Forest Classifier in Python easy
- Champion Challenger Deployment Mock in Python easy
- Compare Model A vs Model B Metrics in Python easy
- Create a Minimal Great Expectations Suite Mock in Python easy
Keep learning
Related tutorials and quizzes for this topic.