Get all the artifacts of a project from the API

How can I use the API to get all the artifacts of a project, possibly of a given type?

Hi @vrodriguezf90 , you can use type method to filter out the artifact type as elaborated in our public api docs here. There are many ways in which you can fetch all the artifacts logged for a project, one possible way is:

runs = api.runs(...)
for run in runs:
   for artifact in run.logged_artifacts():
      if artifact.type == "model": # check the artifact type
          artifact.download()
2 Likes

Thanks! It’s a simple way indeed, hadn’t thought about it. What I finally did is to take the code of the CLI function wandb artifact ls, and adapt it to return the artifacts in a list instead of printing them. I also added an option to constraint the name of the artifact, and to whether only the last version of the artifact will be returned:

def get_wandb_artifacts(project_path, type=None, name=None, last_version=True):
    public_api = wandb.Api()
    if type is not None:
        types = [public_api.artifact_type(type, project_path)]
    else:
        types = public_api.artifact_types(path)

    res = L()
    for kind in types:
        for collection in kind.collections():
            if name is None or name == collection.name:
                versions = public_api.artifact_versions(
                    kind.type,
                    "/".join([kind.entity, kind.project, collection.name]),
                    per_page=1,
                )
                if last_version: res += next(versions)
                else: res += L(versions)
    return res
2 Likes

Thanks for the update @vrodriguezf90 .