{"record":{"id":"fec67e7d92685d20","repo":"calesthio/OpenMontage","slug":"non-json-response-from-tokenhub-api-http-respons","errorCode":null,"errorMessage":"Non-JSON response from TokenHub API: HTTP {response.status_code}","messagePattern":"Non-JSON response from TokenHub API: HTTP (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"tools/graphics/hunyuan_image.py","lineNumber":545,"sourceCode":"    # ------------------------------------------------------------------\n\n    @staticmethod\n    def _safe_error(exc: Exception) -> str:\n        \"\"\"Redact secret values from exception messages.\"\"\"\n        msg = str(exc)\n        for var in (\"TENCENT_TOKENHUB_API_KEY\",):\n            val = os.environ.get(var, \"\")\n            if val:\n                msg = msg.replace(val, \"[redacted]\")\n        return msg\n\n    @staticmethod\n    def _json_or_raise(response: Any) -> dict[str, Any]:\n        \"\"\"Parse JSON response body or raise with HTTP status.\"\"\"\n        try:\n            return response.json()\n        except ValueError as exc:\n            raise RuntimeError(\n                f\"Non-JSON response from TokenHub API: HTTP {response.status_code}\"\n            ) from exc\n\n    @staticmethod\n    def _check_response(payload: dict[str, Any]) -> None:\n        \"\"\"Check the TokenHub API response for errors.\n\n        TokenHub returns errors at the top level with an ``error`` field.\n        \"\"\"\n        error = payload.get(\"error\")\n        if error:\n            message = error.get(\"message\", \"unknown error\")\n            code = error.get(\"code\", error.get(\"type\", \"unknown\"))\n            raise RuntimeError(\n                f\"TokenHub API error: code={code}, message={message}\"\n            )\n\n    # ------------------------------------------------------------------","sourceCodeStart":527,"sourceCodeEnd":563,"githubUrl":"https://github.com/calesthio/OpenMontage/blob/95e1c3d0ab93482159818560f6a8c8e866b9139f/tools/graphics/hunyuan_image.py#L527-L563","documentation":"RuntimeError from _json_or_raise when a TokenHub HTTP response body cannot be parsed as JSON (requests raises ValueError internally). It typically indicates a non-200 status with an HTML/text error page, or a gateway/CDN intercepting the request, rather than an API-level error.","triggerScenarios":"Expired or invalid API key causing an auth proxy to return an HTML 401/403 page; Cloudflare-style challenge pages; wrong _HOST value hitting a non-API server; rate-limit responses with plain-text bodies; transient 502/504 HTML from load balancers.","commonSituations":"TENCENT_TOKENHUB_API_KEY not set or expired; corporate proxy or VPN mangling responses; regional endpoint hostname misconfigured; scraping a status page instead of the API host.","solutions":["Inspect response.status_code (it is in the message) and the raw body (response.text) to identify the interceptor","Verify TENCENT_TOKENHUB_API_KEY is set, valid, and has TokenHub entitlements","Confirm the API host/path constants match the documented TokenHub endpoint for your region","Retry transient 5xx gateway errors after a short backoff"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    data = client._json_or_raise(resp)\nexcept RuntimeError as e:\n    if \"Non-JSON\" in str(e):\n        status = resp.status_code\n        if status >= 500:\n            time.sleep(2 ** attempt)  # gateway hiccup: back off and retry\n        else:\n            raise  # 4xx HTML page = auth/proxy/config problem, not retryable","preventionTips":["Verify TENCENT_TOKENHUB_API_KEY validity and entitlements up front","Confirm the API host constant matches your regional endpoint","Treat HTML bodies as infrastructure/auth signals, never parse them as API data"],"tags":["hunyuan","tokenhub","http","auth","gateway"],"backgroundTag":null,"analyzedSha":"95e1c3d0ab93482159818560f6a8c8e866b9139f","analyzedAt":"2026-08-15T06:31:20.014Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}