How to download wandb artifacts
Quick answer
Use the
wandb.Api() class from the wandb Python SDK to access and download artifacts. Instantiate the API, retrieve the artifact by its name and version, then call artifact.download() to save it locally.PREREQUISITES
Python 3.8+pip install wandbWeights & Biases account with API key configured (wandb login)
Setup
Install the wandb Python package and authenticate your environment with your W&B API key.
- Install wandb:
pip install wandb - Login to W&B CLI to configure your API key:
wandb login
pip install wandb
wandb login Step by step
This example shows how to download a specific artifact from your W&B project using the Python SDK.
import wandb
# Initialize the W&B API
api = wandb.Api()
# Specify the artifact path: "entity/project/artifact_name:version"
artifact_path = "your-entity/your-project/model-weights:v0"
# Retrieve the artifact
artifact = api.artifact(artifact_path)
# Download the artifact locally (returns the local directory path)
local_dir = artifact.download()
print(f"Artifact downloaded to: {local_dir}") output
Artifact downloaded to: /home/user/wandb/artifacts/model-weights:v0
Common variations
- Download the latest version by omitting the version tag, e.g.,
"your-entity/your-project/model-weights". - Use
artifact.file("filename.ext")to access a specific file inside the artifact. - For asynchronous workflows, use Python's async features with W&B's API calls wrapped in async functions.
import wandb
api = wandb.Api()
# Download latest artifact version
artifact = api.artifact("your-entity/your-project/model-weights")
local_dir = artifact.download()
# Access a specific file inside the artifact
file_path = artifact.file("weights.pt")
print(f"File path: {file_path}") output
File path: /home/user/wandb/artifacts/model-weights:v0/weights.pt
Troubleshooting
- If you get authentication errors, ensure you have run
wandb loginand your API key is valid. - Check that the artifact path is correct and that you have access permissions to the project and artifact.
- If download hangs or fails, verify your network connection and W&B service status.
Key Takeaways
- Use
wandb.Api()to programmatically access artifacts in your W&B projects. - Call
artifact.download()to save artifacts locally for use in your workflows. - Ensure your W&B API key is configured via
wandb loginto avoid authentication issues.