Metaflow

On this page 7

Metaflow is an open-source Python framework for building and operating data science and machine learning workflows, in which each project is written as a flow of steps whose code, data and results are versioned automatically. In MLOps & deployment, it sits between the notebook and the production scheduler: the same flow runs on a laptop, scales out to cloud compute and is then deployed to a production orchestrator without being rewritten.

The framework was created inside Netflix to support hundreds of data scientists and was released as open source in 2019. Its design principle is human-centric infrastructure: the data scientist writes ordinary Python, while Metaflow handles state persistence, compute scaling, dependency management and scheduling in the background.

How a Metaflow flow is structured

A Metaflow workflow is a Python class that subclasses `FlowSpec`, and each method decorated with @step is one node of a directed acyclic graph. Every step ends with a call to self.next(), which names the following step, so the control flow is declared in plain code rather than in a separate configuration file. A flow always begins with a start step and ends with an end step.

The central mechanism is the artifact. Any value assigned to an instance variable, such as self.model or self.features, is serialized and stored automatically at the end of the step and becomes available to every later step. Artifacts are written to a content-addressed datastore on object storage, so identical values are stored once and every run keeps a complete, immutable record of its intermediate state.

The graph supports three transition patterns, and choosing between them determines how work is parallelized. The table below summarizes them.

PatternHow it is declaredTypical use
Linearself.next(self.train)Sequential preparation, training and evaluation
Branchself.next(self.a, self.b)Training two model families in parallel
Foreachself.next(self.fit, foreach='params')Hyperparameter sweeps or one model per segment
JoinA step that receives inputsMerging branch results and selecting a winner
Transition patterns in a Metaflow graph

Core components and decorators

Metaflow consists of a client library, a datastore, an optional metadata service and a set of compute and orchestration integrations. Behavior is configured mostly through step decorators, which keep infrastructure concerns out of the modeling code.

DecoratorPurposePractical effect
@resourcesDeclares CPU, memory and GPU needsThe same request applies to any remote backend
@kubernetes / @batchRuns a step on Kubernetes or AWS BatchScales one step without changing others
@conda / @pypiPins step-level dependenciesCreates a reproducible environment per step
@retry / @catchHandles transient and permanent failuresRetries or records the exception and continues
@cardGenerates an HTML report for a taskGives reviewers a visual summary of results
@schedule / @triggerDefines time-based or event-based startsApplies once the flow is deployed
Frequently used Metaflow decorators

Each execution receives a run ID, and every object is addressable through a pathspec of the form Flow/Run/Step/Task. The Client API uses these paths to load any past artifact into a notebook, for example Flow('ChurnFlow').latest_successful_run.data.model, which makes inspection and comparison of runs a routine Python operation.

From local runs to production deployment

Metaflow treats local development, scaled execution and production scheduling as three modes of the same flow. A developer runs python flow.py run locally, adds --with kubernetes to push all steps to a cluster, and finally executes a deployment command that compiles the graph for an external orchestrator.

The supported production targets are AWS Step Functions, Argo Workflows and Apache Airflow. Metaflow does not run its own production scheduler; it compiles the flow definition into the native format of the chosen orchestrator, which then owns scheduling, retries at the workflow level and high availability.

The resume command is a distinctive productivity feature. When a run fails at a late step, Metaflow can restart from the failed step while reusing the artifacts of all successful earlier steps, which saves hours of recomputation in long training pipelines.

Namespaces, tags and the `@project` decorator separate the work of individual users from production deployments. A production branch and several experimental branches of the same flow can run side by side without overwriting each other, which supports a controlled model promotion process.

Metaflow overlaps with several MLOps tools but occupies a specific niche: it is a workflow and artifact layer for data scientists rather than a full platform or a serving system. The comparison below shows where the boundaries lie.

ToolPrimary focusRelation to Metaflow
KubeflowKubernetes-native ML platformBroader scope, heavier operational burden
ZenMLPipeline abstraction over many stacksSimilar goal, more pluggable components
KedroProject structure and data catalogFocus on code organization, not compute
DVCData and model versioning with GitVersions files, while Metaflow versions artifacts
Apache AirflowGeneral workflow schedulingA deployment target for Metaflow flows
Metaflow and neighboring MLOps tools

Metaflow is not a model serving framework and not a feature store. Teams typically pair it with a serving layer such as KServe or BentoML and with a dedicated experiment tracking or monitoring tool when they need dashboards beyond what cards provide.

Limitations and common mistakes

The framework is opinionated about Python and about the step-and-artifact model, which is a strength for data science teams but a constraint elsewhere. Several recurring issues appear in practice.

  • Oversized artifacts: storing raw datasets of many gigabytes as artifacts slows every step; reference data in object storage or a warehouse instead.
  • Unpicklable objects: database connections and some client objects cannot be serialized, so they must be created inside the step that uses them.
  • Missing metadata service: the local metadata mode works for one person, but teams need the shared metadata service to see each other’s runs.
  • Unpinned dependencies: running remote steps without @conda or @pypi leads to environment drift between laptop and cluster.
  • Assuming built-in serving: deployed flows produce models; another component must expose them for online inference.

Put simply

Metaflow is a tool that helps data scientists turn their analysis code into a reliable process. The work is split into steps, like stations on an assembly line. Each station does one job and passes its results to the next one.

Metaflow remembers everything each station produced. If something breaks near the end, the line can restart from that point. The same line can run on one laptop or on hundreds of cloud machines. It can then be handed to a scheduler that runs it every night without anyone watching.

In practice

A subscription company retrains a customer churn model every week. Its data science team writes a Metaflow flow with five steps: load the latest account data from the warehouse, build features, train 40 gradient boosting variants in a foreach step, pick the best model in a join step, and write an evaluation card for review.

During development the team runs the flow on laptops with a small sample. For the full data, the training step carries @kubernetes and @resources(memory=32000), so the 40 variants train in parallel on the cluster in about 25 minutes instead of several hours. When a schema change once broke the evaluation step, the engineer used resume and reran only the last two steps.

The finished flow is deployed to Argo Workflows with a weekly schedule. Every run keeps its model, metrics and data snapshot as artifacts, so analysts can compare any two weeks in a notebook and trace a production model back to the exact data and code that produced it.