How to Build a Mock TFX Pipeline in Python
Simulate a TFX-style ML pipeline with simple Python functions to understand component orchestration, data flow, and artifact passing.
Python code
38 lines# Mock TFX pipeline to illustrate component orchestration
def CsvExampleGen(data_path):
"""Mock component: Simulates reading CSV data."""
print(f"ExampleGen: Reading from {data_path}")
return {"records": 100, "name": "examples"}
def StatisticsGen(example_artifact):
"""Mock component: Simulates generating statistics."""
print(f"StatisticsGen: Processing {example_artifact['records']} records")
return {"mean": 5.2, "std": 1.8}
def SchemaGen(statistics_artifact):
"""Mock component: Simulates schema inference."""
print(f"SchemaGen: Inferring schema from mean={statistics_artifact['mean']}")
return {"columns": ["feature_a", "feature_b", "label"]}
def Transform(schema_artifact, examples_artifact):
"""Mock component: Simulates feature transformation."""
print(f"Transform: Applying schema {schema_artifact['columns']}")
return {"transformed_examples": 100}
def Trainer(transform_artifact):
"""Mock component: Simulates model training."""
print(f"Trainer: Training on {transform_artifact['transformed_examples']} examples")
return {"model_path": "model.pkl", "accuracy": 0.92}
def main():
data_path = "data/train.csv"
examples = CsvExampleGen(data_path)
stats = StatisticsGen(examples)
schema = SchemaGen(stats)
transformed = Transform(schema, examples)
model = Trainer(transformed)
print(f"Pipeline complete: {model['model_path']}, accuracy={model['accuracy']}")
if __name__ == "__main__":
main()
Output
ExampleGen: Reading from data/train.csv
StatisticsGen: Processing 100 records
SchemaGen: Inferring schema from mean=5.2
Transform: Applying schema ['feature_a', 'feature_b', 'label']
Trainer: Training on 100 examples
Pipeline complete: model.pkl, accuracy=0.92
How it works
Each function mimics a TFX component that takes artifacts (dictionaries) as input and produces new artifacts as output. The data flows sequentially: raw data → examples → statistics → schema → transformed data → model. This mirrors how real TFX pipelines pass metadata between components. By using plain Python dicts, you can test orchestration logic without heavy dependencies. The main() function acts as the runner, simulating the pipeline DAG execution.
Common mistakes
- Hardcoding component outputs instead of passing them between functions
- Forgetting to propagate all artifacts needed downstream (e.g., examples to Transform)
- Using global variables instead of explicit function parameters
- Not structuring outputs consistently as dictionaries
Variations
- Use dataclasses to represent typed artifacts instead of plain dicts
- Add a simple config object to control data_path and hyperparameters
Real-world use cases
- Prototyping a TFX pipeline design before implementing with actual TFX components on Kubernetes.
- Unit testing ML pipeline orchestration logic in CI without spinning up distributed infra.
- Teaching or documenting how data artifacts flow through ML training stages at a high level.
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.