AuthenticationError
langfuse.errors.AuthenticationError
Stack trace
langfuse.errors.AuthenticationError: Invalid or missing API keys: public key or secret key not provided or incorrect
File "/app/main.py", line 42, in <module>
client = LangfuseClient(public_key=os.environ['LANGFUSE_PUBLIC_KEY'], secret_key=os.environ['LANGFUSE_SECRET_KEY'])
File "/usr/local/lib/python3.9/site-packages/langfuse/client.py", line 88, in __init__
raise AuthenticationError("Invalid or missing API keys")
Why it happens
Langfuse requires both a valid public key and secret key for authentication. This error happens when either key is missing, empty, or incorrect, causing the client to reject the connection. Environment variables may be unset or keys may be mistyped.
Detection
Check for AuthenticationError exceptions during client initialization or API calls. Log and verify that environment variables LANGFUSE_PUBLIC_KEY and LANGFUSE_SECRET_KEY are set and non-empty before client creation.
Causes & fixes
Environment variables LANGFUSE_PUBLIC_KEY or LANGFUSE_SECRET_KEY are not set or empty
Set the environment variables LANGFUSE_PUBLIC_KEY and LANGFUSE_SECRET_KEY with your valid Langfuse API keys before running the application.
Using incorrect or expired API keys for Langfuse authentication
Verify your API keys in the Langfuse dashboard and update your environment variables with the current valid keys.
Passing API keys as empty strings or incorrect parameter names to LangfuseClient
Ensure you pass the keys exactly as public_key and secret_key parameters with correct values from environment variables.
Code: broken vs fixed
from langfuse import LangfuseClient
import os
client = LangfuseClient(public_key="", secret_key="") # This triggers AuthenticationError from langfuse import LangfuseClient
import os
# Fixed: Use environment variables for keys
client = LangfuseClient(
public_key=os.environ['LANGFUSE_PUBLIC_KEY'],
secret_key=os.environ['LANGFUSE_SECRET_KEY']
)
print("Langfuse client initialized successfully") Workaround
Catch AuthenticationError and prompt for manual key input or fallback to a limited functionality mode until valid keys are provided.
Prevention
Use secure environment management to inject valid Langfuse API keys at runtime and validate keys presence before client initialization to avoid auth failures.