AuthenticationError
replicate.exceptions.AuthenticationError
Stack trace
replicate.exceptions.AuthenticationError: Authentication failed: REPLICATE_API_TOKEN missing or invalid
File "app.py", line 12, in <module>
client = replicate.Client()
File "replicate/client.py", line 45, in __init__
raise AuthenticationError("REPLICATE_API_TOKEN missing") Why it happens
The Replicate Python client requires a valid API token set in the REPLICATE_API_TOKEN environment variable for authentication. If this token is missing or empty, the client cannot authenticate requests and raises AuthenticationError.
Detection
Check for AuthenticationError exceptions when initializing the Replicate client or making API calls, and verify that REPLICATE_API_TOKEN is set in the environment before runtime.
Causes & fixes
REPLICATE_API_TOKEN environment variable is not set in the system or runtime environment
Set the REPLICATE_API_TOKEN environment variable with your valid Replicate API token before running your Python application.
REPLICATE_API_TOKEN is set but empty or contains only whitespace
Ensure REPLICATE_API_TOKEN contains the full valid token string without extra spaces or line breaks.
Using an outdated Replicate client version that does not support current authentication methods
Upgrade the replicate package to the latest version using pip install --upgrade replicate.
Code: broken vs fixed
import replicate
client = replicate.Client() # Raises AuthenticationError if REPLICATE_API_TOKEN missing
print(client.models.list()) import os
import replicate
os.environ["REPLICATE_API_TOKEN"] = "your_valid_token_here" # Set your token securely
client = replicate.Client() # Now authenticates successfully
print(client.models.list()) Workaround
Manually pass the API token as a parameter to replicate.Client(api_token='your_token') if setting environment variables is not possible immediately.
Prevention
Use environment management tools or secrets managers to securely inject REPLICATE_API_TOKEN into your runtime environment to avoid missing token errors.