xtekky/gpt4free · error · MissingAuthError

Failed to decode JSON: {e}

Error message

Failed to decode JSON: {e}

What it means

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.

Source

Thrown at g4f/Provider/needs_auth/hf/HuggingChat.py:216

                yield Reasoning(line.get("token"), status=line.get("status"))

    @classmethod
    def create_conversation(cls, session: Session, model: str):
        if model in cls.image_models:
            model = cls.default_model
        json_data = {
            "model": model,
        }
        response = session.post(f"{cls.url}/conversation", json=json_data)
        if response.status_code == 401:
            raise MissingAuthError(response.text)
        if response.status_code == 400:
            raise ResponseError(f"{response.text}: Model: {model}")
        raise_for_status(response)
        try:
            return response.json().get("conversationId")
        except json.JSONDecodeError as e:
            raise MissingAuthError(f"Failed to decode JSON: {e}") from e

    @classmethod
    def fetch_message_id(cls, session: Session, conversation_id: str):
        response = session.get(f"{cls.url}/api/v2/conversations/{conversation_id}")
        raise_for_status(response)

        try:
            data = response.json()["json"]
        except json.JSONDecodeError as e:
            debug.error(f"Failed to decode JSON: {e}")
            return None

        messages_data_list = data.get("messages", [])
        return messages_data_list[-1]["id"] if messages_data_list else None

View on GitHub (pinned to 973504e177)

Solutions

  1. Refresh HuggingChat cookies — an HTML auth page instead of JSON usually means stale session
  2. Route through a residential/normal egress IP or proxy to avoid Cloudflare challenges
  3. Ensure curl_cffi is installed so the TLS fingerprint looks browser-like
  4. Log response.text before parsing to identify what page was returned
Defensive patterns

Strategy: try-catch

Try / catch

from g4f.errors import MissingAuthError
try:
    conv_id = HuggingChat.create_conversation(session, model)
except MissingAuthError as e:
    if 'Failed to decode JSON' in str(e):
        # HTML page instead of JSON: Cloudflare or auth wall
        refresh_cookies_or_change_proxy()
        raise

Prevention

When it happens

Trigger: 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.

Common situations: Datacenter IPs triggering Cloudflare on huggingface.co; cookie present but session expired mid-flow; corporate proxies rewriting responses.

Understand the failure class

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/85b3c9a1c3bbf9aa. Report an issue: GitHub.