{"record":{"id":"352ae092945ec7d7","repo":"BerriAI/litellm","slug":"failed-to-parse-dashscope-response-as-json-e","errorCode":null,"errorMessage":"Failed to parse DashScope response as JSON: {e}","messagePattern":"Failed to parse DashScope response as JSON: (.+?)","errorType":"http","errorClass":"DashScopeError","httpStatus":null,"severity":"error","filePath":"litellm/llms/dashscope/embed/transformation.py","lineNumber":133,"sourceCode":"            if value is not None:\n                data[key] = value\n        return data\n\n    def transform_embedding_response(\n        self,\n        model: str,\n        raw_response: httpx.Response,\n        model_response: EmbeddingResponse,\n        logging_obj: LiteLLMLoggingObj,\n        api_key: str | None,\n        request_data: dict,\n        optional_params: dict,\n        litellm_params: dict,\n    ) -> EmbeddingResponse:\n        try:\n            response_json: Final = raw_response.json()\n        except Exception as e:\n            raise DashScopeError(\n                status_code=raw_response.status_code,\n                message=f\"Failed to parse DashScope response as JSON: {e}\",\n            )\n\n        logging_obj.post_call(\n            input=request_data.get(\"input\"),\n            api_key=api_key,\n            additional_args={\"complete_input_dict\": request_data},\n            original_response=response_json,\n        )\n\n        if \"error\" in response_json:\n            error: Final = response_json[\"error\"]\n            message: Final = error.get(\"message\", str(error)) if isinstance(error, dict) else str(error)\n            raise DashScopeError(\n                status_code=raw_response.status_code,\n                message=message,\n            )","sourceCodeStart":115,"sourceCodeEnd":151,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/dashscope/embed/transformation.py#L115-L151","documentation":"After the DashScope embedding HTTP call succeeds at the transport level, litellm parses the body with response.json(); if the body is not valid JSON (e.g. an HTML error page from a gateway), it raises DashScopeError with status_code=raw_response.status_code and a message embedding the underlying JSON parse exception. It indicates the wrong shape of response, not necessarily an HTTP failure.","triggerScenarios":"DashScope embeddings endpoint (or a custom api_base in front of it) returning non-JSON: HTML 502/504 pages from proxies, XML/WAF block pages, empty bodies, or text/plain gateway errors — commonly when api_base is misconfigured or an intermediary (corporate proxy, Cloudflare) intercepts the request.","commonSituations":"Custom api_base pointing at a proxy that returns HTML on failure; DashScope regional outage pages; rate-limiter/WAF returning non-JSON; response body truncated by a middlebox so JSON is malformed.","solutions":["Log raw_response.text (first ~500 chars) to identify what the intermediary returned — the HTML title usually names the proxy/firewall","Verify api_base is the real DashScope endpoint unless you intentionally use a gateway","Whitelist/bypass the proxy for the DashScope domain, or fix the gateway to pass through JSON errors","Retry with backoff for transient 502/504 gateway pages"],"exampleFix":"# before\nresp = litellm.embedding(model=\"dashscope/text-embedding-v3\", input=[\"hi\"])\n# DashScopeError: Failed to parse DashScope response as JSON: Expecting value...\n\n# after (debug what the body actually is)\nimport httpx, os\nr = httpx.post(\"https://dashscope-intl.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding\",\n              headers={\"Authorization\": f\"Bearer {os.environ['DASHSCOPE_API_KEY']}\"},\n              json={\"model\": \"text-embedding-v3\", \"input\": {\"texts\": [\"hi\"]}})\nprint(r.status_code, r.headers.get(\"content-type\"), r.text[:300])","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    vecs = litellm.embedding(model=\"dashscope/text-embedding-v3\", input=texts)\nexcept Exception as e:\n    if \"Failed to parse DashScope response as JSON\" in str(e):\n        logging.error(\"non-JSON response from DashScope — check api_base/proxy: %s\", str(e)[:300])\n        raise\n    raise","preventionTips":["Only set api_base for dashscope when you control the gateway and it passes JSON through","Reproduce with a raw httpx call and inspect content-type/text when this fires — the body names the offending intermediary","Treat gateway HTML pages (502/504) as transient and retry with backoff; treat WAF blocks as config to fix"],"tags":["dashscope","json","parsing","proxy","embeddings","litellm"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}