{"record":{"id":"f5f205b6308cf45d","repo":"calesthio/OpenMontage","slug":"non-json-response-from-dashscope-api-http-respon","errorCode":null,"errorMessage":"Non-JSON response from DashScope API: HTTP {response.status_code}","messagePattern":"Non-JSON response from DashScope API: HTTP (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"tools/analysis/dashscope_asr.py","lineNumber":365,"sourceCode":"                    words.append(\n                        {\n                            \"text\": word.get(\"text\", \"\"),\n                            \"begin_time_seconds\": round(\n                                word.get(\"begin_time\", 0) / 1000.0, 3\n                            ),\n                            \"end_time_seconds\": round(\n                                word.get(\"end_time\", 0) / 1000.0, 3\n                            ),\n                        }\n                    )\n        return words\n\n    @staticmethod\n    def _json_or_raise(response: Any) -> dict[str, Any]:\n        try:\n            return response.json()\n        except ValueError as exc:\n            raise RuntimeError(\n                f\"Non-JSON response from DashScope API: \"\n                f\"HTTP {response.status_code}\"\n            ) from exc\n\n    def _raise_for_error(\n        self, http_status: int, payload: dict[str, Any]\n    ) -> None:\n        if http_status < 400:\n            return\n        code = payload.get(\"code\")\n        message = payload.get(\"message\", \"unknown error\")\n        raise RuntimeError(\n            f\"DashScope API error: HTTP {http_status}, \"\n            f\"code {code}: {message}\"\n        )\n\n    @staticmethod\n    def _safe_error(exc: Exception) -> str:","sourceCodeStart":347,"sourceCodeEnd":383,"githubUrl":"https://github.com/calesthio/OpenMontage/blob/95e1c3d0ab93482159818560f6a8c8e866b9139f/tools/analysis/dashscope_asr.py#L347-L383","documentation":"Raised by _json_or_raise when a DashScope HTTP response body cannot be parsed as JSON (requests raises ValueError from response.json()). This usually means the endpoint returned HTML (auth wall, proxy error page) or an empty body instead of the expected JSON envelope. The RuntimeError wraps the original ValueError with the HTTP status for context.","triggerScenarios":"requests.post/get to the DashScope REST endpoint returning HTML from a corporate proxy, a CAPTCHA/anti-bot page, or a 502 gateway error with an HTML body; an empty body from a truncated connection; hitting the wrong base URL (typo, environment override) that serves a generic web page.","commonSituations":"HTTP_PROXY/HTTPS_PROXY env vars routing DashScope traffic through a proxy that injects error pages; DNS or region misconfiguration pointing at a non-API host; intermittent gateway errors on Aliyun edge nodes; the API key missing causing a redirect to a login page.","solutions":["Print/log response.status_code and response.text[:500] at the raise site to see what actually came back","Check proxy env vars (HTTP_PROXY/HTTPS_PROXY/NO_PROXY) and bypass them for dashscope.aliyuncs.com","Verify the base URL configuration and that DASHSCOPE_API_KEY is set and valid","Retry once — transient 502/503 HTML pages from gateways often clear immediately"],"exampleFix":"// before\ndata = self._json_or_raise(resp)\n\n// after — capture diagnostics before the generic raise\ntry:\n    data = self._json_or_raise(resp)\nexcept RuntimeError:\n    logger.error(\"body=%r status=%s\", resp.text[:500], resp.status_code)\n    raise","handlingStrategy":"retry","validationCode":null,"typeGuard":"def is_json_response(resp) -> bool:\n    content_type = resp.headers.get(\"Content-Type\", \"\")\n    return \"application/json\" in content_type","tryCatchPattern":"try:\n    data = self._json_or_raise(resp)\nexcept RuntimeError as e:\n    if \"Non-JSON\" in str(e):\n        logger.error(\"dashscope non-JSON body: %r (status %s)\", resp.text[:500], resp.status_code)\n        # one retry — gateway HTML pages are often transient\n        resp = session.post(url, json=payload, timeout=timeout)\n        data = self._json_or_raise(resp)\n    else:\n        raise","preventionTips":["Check the Content-Type header before parsing when debugging","Bypass corporate proxies for the DashScope API host","Log response.text on parse failures instead of only the status code"],"tags":["dashscope","json","http","proxy","network"],"backgroundTag":null,"analyzedSha":"95e1c3d0ab93482159818560f6a8c8e866b9139f","analyzedAt":"2026-08-15T06:31:20.014Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}