How to query run-setup from artifact ids from python

I have a bunch of artifacts I am interested in analysing, but I need the metadata (like config and summary) for the run for it. I can load them with:

import wandb

api = wandb.Api()
artifact = api.artifact("LINK")
artifact_dir = artifact.download("../data/artifacts/"+artifact.name)

but I am not sure how to access information from the run from the artifact object? I need data from the config and summary.

Hi @leander-kurscheidt , to access the metadata such as config and summary from the run associated with an artifact, you can use the logged_by() method of the artifact object to get the run that logged the artifact. Once you have the run object, you can access its config and summary attributes.

Here’s a code snippet that demonstrates how to do this:

import wandb
from pathlib import Path

# Initialize the W&B API
api = wandb.Api()

# Get the artifact object using its path
artifact_path = "entity/project/artifact_name:version"
artifact = api.artifact(artifact_path)

# Download the artifact to a local directory
artifact_dir = Path(artifact.download("../data/artifacts/" + artifact.name))

# Get the run that logged the artifact
producer_run = artifact.logged_by()

# Access the config and summary of the run
run_config = producer_run.config
run_summary = producer_run.summary

# Now you can use run_config and run_summary as needed
# For example, print a specific config value
print("Learning rate:", run_config.get("learning_rate"))

# Print a specific summary metric
print("Final accuracy:", run_summary.get("accuracy"))

In this code, replace "entity/project/artifact_name:version" with the actual path to your artifact. The logged_by() method returns the run object that created the artifact. You can then access the config and summary dictionaries of the run to get the metadata you need.

Remember to handle cases where the config or summary might not contain the keys you’re looking for, as attempting to access a non-existent key will raise a KeyError.