High severity beginner · Fix: 2-5 min

TypeError

TypeError: __init__() got an unexpected keyword argument 'api_key'

What this error means
This error occurs when using Langfuse's OpenAI drop-in replacement client with incorrect or unsupported initialization parameters.

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 10, in <module>
    client = LangfuseOpenAI(api_key=os.environ['OPENAI_API_KEY'])  # TypeError here
TypeError: __init__() got an unexpected keyword argument 'api_key'
QUICK FIX
Remove 'api_key' from Langfuse client constructor and rely on environment variables for API key configuration.

Why it happens

Langfuse's OpenAI drop-in replacement client does not accept the 'api_key' parameter in its constructor, unlike the official OpenAI SDK. Passing 'api_key' causes a TypeError because the constructor signature differs. This happens when developers assume identical initialization parameters between Langfuse and OpenAI clients.

Detection

Detect this error by catching TypeError exceptions during client initialization and verifying if 'api_key' is passed as a parameter to Langfuse's client constructor.

Causes & fixes

1

Passing 'api_key' parameter to Langfuse OpenAI client constructor which does not accept it.

✓ Fix

Remove the 'api_key' argument from the Langfuse client initialization and instead set the API key via environment variables or Langfuse-specific configuration.

2

Confusing Langfuse client initialization with OpenAI SDK v1+ initialization patterns.

✓ Fix

Refer to Langfuse documentation for correct client instantiation and avoid using OpenAI SDK constructor parameters directly.

3

Not setting environment variable 'OPENAI_API_KEY' before initializing Langfuse client.

✓ Fix

Ensure 'OPENAI_API_KEY' is set in the environment before running the application so Langfuse client can read it automatically.

Code: broken vs fixed

Broken - triggers the error
python
import os
from langfuse import LangfuseOpenAI

client = LangfuseOpenAI(api_key=os.environ['OPENAI_API_KEY'])  # TypeError here
response = client.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": "Hello"}])
print(response.choices[0].message.content)
Fixed - works correctly
python
import os
from langfuse import LangfuseOpenAI

# Removed api_key argument; rely on environment variable OPENAI_API_KEY
client = LangfuseOpenAI()
response = client.chat.completions.create(model="gpt-4o-mini", messages=[{"role": "user", "content": "Hello"}])
print(response.choices[0].message.content)  # Fixed initialization
Removed the unsupported 'api_key' parameter from Langfuse client initialization to match Langfuse's expected constructor signature and rely on environment variables.

Workaround

If you cannot remove 'api_key' immediately, wrap the client initialization in try/except TypeError and fallback to initializing without parameters, ensuring environment variables are set.

Prevention

Always consult Langfuse's official docs for client initialization patterns and avoid assuming OpenAI SDK constructor compatibility; use environment variables for API keys.

Python 3.9+ · langfuse >=0.1.0 · tested on 0.2.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.