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.

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

Python code

38 lines
Python 3.9+
# 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

stdout
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

  1. Use dataclasses to represent typed artifacts instead of plain dicts
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from ML engineering pipelines

Related tutorials and quizzes for this topic.