AuthenticationError
openai.AuthenticationError (HTTP 401)
Stack trace
openai.AuthenticationError: Invalid API key provided: You must provide a valid API key.
File "/app/haystack/generator.py", line 45, in generate
response = client.chat.completions.create(...)
File "/usr/local/lib/python3.9/site-packages/openai/api_resources/chat_completion.py", line 30, in create
raise openai.AuthenticationError("Invalid API key provided") Why it happens
The OpenAI API requires a valid API key passed via environment variables or configuration. If the key is missing, empty, or invalid, the OpenAI client raises an AuthenticationError with HTTP 401. Haystack's OpenAIGenerator depends on this key to authenticate requests.
Detection
Check for openai.AuthenticationError exceptions during generation calls and verify that the environment variable OPENAI_API_KEY is set and correctly loaded before initializing OpenAIGenerator.
Causes & fixes
OPENAI_API_KEY environment variable is not set or is empty
Set the OPENAI_API_KEY environment variable to your valid OpenAI API key before running your Haystack application.
OPENAI_API_KEY is set but contains an invalid or revoked key
Verify your API key in the OpenAI dashboard and update the environment variable with a valid, active key.
Haystack OpenAIGenerator is initialized before environment variables are loaded
Ensure environment variables are loaded (e.g., via os.environ or dotenv) before creating the OpenAIGenerator instance.
Code: broken vs fixed
from haystack.nodes import OpenAIGenerator
generator = OpenAIGenerator() # Missing API key causes AuthenticationError
response = generator.generate(query="Hello") import os
from haystack.nodes import OpenAIGenerator
os.environ["OPENAI_API_KEY"] = "your_valid_api_key_here" # Set your API key securely
generator = OpenAIGenerator() # Now uses the valid API key
response = generator.generate(query="Hello")
print(response) Workaround
Catch openai.AuthenticationError exceptions and prompt the user or system to set a valid OPENAI_API_KEY environment variable before retrying.
Prevention
Use secure environment management to ensure OPENAI_API_KEY is always set and valid before application startup, and validate keys during deployment pipelines.