Azure Machine Learning Component SDK
The `azure-ml-component` library is a low-level Python SDK providing core functionalities and base classes for defining and managing components within Azure Machine Learning. It is primarily an internal dependency of the higher-level `azure-ai-ml` SDK, which is the recommended package for most end-user interactions. This package is actively maintained with frequent updates, currently at version 0.9.18.post2, usually released in sync with the `azure-ai-ml` ecosystem.
Common errors
-
ModuleNotFoundError: No module named 'azure.ml.component.core'
cause Attempting to directly import internal modules of `azure-ml-component` which are not exposed or intended for direct user consumption.fixInstead, use the `azure.ai.ml` SDK, which is the high-level API for defining components. For example, use `from azure.ai.ml import command` or `from azure.ai.ml.entities import CommandComponent`. -
azure.ai.ml.exceptions.ValidationException: Invalid schema for component
cause This error typically occurs when the component definition (e.g., inputs, outputs, command, environment) does not conform to the expected schema of `azure.ai.ml`.fixCarefully review the component's YAML definition or the arguments passed to the `command` decorator/ `CommandComponent` constructor. Ensure all required fields are present and their types/formats match the official `azure.ai.ml` component schema documentation.
Warnings
- gotcha The `azure-ml-component` library is a low-level dependency of the `azure-ai-ml` SDK and is generally not intended for direct end-user interaction for defining or managing components. For these tasks, the `azure.ai.ml` package (the main Azure Machine Learning Python SDK) is the recommended and stable entry point.
- breaking Versions of `azure-ml-component` are tightly coupled with `azure-ai-ml` versions. Direct upgrades of `azure-ml-component` without upgrading `azure-ai-ml` can lead to version conflicts, unexpected behavior, or API incompatibilities.
Install
-
pip install azure-ml-component -
pip install azure-ai-ml
Imports
- CommandComponent
from azure.ai.ml.entities import CommandComponent
- command
from azure.ai.ml import command
Quickstart
import os
from azure.ai.ml import command, Input, Output
from azure.ai.ml.entities import CommandComponent
# Define a simple command component using the recommended azure.ai.ml SDK
# This component takes an input string and produces an output string.
# For this example, we'll define a simple Python script as the command source
component_script_content = """
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--input_data", type=str)
parser.add_argument("--output_data", type=str)
args = parser.parse_args()
print(f"Received input: {args.input_data}")
with open(args.output_data, "w") as f:
f.write(f"Processed: {args.input_data.upper()}")
print(f"Wrote output to {args.output_data}")
"""
# Create a temporary directory and save the script
import tempfile
import shutil
with tempfile.TemporaryDirectory() as temp_dir:
script_path = os.path.join(temp_dir, "process.py")
with open(script_path, "w") as f:
f.write(component_script_content)
# Define the command component
hello_world_component = command(
name="hello_world_component",
display_name="Hello World Component Example",
description="A simple component that processes input text.",
inputs={
"input_data": Input(type="string", description="Input string for processing")
},
outputs={
"output_data": Output(type="uri_file", description="Processed output string")
},
command=f"python {{inputs.script_path}}/process.py --input_data {{inputs.input_data}} --output_data {{outputs.output_data}}",
environment=dict(
conda_file=os.path.join(temp_dir, "conda_env.yml"),
image="mcr.microsoft.com/azureml/openmpi4.1.0-ubuntu20.04:latest"
),
code=temp_dir
)
print("\n--- Component Definition ---")
print(hello_world_component.as_dict())
# To actually run this, you would need an MLClient connected to an Azure ML workspace.
# from azure.ai.ml import MLClient
# from azure.identity import DefaultAzureCredential
# ml_client = MLClient(
# DefaultAzureCredential(),
# subscription_id=os.environ.get("AZURE_SUBSCRIPTION_ID", ""),
# resource_group_name=os.environ.get("AZURE_RESOURCE_GROUP", ""),
# workspace_name=os.environ.get("AZURE_WORKSPACE_NAME", "")
# )
# registered_component = ml_client.components.create_or_update(hello_world_component)
# print(f"Component registered with name: {registered_component.name}, version: {registered_component.version}")