{"record":{"id":"ba5d5760a0ffee18","repo":"xtekky/gpt4free","slug":"add-a-api-key-ba5d57","errorCode":null,"errorMessage":"Add a \"api_key\"","messagePattern":"Add a \"api_key\"","errorType":"exception","errorClass":"MissingAuthError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/needs_auth/Cohere.py","lineNumber":72,"sourceCode":"    async def create_async_generator(\n        cls,\n        model: str,\n        messages: Messages,\n        proxy: str = None,\n        timeout: int = 120,\n        api_key: str = None,\n        temperature: float = None,\n        max_tokens: int = None,\n        top_k: int = None,\n        top_p: float = None,\n        stop: list[str] = None,\n        stream: bool = True,\n        headers: dict = None,\n        impersonate: str = None,\n        **kwargs,\n    ) -> AsyncResult:\n        if api_key is None:\n            raise MissingAuthError('Add a \"api_key\"')\n\n        async with StreamSession(\n            proxy=proxy,\n            headers=cls.get_headers(stream, api_key, headers),\n            timeout=timeout,\n            impersonate=impersonate,\n        ) as session:\n            data = filter_none(\n                messages=messages,\n                model=cls.get_model(model, api_key=api_key),\n                temperature=temperature,\n                max_tokens=max_tokens,\n                k=top_k,\n                p=top_p,\n                stop_sequences=stop,\n                stream=stream,\n            )\n            async with session.post(cls.api_endpoint, json=data) as response:","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/needs_auth/Cohere.py#L54-L90","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\nresp = await client.chat.completions.create(model=\"command-r\", provider=Cohere, messages=msgs)\n# MissingAuthError: Add a \"api_key\"\n\n# after\nresp = await client.chat.completions.create(model=\"command-r\", provider=Cohere, messages=msgs, api_key=os.environ[\"COHERE_API_KEY\"])","handlingStrategy":"validation","validationCode":"import os\n\ndef cohere_key_available():\n    return bool(os.environ.get(\"COHERE_API_KEY\"))\n\nif not cohere_key_available():\n    raise SystemExit(\"Set COHERE_API_KEY before using the Cohere provider\")","typeGuard":null,"tryCatchPattern":"try:\n    resp = await client.chat.completions.create(model=m, provider=Cohere, messages=msgs, api_key=key)\nexcept MissingAuthError:\n    key = os.environ.get(\"COHERE_API_KEY\")\n    if not key:\n        raise","preventionTips":["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."],"tags":["authentication","api-key","cohere"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}