High severity HTTP 401 beginner · Fix: 2-5 min

AuthenticationError

cohere.error.AuthenticationError

What this error means
Cohere rerank API key invalid error means the provided API key is missing, expired, or incorrect, blocking reranking requests.

Stack trace

traceback
cohere.error.AuthenticationError: Invalid API key provided. Please check your COHERE_API_KEY environment variable and try again.
QUICK FIX
Ensure COHERE_API_KEY environment variable is set to a valid, active API key before calling rerank.

Why it happens

This error occurs when the Cohere client receives a 401 Unauthorized response due to an invalid or missing API key. The rerank endpoint requires a valid API key set in the environment or client configuration. If the key is expired, revoked, or mistyped, authentication fails.

Detection

Check for AuthenticationError exceptions when calling the rerank method and verify the COHERE_API_KEY environment variable is set and correct before making requests.

Causes & fixes

1

API key environment variable COHERE_API_KEY is not set or is empty

✓ Fix

Set the COHERE_API_KEY environment variable with your valid Cohere API key before running your application.

2

API key provided is expired or revoked in the Cohere dashboard

✓ Fix

Generate a new API key from the Cohere dashboard and update your environment variable accordingly.

3

Typo or incorrect API key string used in environment or client initialization

✓ Fix

Double-check the API key string for typos and ensure it matches exactly the key from your Cohere account.

4

Using an API key with insufficient permissions for reranking endpoint

✓ Fix

Verify your API key has reranking access enabled in your Cohere account settings.

Code: broken vs fixed

Broken - triggers the error
python
import os
from cohere import Client

client = Client(api_key=None)  # Missing API key

response = client.rerank(
    model='rerank-english-v2.0',
    query='What is AI?',
    documents=['AI is artificial intelligence.', 'AI stands for artificial intelligence.']
)  # This line raises AuthenticationError
print(response)
Fixed - works correctly
python
import os
from cohere import Client

client = Client(api_key=os.environ['COHERE_API_KEY'])  # Pass API key correctly

response = client.rerank(
    model='rerank-english-v2.0',
    query='What is AI?',
    documents=['AI is artificial intelligence.', 'AI stands for artificial intelligence.']
)  # Fixed: valid API key used
print(response)
Added passing of a valid COHERE_API_KEY environment variable to authenticate rerank calls successfully.

Workaround

Catch AuthenticationError exceptions and prompt the user or system to reload or reset the API key environment variable dynamically before retrying.

Prevention

Use environment management tools to securely store and inject valid API keys, and implement startup checks that verify API key validity before making rerank requests.

Python 3.7+ · cohere >=5.0.0 · tested on 5.3.0
Verified 2026-04
Verify ↗

Community Notes

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