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
- 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
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
- 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
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
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Failed to obtain Turnstile token for DeepInfra request.
- {message or html}
- No Yupp accounts configured. Set YUPP_API_KEY environment va
- Yupp request failed: {str(e)}
- hCaptcha accessibility cookie required: log in at https://da
AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14).
Data as JSON: /api/errors/85b3c9a1c3bbf9aa.
Report an issue: GitHub.