xtekky/gpt4free · error · MissingAuthError
Add a "api_key"
Error message
Add a "api_key"
What it means
MissingAuthError raised by g4f/Provider/needs_auth/Cohere.py at the top of create_async_generator: api_key is None and no fallback exists, because Cohere's API requires a Bearer token on every request (see get_headers, which only adds Authorization when api_key is not None). The provider fails fast before opening the StreamSession.
Source
Thrown at g4f/Provider/needs_auth/Cohere.py:72
async def create_async_generator(
cls,
model: str,
messages: Messages,
proxy: str = None,
timeout: int = 120,
api_key: str = None,
temperature: float = None,
max_tokens: int = None,
top_k: int = None,
top_p: float = None,
stop: list[str] = None,
stream: bool = True,
headers: dict = None,
impersonate: str = None,
**kwargs,
) -> AsyncResult:
if api_key is None:
raise MissingAuthError('Add a "api_key"')
async with StreamSession(
proxy=proxy,
headers=cls.get_headers(stream, api_key, headers),
timeout=timeout,
impersonate=impersonate,
) as session:
data = filter_none(
messages=messages,
model=cls.get_model(model, api_key=api_key),
temperature=temperature,
max_tokens=max_tokens,
k=top_k,
p=top_p,
stop_sequences=stop,
stream=stream,
)
async with session.post(cls.api_endpoint, json=data) as response:View on GitHub (pinned to 973504e177)
Solutions
- Get a free API key from dashboard.cohere.com and pass api_key="..." to the call.
- Or register the key in g4f's provider settings so g4f injects it automatically.
- Confirm you are not hitting a subclass of Cohere that expects a different auth mechanism.
Example fix
# before resp = await client.chat.completions.create(model="command-r", provider=Cohere, messages=msgs) # MissingAuthError: Add a "api_key" # after resp = await client.chat.completions.create(model="command-r", provider=Cohere, messages=msgs, api_key=os.environ["COHERE_API_KEY"])
Defensive patterns
Strategy: validation
Validate before calling
import os
def cohere_key_available():
return bool(os.environ.get("COHERE_API_KEY"))
if not cohere_key_available():
raise SystemExit("Set COHERE_API_KEY before using the Cohere provider") Try / catch
try:
resp = await client.chat.completions.create(model=m, provider=Cohere, messages=msgs, api_key=key)
except MissingAuthError:
key = os.environ.get("COHERE_API_KEY")
if not key:
raise Prevention
- Always pass api_key or configure it in g4f settings before first use.
- Keep the key in an env var / secret manager, not in code.
- Fail fast at startup when the key is missing.
When it happens
Trigger: Calling Cohere (or a subclass) through g4f without an api_key argument and without an api_key configured in g4f's settings for the provider. The very first check in create_async_generator raises.
Common situations: New g4f setup where the Cohere key was never added via g4f's settings/CLI; forgetting to pass api_key when invoking the provider directly; key stored under a different provider name.
Related errors
- No Yupp accounts configured. Set YUPP_API_KEY environment va
- Cannot validate GLM account (status {response.status})
- API key has failed too many times.
- Add a "api_key"
- API key is required for {cls.__name__}
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/ba5d5760a0ffee18.
Report an issue: GitHub.