Metaflow Stubs
ob-metaflow-stubs provides type stubs for the Metaflow library, enhancing static analysis and developer experience for Metaflow users. Metaflow is a human-centric framework for building and managing real-life AI and ML systems, originally developed at Netflix and now supported by Outerbounds. It streamlines the entire development lifecycle from rapid prototyping to production deployments, enabling teams to iterate quickly and deliver robust systems efficiently. The library is actively maintained with frequent patch releases, aligning with the core Metaflow project's release cadence.
Common errors
-
Error: MetaflowStepDependencyError: Step 'step_name' failed due to missing or incompatible dependencies.
cause A required Python package for a step was not found or had a version conflict in the execution environment, particularly when running remotely without explicitly declared dependencies.fixAdd `@pypi` or `@conda` decorators with the necessary packages and their versions to the `FlowSpec` or the specific `@step` method. Ensure consistency across environments. -
AttributeError: 'NoneType' object has no attribute 'some_attribute' or similar when accessing self.variable from a previous step.
cause The artifact `self.variable` from a prior step was not properly set or was not serialized/deserialized correctly, or an attempt was made to access it from an unsupported context (e.g., before the preceding step completed).fixEnsure the variable is assigned to `self.variable_name` in the producing step and that `self.next()` correctly transitions to the consuming step. When inspecting past runs, use the Metaflow Client API, `from metaflow import current`, to reliably access artifacts. -
TypeError: Object of type MyObject is not JSON serializable
cause An attempt was made to store a non-serializable Python object directly as a Metaflow artifact (instance variable `self.xyz`), which Metaflow attempts to persist between steps.fixConvert non-serializable objects into a serializable format (e.g., JSON, string, bytes, or a well-known data structure like a pandas DataFrame) before assigning them to `self.` attributes.
Warnings
- breaking While Metaflow prioritizes backward compatibility, minor breaking changes can occur in patch versions, especially those addressing bug fixes or internal architectural improvements. These can impact how stubs align with the runtime library.
- gotcha When scaling Metaflow flows to remote compute environments (e.g., AWS Batch, Kubernetes), locally `pip install`'d or `conda install`'d third-party dependencies are not automatically available. This can lead to runtime errors even if your local environment with stubs is correct.
- gotcha Metaflow does not offer native support for Windows operating systems. Users on Windows must utilize the Windows Subsystem for Linux (WSL) to install and run Metaflow and its stubs, as it relies on a *nix-like environment.
- gotcha Using mutable default arguments in `@step` method signatures (e.g., `items=[]`) can lead to unexpected shared state across different task executions, violating Metaflow's reproducibility principles.
- gotcha Failing to call `self.next()` at the end of a step will terminate the flow execution at that step, preventing subsequent steps from running, even if the logic within the current step completes successfully.
Install
-
pip install ob-metaflow-stubs
Imports
- FlowSpec
from metaflow import FlowSpec
- step
from metaflow import step
- Parameter
from metaflow import Parameter
- current
from metaflow import current
- retry
from metaflow.decorators import retry
from metaflow import retry
Quickstart
from metaflow import FlowSpec, step
class HelloFlow(FlowSpec):
"""A simple Metaflow flow to validate installation."""
@step
def start(self):
print("HelloFlow is starting.")
self.message = "Metaflow says: Hi!"
self.next(self.hello)
@step
def hello(self):
print(self.message)
self.next(self.end)
@step
def end(self):
print("HelloFlow is all done.")
if __name__ == "__main__":
HelloFlow()