{"record":{"id":"85b3c9a1c3bbf9aa","repo":"xtekky/gpt4free","slug":"failed-to-decode-json-e","errorCode":null,"errorMessage":"Failed to decode JSON: {e}","messagePattern":"Failed to decode JSON: (.+?)","errorType":"exception","errorClass":"MissingAuthError","httpStatus":null,"severity":"error","filePath":"g4f/Provider/needs_auth/hf/HuggingChat.py","lineNumber":216,"sourceCode":"                yield Reasoning(line.get(\"token\"), status=line.get(\"status\"))\n\n    @classmethod\n    def create_conversation(cls, session: Session, model: str):\n        if model in cls.image_models:\n            model = cls.default_model\n        json_data = {\n            \"model\": model,\n        }\n        response = session.post(f\"{cls.url}/conversation\", json=json_data)\n        if response.status_code == 401:\n            raise MissingAuthError(response.text)\n        if response.status_code == 400:\n            raise ResponseError(f\"{response.text}: Model: {model}\")\n        raise_for_status(response)\n        try:\n            return response.json().get(\"conversationId\")\n        except json.JSONDecodeError as e:\n            raise MissingAuthError(f\"Failed to decode JSON: {e}\") from e\n\n    @classmethod\n    def fetch_message_id(cls, session: Session, conversation_id: str):\n        response = session.get(f\"{cls.url}/api/v2/conversations/{conversation_id}\")\n        raise_for_status(response)\n\n        try:\n            data = response.json()[\"json\"]\n        except json.JSONDecodeError as e:\n            debug.error(f\"Failed to decode JSON: {e}\")\n            return None\n\n        messages_data_list = data.get(\"messages\", [])\n        return messages_data_list[-1][\"id\"] if messages_data_list else None\n","sourceCodeStart":198,"sourceCodeEnd":231,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/Provider/needs_auth/hf/HuggingChat.py#L198-L231","documentation":"Raised by HuggingChat.create_conversation when the POST succeeds (non-401/400) but the response body is not valid JSON, so response.json() raises JSONDecodeError. It is raised as MissingAuthError because the usual cause is an HTML login/redirect page (Cloudflare or auth wall) returned instead of the expected {\"conversationId\": ...} JSON.","triggerScenarios":"Cloudflare interception of the session; cookies valid enough to avoid 401 but causing a redirect to an HTML page; proxy or captive portal mangling responses.","commonSituations":"Datacenter IPs triggering Cloudflare on huggingface.co; cookie present but session expired mid-flow; corporate proxies rewriting responses.","solutions":["Refresh HuggingChat cookies — an HTML auth page instead of JSON usually means stale session","Route through a residential/normal egress IP or proxy to avoid Cloudflare challenges","Ensure curl_cffi is installed so the TLS fingerprint looks browser-like","Log response.text before parsing to identify what page was returned"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"from g4f.errors import MissingAuthError\ntry:\n    conv_id = HuggingChat.create_conversation(session, model)\nexcept MissingAuthError as e:\n    if 'Failed to decode JSON' in str(e):\n        # HTML page instead of JSON: Cloudflare or auth wall\n        refresh_cookies_or_change_proxy()\n        raise","preventionTips":["Treat non-JSON 2xx responses as an auth/Cloudflare signal, not a parsing bug","Use residential egress IPs for huggingface.co in automation","Ensure curl_cffi is installed for browser-like TLS fingerprints"],"tags":["huggingchat","authentication","json-decoding","cloudflare"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}