Returns the path and Git revision for an artifact in a DVC project. Artifacts are managed through the DVC model registry and can have versions or be assigned to stages (like staging, production).The resulting path and revision can be used in conjunction with other dvc.api calls (like open() or read()) to access and work with the artifact.
Version of the artifact to retrieve. Defaults to the latest version if not specified.Mutually exclusive with stage - you can specify either version or stage, not both.
Name of the model registry stage.Common stages include “staging”, “production”, “dev”, etc.Mutually exclusive with version - you can specify either version or stage, not both.
import dvc.api# Get artifact assigned to production stageprod_artifact = dvc.api.artifacts_show( "my-model", stage="production")print(f"Production model at: {prod_artifact['path']}")print(f"From revision: {prod_artifact['rev']}")
import dvc.apiimport pickle# Get artifact infoartifact = dvc.api.artifacts_show("my-model", stage="production")# Use the info to read the artifactmodel_data = dvc.api.read( artifact["path"], rev=artifact["rev"], mode="rb")# Load the modelmodel = pickle.loads(model_data)predictions = model.predict(X_test)
import dvc.api# Get staging and production versionsstaging = dvc.api.artifacts_show("my-model", stage="staging")production = dvc.api.artifacts_show("my-model", stage="production")print("Staging:")print(f" Rev: {staging['rev']}")print(f" Path: {staging['path']}")print("\nProduction:")print(f" Rev: {production['rev']}")print(f" Path: {production['path']}")if staging['rev'] != production['rev']: print("\nStaging and production are on different revisions")
import dvc.apiimport requests# Get artifact infoartifact = dvc.api.artifacts_show("my-model", stage="production")# Get the URLurl = dvc.api.get_url( artifact["path"], rev=artifact["rev"])# Download if it's an HTTP URLif url.startswith("http"): response = requests.get(url) with open("local_model.pkl", "wb") as f: f.write(response.content) print("Model downloaded to local_model.pkl")
import dvc.apidef get_production_model(name): """Get production model, with fallback to staging.""" try: return dvc.api.artifacts_show(name, stage="production") except ArtifactNotFoundError: print(f"No production version, trying staging...") try: return dvc.api.artifacts_show(name, stage="staging") except ArtifactNotFoundError: raise ValueError(f"No production or staging version for {name}")artifact = get_production_model("my-classifier")print(f"Using: {artifact['path']}")
Use version when you need a specific version of an artifact:
# Get exact versionartifact = dvc.api.artifacts_show("model", version="v2.1.0")# Good for:# - Reproducibility# - Testing specific versions# - Rollback scenarios
Use stage when you want the current artifact for an environment:
# Get whatever is currently in productionartifact = dvc.api.artifacts_show("model", stage="production")# Good for:# - Deployment pipelines# - Environment-specific configs# - Promotion workflows
Omit both to get the most recent version:
# Get latest versionartifact = dvc.api.artifacts_show("model")# Good for:# - Development# - Testing newest changes# - Quick access
When you need exact reproducibility, use specific versions:
# For reproducing results from a paperartifact = dvc.api.artifacts_show( "research-model", version="v1.0.0", # Exact version from publication repo="https://github.com/lab/research-repo")
Combine with other API functions
Use the returned info with other dvc.api functions:
# Get artifact locationartifact = dvc.api.artifacts_show("model", stage="production")# Load the artifactwith dvc.api.open(artifact["path"], rev=artifact["rev"], mode="rb") as f: model = pickle.load(f)# Or get the URLurl = dvc.api.get_url(artifact["path"], rev=artifact["rev"])
Handle missing artifacts gracefully
Always handle the case where an artifact might not exist:
from dvc.exceptions import ArtifactNotFoundErrortry: artifact = dvc.api.artifacts_show("model", stage="production")except ArtifactNotFoundError: # Fall back to staging or default print("No production model, using staging") artifact = dvc.api.artifacts_show("model", stage="staging")
import dvc.apiimport pickleimport osdef deploy_model(model_name, stage="production"): """Deploy a model from the registry.""" # Get artifact info artifact = dvc.api.artifacts_show(model_name, stage=stage) print(f"Deploying {model_name} from {stage} stage") print(f" Revision: {artifact['rev'][:7]}") print(f" Path: {artifact['path']}") # Load model model_data = dvc.api.read( artifact["path"], rev=artifact["rev"], mode="rb" ) model = pickle.loads(model_data) # Save locally for serving os.makedirs("deployed_models", exist_ok=True) with open(f"deployed_models/{model_name}.pkl", "wb") as f: f.write(model_data) print(f"✓ Model deployed successfully") return model# Deploy production modelmodel = deploy_model("my-classifier", stage="production")