Skip to main content
The Model class represents a namespace for organizing artifacts, metadata, and other resources related to a machine learning model.

Signature

Model(
    name: str,
    license: Optional[str] = None,
    description: Optional[str] = None,
    audience: Optional[str] = None,
    use_cases: Optional[str] = None,
    limitations: Optional[str] = None,
    trade_offs: Optional[str] = None,
    ethics: Optional[str] = None,
    tags: Optional[List[str]] = None,
    version: Optional[Union[ModelStages, int, str]] = None,
    save_models_to_registry: bool = True,
)

Parameters

name
str
required
The name of the model.
license
str
The license under which the model is created.
description
str
The description of the model.
audience
str
The target audience of the model.
use_cases
str
The use cases of the model.
limitations
str
The known limitations of the model.
trade_offs
str
The tradeoffs of the model.
ethics
str
The ethical implications of the model.
tags
List[str]
Tags associated with the model.
version
Union[ModelStages, int, str]
The version name, version number, or stage. If skipped, a new version will be created. Supports placeholders like {date} and {time}, plus custom placeholders passed as substitutions.
save_models_to_registry
bool
default:"True"
Whether to save all ModelArtifacts to Model Registry if available in the active stack.

Properties

id
UUID
The unique identifier of the model version.
model_id
UUID
The unique identifier of the model.
number
int
The version number of this model version.
stage
Optional[ModelStages]
The current stage of this model version (e.g., staging, production).

Methods

load_artifact

def load_artifact(
    name: str,
    version: Optional[str] = None
) -> Any
Load an artifact linked to this model version. Parameters:
  • name (str): Name of the artifact to load
  • version (Optional[str]): Version of the artifact to load
Returns: The loaded artifact

get_artifact

def get_artifact(
    name: str,
    version: Optional[str] = None
) -> Optional[ArtifactVersionResponse]
Get the artifact response linked to this model version.

set_stage

def set_stage(
    stage: Union[str, ModelStages],
    force: bool = False
) -> None
Set this model version to a specific stage. Parameters:
  • stage: The target stage (e.g., “staging”, “production”)
  • force: Whether to force archiving of the current version in the target stage

log_metadata

def log_metadata(
    metadata: Dict[str, MetadataType]
) -> None
Log metadata for this model version. Parameters:
  • metadata: Dictionary of metadata key-value pairs

Examples

Basic Model Usage

from zenml import Model, pipeline, step

model = Model(
    name="iris_classifier",
    version="1.0.0",
    description="Iris species classifier",
    tags=["classification", "iris"]
)

@pipeline(model=model)
def training_pipeline():
    # Pipeline steps
    pass

Using Model Stages

from zenml import Model, ModelStages

# Load production model
production_model = Model(
    name="iris_classifier",
    version=ModelStages.PRODUCTION
)

# Load specific version
specific_model = Model(
    name="iris_classifier",
    version="1.2.0"
)

Loading Artifacts from Model

from zenml import Model

model = Model(
    name="iris_classifier",
    version="production"
)

# Load a specific artifact
training_data = model.load_artifact("training_data")
model_weights = model.load_artifact("model", version="3")

Logging Model Metadata

from zenml import step, Model

@step(model=Model(name="my_model"))
def train_model():
    # Training code
    accuracy = 0.95
    
    from zenml import get_step_context
    context = get_step_context()
    
    context.model.log_metadata({
        "accuracy": accuracy,
        "framework": "sklearn",
        "dataset_size": 1000
    })

Promoting Model to Stage

from zenml import Model, ModelStages

model = Model(
    name="iris_classifier",
    version="1.5.0"
)

# Promote to staging
model.set_stage(ModelStages.STAGING)

# Promote to production (force replace current production model)
model.set_stage(ModelStages.PRODUCTION, force=True)

@pipeline

Configure model in pipelines

@step

Configure model in steps

log_model_metadata

Log model metadata

link_artifact_to_model

Link artifacts to models

Build docs developers (and LLMs) love