High severity beginner · Fix: 2-5 min

ValueError

langchain.langsmith.client.ValueError

What this error means
LangSmith client initialization fails because the LANGCHAIN_API_KEY environment variable is not set or accessible.

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 10, in <module>
    client = LangSmithClient()
  File "langchain/langsmith/client.py", line 45, in __init__
    raise ValueError("LANGCHAIN_API_KEY environment variable not set")
ValueError: LANGCHAIN_API_KEY environment variable not set
QUICK FIX
Set the LANGCHAIN_API_KEY environment variable in your OS environment before running your Python app.

Why it happens

The LangSmith client requires the LANGCHAIN_API_KEY environment variable to authenticate API requests. If this environment variable is missing or not loaded into the runtime environment, the client raises a ValueError to prevent unauthorized calls.

Detection

Check for the presence of LANGCHAIN_API_KEY in your environment variables before initializing LangSmithClient, or catch ValueError exceptions during client creation and log the missing key error.

Causes & fixes

1

LANGCHAIN_API_KEY environment variable is not set in the OS environment

✓ Fix

Set the LANGCHAIN_API_KEY environment variable in your shell or deployment environment before running the application.

2

Environment variable is set but not loaded into the Python process environment

✓ Fix

Ensure your runtime loads environment variables correctly, for example by using python-dotenv or exporting variables in the same shell session.

3

Code attempts to instantiate LangSmithClient before environment variables are loaded

✓ Fix

Load environment variables at the very start of your script or application entry point before creating the LangSmithClient instance.

Code: broken vs fixed

Broken - triggers the error
python
from langchain.langsmith import LangSmithClient

client = LangSmithClient()  # Raises ValueError: LANGCHAIN_API_KEY environment variable not set
Fixed - works correctly
python
import os
from langchain.langsmith import LangSmithClient

os.environ["LANGCHAIN_API_KEY"] = os.environ.get("LANGCHAIN_API_KEY", "your_api_key_here")  # Set your API key securely
client = LangSmithClient()  # Now works without error
print("LangSmith client initialized successfully")
Added setting of LANGCHAIN_API_KEY environment variable before client initialization to provide required authentication key.

Workaround

Wrap LangSmithClient initialization in try/except ValueError, and prompt the user or log a clear message to set LANGCHAIN_API_KEY before retrying.

Prevention

Use environment management tools like dotenv or container environment variables to ensure LANGCHAIN_API_KEY is always set before app startup, avoiding runtime authentication errors.

Python 3.9+ · langchain >=0.2.0 · tested on 0.3.x
Verified 2026-04
Verify ↗

Community Notes

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