AuthenticationError
wandb.errors.CommError.AuthenticationError
Stack trace
wandb.errors.CommError.AuthenticationError: API key not set. Please set your WANDB_API_KEY environment variable or run `wandb login`.
Why it happens
Weights & Biases requires an API key to authenticate your client with their servers. If the API key is missing from environment variables or not configured via `wandb login`, the client cannot authenticate and raises this error.
Detection
Check for AuthenticationError exceptions when initializing wandb or starting a run; verify if WANDB_API_KEY is set in your environment before running your script.
Causes & fixes
WANDB_API_KEY environment variable is not set or empty
Set your API key in the environment by running `export WANDB_API_KEY='your_api_key'` on Linux/macOS or `setx WANDB_API_KEY 'your_api_key'` on Windows.
User did not run `wandb login` to configure API key locally
Run `wandb login` in your terminal and enter your API key to store it securely for wandb usage.
API key is set but in the wrong environment or session
Ensure the environment variable is set in the same shell or environment where your Python script runs, including IDE or container environments.
Code: broken vs fixed
import wandb
wandb.init(project='my-project') # triggers AuthenticationError if API key not set import os
import wandb
os.environ['WANDB_API_KEY'] = 'your_api_key_here' # Set your API key securely
wandb.init(project='my-project') # now authenticates successfully
print('wandb run started') Workaround
Catch AuthenticationError and prompt the user to set the API key or fallback to offline mode with `wandb.init(mode='offline')` to avoid crashes.
Prevention
Always configure your W&B API key in environment variables or run `wandb login` before running scripts that use wandb to ensure seamless authentication.