AuthenticationError
cerebras.client.AuthenticationError
Stack trace
cerebras.client.AuthenticationError: Invalid API key provided. Please check your API key and try again.
File "app.py", line 12, in <module>
client = CerebrasClient(api_key="wrong_key")
File "cerebras/client.py", line 45, in __init__
self.authenticate()
File "cerebras/client.py", line 78, in authenticate
raise AuthenticationError("Invalid API key provided.") Why it happens
The Cerebras SDK requires a valid API key for authentication. If the key is missing, malformed, expired, or revoked, the SDK raises AuthenticationError to prevent unauthorized access.
Detection
Check for AuthenticationError exceptions during client initialization or API calls; log the error message to detect invalid or missing API keys before retrying.
Causes & fixes
API key environment variable is not set or misspelled
Ensure the environment variable holding the API key (e.g., CEREBRAS_API_KEY) is correctly set and accessed in your code.
Using an expired or revoked API key
Generate a new API key from the Cerebras dashboard and update your environment variable accordingly.
Passing the API key directly as a hardcoded string with typos
Use os.environ to load the API key securely and verify the key string matches exactly what is provided by Cerebras.
Incorrect client initialization without passing the API key
Pass the API key explicitly when creating the Cerebras client instance or configure the SDK to read it from environment variables.
Code: broken vs fixed
from cerebras import CerebrasClient
client = CerebrasClient(api_key="wrong_or_missing_key") # This triggers AuthenticationError
response = client.run_model(input_data) import os
from cerebras import CerebrasClient
# Load API key securely from environment variable
api_key = os.environ.get("CEREBRAS_API_KEY")
client = CerebrasClient(api_key=api_key) # Fixed: use correct env var
response = client.run_model(input_data)
print(response) Workaround
Catch AuthenticationError exceptions and prompt for re-authentication or fallback to a cached valid token if available, while logging the failure.
Prevention
Use environment variables or secure vaults to manage API keys, rotate keys regularly, and validate keys on startup to prevent invalid authentication attempts.