AuthenticationError
mistral.client.errors.AuthenticationError
Stack trace
mistral.client.errors.AuthenticationError: Invalid API key provided. Please check your API key and try again.
File "app.py", line 12, in <module>
client = Mistral(api_key=os.environ['MISTRAL_API_KEY'])
File "mistral/client.py", line 45, in __init__
raise AuthenticationError("Invalid API key provided.") Why it happens
This error occurs when the Mistral client attempts to authenticate with the API server using an invalid, expired, or missing API key. The server rejects the request with a 401 Unauthorized status, causing the client to raise AuthenticationError.
Detection
Check for AuthenticationError exceptions when initializing the Mistral client or making API calls, and verify that the API key environment variable is set and correctly loaded before client instantiation.
Causes & fixes
API key environment variable MISTRAL_API_KEY is not set or is empty
Set the MISTRAL_API_KEY environment variable with your valid API key before running the application.
API key value is incorrect or expired
Obtain a new valid API key from the Mistral dashboard and update the environment variable accordingly.
API key is passed incorrectly to the Mistral client constructor
Ensure the API key is passed as the 'api_key' parameter when creating the Mistral client instance.
Code: broken vs fixed
from mistral import Mistral
client = Mistral() # Missing api_key parameter causes AuthenticationError
response = client.chat("Hello") import os
from mistral import Mistral
# Fixed: Pass API key from environment variable
client = Mistral(api_key=os.environ['MISTRAL_API_KEY'])
response = client.chat("Hello")
print(response) Workaround
Catch AuthenticationError exceptions and prompt for manual API key input or reload environment variables dynamically before retrying the client initialization.
Prevention
Always securely store and load your API key from environment variables or secret managers and validate the key before client usage to avoid authentication failures.