Building Reusable Components
This lesson shows how components are defined in YAML and Python, explains how registered versions work, and describes practices that keep shared components safe to reuse.
Learning outcomes
After completing this lesson, you will be able to:
- Define a component in YAML or with mldesigner.
- Explain why registered component versions are immutable.
- Describe how components are registered from a workflow.
- Identify changes that break a component's consumers.
Defining a component in YAML
A command component's YAML file declares its metadata, interface, code, environment and command. The following component trains a model and writes an MLflow model to its output.
$schema: https://azuremlschemas.azureedge.net/latest/commandComponent.schema.json
name: train_churn_model
display_name: Train churn model
version: 3
type: command
inputs:
training_data:
type: uri_folder
learning_rate:
type: number
default: 0.1
max_depth:
type: integer
default: 6
outputs:
model:
type: mlflow_model
code: ./src
environment: azureml:train-env:2
command: >-
python train.py
--data ${{inputs.training_data}}
--learning-rate ${{inputs.learning_rate}}
--max-depth ${{inputs.max_depth}}
--model-output ${{outputs.model}}An optional input uses the $[[ ]] syntax in the command, such as $[[--weights ${{inputs.class_weights}}]], so that the argument is omitted when the caller does not supply a value.
Defining and loading components in Python
The Python SDK can load a YAML component with load_component() and use it as a function in a pipeline. Components can also be written entirely in Python with the command_component decorator from the mldesigner package, which generates the interface from the function's typed parameters.
from azure.ai.ml import load_component
train = load_component(source="components/train.yml")
registered = ml_client.components.create_or_update(train)
print(registered.name, registered.version)Whichever style is used, the registered component is the same kind of asset. YAML suits teams who keep definitions next to infrastructure files and review them as text; Python suits teams who prefer to define and test everything in code.
Registering and versioning
az ml component create --file train.yml registers the component in a workspace, and adding --registry-name registers it in a registry instead. A registered version is immutable: registering a definition with the same name and version but different content fails, so each change must have a new version number.
-
Change the code or definition
Edit the script or YAML in a branch.
-
Test the component
Run it as a standalone job on sample data, and run unit tests on the script.
-
Increase the version
Set a new version in the YAML, following the team's versioning scheme.
-
Register through automation
A workflow registers the new version after the pull request is merged.
-
Adopt in pipelines
Pipelines update their reference to the new version when they are ready.
Old versions can be archived with az ml component archive to keep lists tidy; pipelines that reference them keep working.
Design practices for shareable components
A component that others depend on behaves like a public API, and the same design rules apply.
- Single responsibility: one component prepares data, another trains, another evaluates, so each can be reused and replaced separately.
- Explicit interface: every input and parameter is declared with a type and, where sensible, a default, and no step reads from hidden shared paths.
- Deterministic outputs: the same inputs and code produce the same outputs, which makes step reuse safe.
- Pinned environment: the component references a specific environment version.
- Backward compatibility: removing or renaming an input is a breaking change that deserves a clearly new version and release notes.
Registering a component from a workflow
A team keeps component YAML files in Git. Developers sometimes register components by hand from their laptops with uncommitted changes, so a registered version does not always match the code in the repository. The team wants every registered version to correspond to reviewed, committed code.
Remove the permission to register components from individual developers in the shared workspace or registry, and add a GitHub Actions workflow that runs after merges to main. It signs in with OIDC, runs tests, and runs az ml component create --file for each changed component, so every registered version is built from a reviewed commit. The commit hash can be recorded in the component's tags or description.
Asking developers to commit before registering relies on discipline and does not prevent mistakes. Using automatic version numbers would make versions unique but would not tie them to reviewed code. Registering in each developer's own workspace would hide the problem without solving it.
A team fixes a bug in a registered component and tries to register it again with the same version. What happens, and what should they do?
Show answer
Registered versions are immutable, so the same version cannot be changed. They must register a new version and update the pipelines that should use it.
Exam Tips
Remember for the exam
- Define components in YAML or with the mldesigner command_component decorator.
- Registered versions are immutable; every change gets a new version.
- Register components from a workflow after review, not from laptops.
- Treat a shared component's interface like an API: avoid breaking changes.