Bird
Raised Fist0
MLOpsdevops~10 mins

Data validation in CI pipeline in MLOps - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to run data validation using Great Expectations in the CI pipeline.

MLOps
import great_expectations as ge
import pandas as pd
data = ge.dataset.PandasDataset(pd.read_csv('data.csv'))
results = data.[1]()
Drag options to blanks, or click blank then click option'
Avalidate
Brun
Ctest
Dcheck
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'check' or 'test' instead of 'validate' causes errors.
2fill in blank
medium

Complete the code to load the Great Expectations suite named 'my_suite' for validation.

MLOps
from great_expectations.data_context import DataContext
context = DataContext()
suite = context.get_expectation_suite('[1]')
Drag options to blanks, or click blank then click option'
Adefault_suite
Bvalidation_suite
Csuite1
Dmy_suite
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong suite name causes the pipeline to fail.
3fill in blank
hard

Fix the error in the command to run data validation in the CI pipeline using Great Expectations CLI.

MLOps
great_expectations [1] --suite my_suite --data-dir ./data
Drag options to blanks, or click blank then click option'
Atest
Bvalidate
Crun
Dcheck
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'check' or 'run' causes command not found errors.
4fill in blank
hard

Fill both blanks to create a Python dictionary comprehension that filters data rows where 'age' is greater than 18.

MLOps
filtered_data = {row['id']: row for row in data if row['[1]'] [2] 18}
Drag options to blanks, or click blank then click option'
Aage
B>
C<
Dscore
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or wrong key filters incorrect data.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase 'name' to 'score' for scores above 50.

MLOps
result = [1]: [2] for item in data if item['score'] [3] 50
Drag options to blanks, or click blank then click option'
Aitem['name'].upper()
Bitem['score']
C>
Ditem['name']
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase names or wrong comparison operator.

Practice

(1/5)
1. What is the main purpose of adding data validation in a CI pipeline for machine learning projects?
easy
A. To speed up the model training process
B. To catch data problems early before training models
C. To reduce the size of the dataset
D. To automatically deploy models to production

Solution

  1. Step 1: Understand the role of CI pipelines

    CI pipelines automate checks and tests to ensure quality before further steps.
  2. Step 2: Identify the purpose of data validation

    Data validation ensures data quality and format correctness to avoid errors in training.
  3. Final Answer:

    To catch data problems early before training models -> Option B
  4. Quick Check:

    Data validation = catch problems early [OK]
Hint: Data validation stops bad data early in pipeline [OK]
Common Mistakes:
  • Thinking validation speeds training
  • Confusing validation with deployment
  • Assuming validation reduces data size
2. Which of the following is the correct way to fail a CI pipeline step if a data validation script returns a non-zero exit code in a bash script?
easy
A. python validate_data.py || exit 1
B. python validate_data.py && exit 1
C. python validate_data.py; exit 0
D. python validate_data.py | exit 1

Solution

  1. Step 1: Understand bash exit codes and operators

    The '||' operator runs the command after it if the first command fails (non-zero exit).
  2. Step 2: Apply to data validation script

    If 'validate_data.py' fails, 'exit 1' stops the pipeline with error.
  3. Final Answer:

    python validate_data.py || exit 1 -> Option A
  4. Quick Check:

    Fail on error = '|| exit 1' [OK]
Hint: Use '|| exit 1' to fail on script error [OK]
Common Mistakes:
  • Using '&&' instead of '||' to fail
  • Using pipe '|' incorrectly
  • Exiting with 0 always
3. Given this Python snippet in a CI pipeline step:
import sys

def validate(data):
    if not data or len(data) < 5:
        return False
    return True

if __name__ == '__main__':
    data = sys.argv[1] if len(sys.argv) > 1 else ''
    if validate(data):
        print('Validation passed')
        sys.exit(0)
    else:
        print('Validation failed')
        sys.exit(1)
What will be the output and exit code if the pipeline runs python validate.py "abc"?
medium
A. Validation failed and exit code 0
B. Validation passed and exit code 0
C. Validation passed and exit code 1
D. Validation failed and exit code 1

Solution

  1. Step 1: Check input data length

    Input is 'abc' which length is 3, less than 5, so validate returns False.
  2. Step 2: Determine output and exit code

    Since validate returns False, it prints 'Validation failed' and exits with code 1.
  3. Final Answer:

    Validation failed and exit code 1 -> Option D
  4. Quick Check:

    Short data fails validation = A [OK]
Hint: Check input length to predict validation result [OK]
Common Mistakes:
  • Assuming any input passes
  • Confusing exit codes 0 and 1
  • Ignoring input length check
4. You have this YAML snippet in a CI pipeline to run data validation:
steps:
  - name: Validate Data
    run: |
      python validate.py data.csv
      echo "Data validation complete"
The pipeline does not fail even when validation.py returns exit code 1. What is the likely problem?
medium
A. The shell does not stop on errors by default; need 'set -e'
B. The 'if' condition is incorrect and never triggers
C. The 'exit 1' is inside the if but the script continues after
D. The validate.py script always returns 0

Solution

  1. Step 1: Understand shell error handling

    By default, shell scripts continue even if a command fails unless 'set -e' is used.
  2. Step 2: Apply to CI step

    Without 'set -e', the script continues after python fails, runs the echo which succeeds, so step exit code is 0.
  3. Final Answer:

    The shell does not stop on errors by default; need 'set -e' -> Option A
  4. Quick Check:

    Use 'set -e' to fail pipeline on errors [OK]
Hint: Add 'set -e' to stop on errors in shell scripts [OK]
Common Mistakes:
  • Assuming exit 1 always stops pipeline
  • Misreading if condition syntax
  • Ignoring shell default behavior
5. You want to add a data validation step in your CI pipeline that checks if a CSV file has no missing values and all numeric columns are within a specific range. Which approach best fits this requirement?
hard
A. Use a shell script with grep to search for empty fields and numeric ranges
B. Manually inspect the CSV file before running the pipeline
C. Write a Python script using pandas to check missing values and ranges, then fail with exit code 1 if invalid
D. Skip validation and rely on model training to catch errors

Solution

  1. Step 1: Identify tools for data validation

    Pandas in Python is ideal for checking missing values and numeric ranges programmatically.
  2. Step 2: Implement validation and fail pipeline

    Script should exit with code 1 if validation fails to stop the pipeline safely.
  3. Final Answer:

    Write a Python script using pandas to check missing values and ranges, then fail with exit code 1 if invalid -> Option C
  4. Quick Check:

    Use pandas script + exit 1 for robust validation [OK]
Hint: Use pandas for detailed CSV validation and fail on error [OK]
Common Mistakes:
  • Using grep which can't handle numeric ranges well
  • Relying on manual checks
  • Skipping validation entirely