{"record":{"id":"cb24355b2c97c4f9","repo":"xtekky/gpt4free","slug":"cohere-api-error-data-error","errorCode":null,"errorMessage":"Cohere API Error: {data['error']}","messagePattern":"Cohere API Error: (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/needs_auth/Cohere.py","lineNumber":153,"sourceCode":"                                            \"billed_units\"\n                                        ),\n                                    )\n\n    @classmethod\n    def get_headers(\n        cls, stream: bool, api_key: str = None, headers: dict = None\n    ) -> dict:\n        return {\n            \"Accept\": \"text/event-stream\" if stream else \"application/json\",\n            \"Content-Type\": \"application/json\",\n            **({\"Authorization\": f\"Bearer {api_key}\"} if api_key is not None else {}),\n            **({} if headers is None else headers),\n        }\n\n    @classmethod\n    def raise_error(cls, data: dict):\n        if \"error\" in data:\n            raise RuntimeError(f\"Cohere API Error: {data['error']}\")\n","sourceCodeStart":135,"sourceCodeEnd":154,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/needs_auth/Cohere.py#L135-L154","documentation":"RuntimeError raised by Cohere.raise_error when the parsed response JSON contains an \"error\" key, i.e. Cohere's API accepted the request shape but rejected it semantically (invalid model, malformed messages, bad parameters, or an API-level failure). The raw error payload from Cohere is embedded verbatim in the message for diagnosis.","triggerScenarios":"Any Cohere API call whose JSON response body contains an error object — e.g. requesting a model your key cannot access, sending messages in an unsupported format, or exceeding Cohere-side limits that are reported in the body rather than the HTTP status.","commonSituations":"Using a model name deprecated/removed by Cohere; trial keys hitting feature-gated endpoints; passing chat-history formats Cohere rejects (e.g. invalid roles); Cohere incident returning error JSON with HTTP 200.","solutions":["Read the embedded Cohere error payload — it states the exact cause (bad model, invalid parameter, etc.) and fix accordingly.","Verify the model string against Cohere's current model list.","Check that your API key's plan/tier permits the requested model and features.","If the payload mentions overload/try-again, retry after a short backoff."],"exampleFix":"# before\nresp = await client.chat.completions.create(model=\"command-r-03-2024\", provider=Cohere, messages=msgs, api_key=key)\n# RuntimeError: Cohere API Error: {'message': 'model not found'}\n\n# after\nresp = await client.chat.completions.create(model=\"command-r\", provider=Cohere, messages=msgs, api_key=key)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    resp = await client.chat.completions.create(model=m, provider=Cohere, messages=msgs, api_key=key)\nexcept RuntimeError as e:\n    if \"Cohere API Error\" in str(e):\n        log_and_handle_cohere_payload(str(e))\n    else:\n        raise","preventionTips":["Validate model names against Cohere's current list before sending.","Log the embedded payload — it carries the exact upstream reason.","Keep the g4f SDK current so schema changes in Cohere errors stay parseable."],"tags":["api-error","cohere","response-parsing"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}