Add wandb to any library
This guide provides best practices on how to integrate W&B into your Python library to get powerful Experiment Tracking, GPU and System Monitoring, Model Checkpointing, and more for your own library.If you are still learning how to use W&B, we recommend exploring the other W&B Guides in these docs, such as Experiment Tracking, before reading further.
- Setup requirements
- User Login
- Starting a wandb Run
- Defining a Run Config
- Logging to W&B
- Distributed Training
- Model Checkpointing and More
- Hyper-parameter tuning
- Advanced Integrations
Setup requirements
Before you get started, decide whether or not to require W&B in your library’s dependencies:Require W&B on installation
Add the W&B Python library (wandb
) to your dependencies file, for example, in your requirements.txt
file:
Make W&B optional on installation
There are two ways to make the W&B SDK (wandb
) optional:
A. Raise an error when a user tries to use wandb
functionality without installing it manually and show an appropriate error message:
wandb
as an optional dependency to your pyproject.toml
file, if you are building a Python package:
User login
Create an API key
An API key authenticates a client or machine to W&B. You can generate an API key from your user profile.For a more streamlined approach, you can generate an API key by going directly to the W&B authorization page. Copy the displayed API key and save it in a secure location such as a password manager.
- Click your user profile icon in the upper right corner.
- Select User Settings, then scroll to the API Keys section.
- Click Reveal. Copy the displayed API key. To hide the API key, reload the page.
Install the wandb
library and log in
To install the wandb
library locally and log in:
- Command Line
- Python
- Python notebook
-
Set the
WANDB_API_KEY
environment variable to your API key. -
Install the
wandb
library and log in.
wandb.init
.
Start a run
A W&B Run is a unit of computation logged by W&B. Typically, you associate a single W&B Run per training experiment. Initialize W&B and start a Run within your code with:wandb_project
in your code along with the username or team name, such as wandb_entity
, for the entity parameter:
run.finish()
to finish the run. If this works with your integration’s design, use the run as a context manager:
When to call wandb.init
?
Your library should create W&B Run as early as possible because any output in your console, including error messages, is logged as part of the W&B Run. This makes debugging easier.
Use wandb
as an optional dependency
If you want to make wandb
optional when your users use your library, you can either:
- Define a
wandb
flag such as:
- Python
- Bash
- Or, set
wandb
to bedisabled
inwandb.init
:
- Python
- Bash
- Or, set
wandb
to be offline - note this will still runwandb
, it just won’t try and communicate back to W&B over the internet:
- Environment Variable
- Bash
Define a run config
With awandb
run config, you can provide metadata about your model, dataset, and so on when you create a W&B Run. You can use this information to compare different experiments and quickly understand the main differences.

- Model name, version, architecture parameters, etc.
- Dataset name, version, number of train/val examples, etc.
- Training parameters such as learning rate, batch size, optimizer, etc.
Update the run config
Usewandb.Run.config.update
to update the config. Updating your configuration dictionary is useful when parameters are obtained after the dictionary was defined. For example, you might want to add a model’s parameters after the model is instantiated.
Log to W&B
Log metrics
Create a dictionary where the key value is the name of the metric. Pass this dictionary object torun.log
:
train/...
and val/...
. This will create separate sections in your W&B Workspace for your training and validation metrics, or other metric types you’d like to separate:

wandb.Run.log()
reference.
Prevent x-axis misalignments
If you perform multiple calls torun.log
for the same training step, the wandb SDK increments an internal step counter for each call to run.log
. This counter may not align with the training step in your training loop.
To avoid this situation, define your x-axis step explicitly with run.define_metric
, one time, immediately after you call wandb.init
:
*
, means that every metric will use global_step
as the x-axis in your charts. If you only want certain metrics to be logged against global_step
, you can specify them instead:
step
metric, and your global_step
each time you call run.log
:
Log images, tables, audio, and more
In addition to metrics, you can log plots, histograms, tables, text, and media such as images, videos, audios, 3D, and more. Some considerations when logging data include:- How often should the metric be logged? Should it be optional?
- What type of data could be helpful in visualizing?
- For images, you can log sample predictions, segmentation masks, etc., to see the evolution over time.
- For text, you can log tables of sample predictions for later exploration.
Distributed training
For frameworks supporting distributed environments, you can adapt any of the following workflows:- Detect which is the “main” process and only use
wandb
there. Any required data coming from other processes must be routed to the main process first. (This workflow is encouraged). - Call
wandb
in every process and auto-group them by giving them all the same uniquegroup
name.
Log model checkpoints and more
If your framework uses or produces models or datasets, you can log them for full traceability and have wandb automatically monitor your entire pipeline through W&B Artifacts.
- The ability to log model checkpoints or datasets (in case you want to make it optional).
- The path/reference of the artifact being used as input, if any. For example,
user/project/artifact
. - The frequency for logging Artifacts.
Log model checkpoints
You can log Model Checkpoints to W&B. It is useful to leverage the uniquewandb
Run ID to name output Model Checkpoints to differentiate them between Runs. You can also add useful metadata. In addition, you can also add aliases to each model as shown below:
Log and track pre-trained models or datasets
You can log artifacts that are used as inputs to your training such as pre-trained models or datasets. The following snippet demonstrates how to log an Artifact and add it as an input to the ongoing Run as shown in the graph above.Download an artifact
You re-use an Artifact (dataset, model, etc.) andwandb
will download a copy locally (and cache it):
latest
, v2
, v3
) or manually when logging (best_accuracy
, etc.).
To download an Artifact without creating a wandb
run (through wandb.init
), for example in distributed environments or for simple inference, you can instead reference the artifact with the wandb API: