How to create a batch inference pipeline in Azure ML: step by step
This tutorial shows how to create a batch inference pipeline in Azure ML to process large CSV files and generate predictions in a scalable way. A batch pipeline is useful when you have many records or want to automate periodic predictions without a real-time service.
Prerequisites
- Azure account with a subscription and permissions to create resources.
- Azure Machine Learning workspace created.
- Azure Machine Learning SDK for Python (azure-ai-ml) installed locally or on a Compute.
- Test CSV file with the same columns the model expects.
- Model already registered in Azure ML (or a ready model.pkl file).
Step 1: Prepare the environment and dependencies
Why: ensuring the SDK and variables are configured prevents common errors in remote execution. Next, we authenticate and set the workspace.
pip install azure-ai-ml azure-identity pandas scikit-learn
from azure.ai.ml import MLClient from azure.identity import DefaultAzureCredential subscription_id = "" resource_group = "" workspace_name = "" ml_client = MLClient(DefaultAzureCredential(), subscription_id, resource_group, workspace_name)
Step 2: Package the inference script
Why: the script defines how to read the data and invoke the model. Include a minimal script that loads a registered model and applies predictions to a CSV.
# ficheiro: score.py import os import pandas as pd import joblib def main(input_csv, output_csv): model_path = os.environ.get("MODEL_PATH", "model.pkl") model = joblib.load(model_path) df = pd.read_csv(input_csv) preds = model.predict(df) df['prediction'] = preds df.to_csv(output_csv, index=False) if __name__ == '__main__': import sys main(sys.argv[1], sys.argv[2])
Step 3: Create a component that runs the script
Why: in Azure ML pipelines we use components to package the execution and specify requirements (CPU, image, dependencies). We create a simple YAML or define the component in Python.
from azure.ai.ml import command_component, Input, Output from azure.ai.ml.entities import Environment comp = command_component( name="batch-score-comp", description="Component for batch inference from CSV", inputs={"input_csv": Input(type="uri_file")}, outputs={"output_csv": Output(type="uri_file")}, code="./", # directory with score.py and environment.yml command="python score.py ${{inputs.input_csv}} ${{outputs.output_csv}}", environment=Environment(image="mcr.microsoft.com/azureml/base:latest", conda_file="environment.yml") ) ml_client.components.create_or_update(comp)
Step 4: Register or reference the model
Why: the component needs access to the model file. You can register the model in the Workspace and mount/transfer it to the job.
from azure.ai.ml.entities import Model model = ml_client.models.create_or_update( Model(path="./model.pkl", name="my-model", type="mlflow" ) )
Step 5: Create and submit the batch pipeline job
Why: a job encapsulates the execution on a target compute and enables scalability. Here we configure a simple job that passes the input CSV and receives the output CSV.
from azure.ai.ml import Input, Output, command from azure.ai.ml.entities import AmlCompute # optional: create compute compute = AmlCompute(name="cpu-cluster", size="STANDARD_DS3_V2", min_instances=0, max_instances=2) ml_client.begin_create_or_update(compute).result() job = command( name="batch-inference-job", component=comp, inputs={ "input_csv": Input(type="uri_file", path="https://.blob.core.windows.net/data/input.csv")}, outputs={"output_csv": Output(type="uri_file", path="azureml://datastores/workspaceblobstore/paths/batch-output/output.csv")}, compute="cpu-cluster" ) returned_job = ml_client.jobs.create_or_update(job) ml_client.jobs.stream(returned_job.name)
Step 6: Automate with triggers (optional)
Why: for recurring runs you can use Azure Logic Apps or Azure Data Factory to trigger the job. A common pattern is calling the Azure ML REST API to submit the job when a new file arrives in Storage.
Verify the result
Simple check: view the job logs with ml_client.jobs.stream(job.name) and inspect the output file in the datastore/Blob. The output CSV should have the 'prediction' column with generated values.
Conclusion
You created a batch inference pipeline in Azure ML, from packaging the script to submitting the job and verifying the results. Next steps: add structured logging, manage model versions and use parallelism for very large files. Tip: test locally with a subset of data before uploading to remote compute — what is the largest CSV file you have to test?