ValueError
ValueError: COHERE_API_KEY environment variable is not set
Stack trace
Traceback (most recent call last):
File "search.py", line 42, in <module>
reranker = CohereRerank()
File "/usr/local/lib/python3.11/site-packages/langchain_community/document_compressors/cohere_rerank.py", line 45, in __init__
if not (api_key := get_from_env("cohere_api_key", "COHERE_API_KEY")):
ValueError: COHERE_API_KEY environment variable is not set. Get an API key at https://dashboard.cohere.com/ Why it happens
CohereRerank requires authentication to the Cohere API to perform re-ranking. The library attempts to read the COHERE_API_KEY environment variable at initialization time. If this variable is not set, not exported to the Python process, or set after the import statement, the ValueError is raised immediately. This is the first line of defense against authentication failures and prevents wasted API calls with invalid credentials.
Detection
Check environment variables before launching your application: `echo $COHERE_API_KEY`. In your Python process, verify the variable is visible: `import os; print(os.environ.get('COHERE_API_KEY'))`. If None is printed, the variable was not exported to this shell session.
Causes & fixes
COHERE_API_KEY not exported to shell environment before running Python script
Run `export COHERE_API_KEY='your-key-here'` in your terminal before executing Python, or set it inline: `COHERE_API_KEY='your-key-here' python script.py`
COHERE_API_KEY set in .env file but python-dotenv not loaded
Add `from dotenv import load_dotenv; load_dotenv()` at the very top of your script before any LangChain imports
COHERE_API_KEY set with typo or wrong case (e.g., Cohere_Api_Key, cohere_api_key)
Environment variable names are case-sensitive. Use exactly COHERE_API_KEY (all uppercase with underscores)
API key obtained but not yet activated on Cohere dashboard, or subscription expired
Visit https://dashboard.cohere.com/, verify your API key is listed under API Keys, check your plan is active, and regenerate the key if it's older than 90 days
Code: broken vs fixed
import os
from langchain_community.document_compressors import CohereRerank
# BROKEN: No environment variable set — this will crash immediately
reranker = CohereRerank() # ValueError: COHERE_API_KEY environment variable is not set
compressed_docs = reranker.compress_documents(
documents=docs,
query="What is vector search?"
) import os
from dotenv import load_dotenv
from langchain_community.document_compressors import CohereRerank
# FIXED: Load API key from .env file first
load_dotenv() # Added: loads COHERE_API_KEY from .env
# Verify the key is available before proceeding
if not os.environ.get('COHERE_API_KEY'):
raise ValueError(
'COHERE_API_KEY not found. '
'Set it in .env or export COHERE_API_KEY=\'your-key-here\''
)
# Now CohereRerank can initialize successfully
reranker = CohereRerank(model="rerank-english-v3.0")
compressed_docs = reranker.compress_documents(
documents=docs,
query="What is vector search?"
)
print(f"Reranked {len(compressed_docs)} documents") Workaround
Pass the API key directly to CohereRerank's constructor instead of relying on environment variables: `reranker = CohereRerank(cohere_api_key=os.environ.get('COHERE_API_KEY') or 'your-key-here')`: this trades security (key in code) for immediate functionality, suitable only for development or testing.
Prevention
1) Use a secrets manager (AWS Secrets Manager, HashiCorp Vault, or GitHub Actions Secrets) in production instead of .env files. 2) Verify all API key environment variables are set before application startup using a configuration validation function. 3) Use service accounts with minimal-scoped API keys (Cohere supports key restrictions by IP/domain). 4) Never commit .env or credentials to version control: add .env to .gitignore and document required env vars in README.