{"record":{"id":"71b7eb8e92f5a118","repo":"calesthio/OpenMontage","slug":"non-json-response-from-tokenhub-api-http-respons-71b7eb","errorCode":null,"errorMessage":"Non-JSON response from TokenHub API: HTTP {response.status_code}","messagePattern":"Non-JSON response from TokenHub API: HTTP (.+?)","errorType":"http","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"tools/video/hunyuan_cloud_video.py","lineNumber":514,"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","sourceCodeStart":496,"sourceCodeEnd":531,"githubUrl":"https://github.com/calesthio/OpenMontage/blob/95e1c3d0ab93482159818560f6a8c8e866b9139f/tools/video/hunyuan_cloud_video.py#L496-L531","documentation":"Raised by _json_or_raise when a TokenHub HTTP response body is not valid JSON (response.json() raises ValueError). The tool wraps the parse failure with the HTTP status code so you can tell an HTML error page (502/503 from a gateway) from a 200 with a malformed body.","triggerScenarios":"Any TokenHub request (submit or poll) whose response is HTML/XML/plain text — gateway 502/504 pages, WAF block pages, maintenance notices, or empty bodies on dropped connections.","commonSituations":"TokenHub gateway briefly restarting or overloaded; a WAF rejecting the request (large base64 payloads tripping rules); corporate proxies intercepting the connection; provider maintenance windows returning HTML.","solutions":["Retry with backoff — transient gateway errors are the most common cause and usually self-heal.","If reproducing consistently, capture response.text to see what the body actually is (WAF page, maintenance notice) before parsing.","Shrink the request payload (see the 6 MB image cap, error 249) if a WAF is rejecting large bodies.","Bypass intermediary proxies for the TokenHub host if one is in the path."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"last_err = None\nfor attempt in range(3):\n    try:\n        result = hunyuan_cloud_video(inputs)\n        break\n    except RuntimeError as e:\n        if \"Non-JSON response\" in str(e):\n            last_err = e\n            time.sleep(2 ** attempt * 5)  # gateway hiccup: backoff and retry\n        else:\n            raise\nelse:\n    raise last_err","preventionTips":["Wrap TokenHub calls in exponential-backoff retry for non-JSON/gateway errors.","Keep request payloads small (see the 6MB image cap) to avoid WAF rejections.","Avoid routing TokenHub traffic through body-rewriting proxies."],"tags":["hunyuan","tokenhub","non-json","gateway-error","network","parsing"],"backgroundTag":null,"analyzedSha":"95e1c3d0ab93482159818560f6a8c8e866b9139f","analyzedAt":"2026-08-15T06:31:20.014Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}