{"record":{"id":"5bdc055814937d9d","repo":"BerriAI/litellm","slug":"unable-to-get-json-response-e-original-respon","errorCode":null,"errorMessage":"Unable to get json response - {e}, Original Response: {raw_response.text}","messagePattern":"Unable to get json response - (.+?), Original Response: (.+?)","errorType":"http","errorClass":"AnthropicError","httpStatus":null,"severity":"error","filePath":"litellm/llms/anthropic/chat/transformation.py","lineNumber":2490,"sourceCode":"        litellm_params: dict,\n        encoding: Any,\n        api_key: str | None = None,\n        json_mode: bool | None = None,\n    ) -> ModelResponse:\n        ## LOGGING\n        logging_obj.post_call(\n            input=messages,\n            api_key=api_key,\n            original_response=raw_response.text,\n            additional_args={\"complete_input_dict\": request_data},\n        )\n\n        ## RESPONSE OBJECT\n        try:\n            completion_response: Final = raw_response.json()\n        except Exception as e:\n            response_headers: Final = getattr(raw_response, \"headers\", None)\n            raise AnthropicError(\n                message=f\"Unable to get json response - {e}, Original Response: {raw_response.text}\",\n                status_code=raw_response.status_code,\n                headers=response_headers,\n            )\n\n        prefix_prompt: Final = self.get_prefix_prompt(messages=messages)\n        speed: Final = optional_params.get(\"speed\")\n        tool_name_reverse_map: dict[str, str] | None = None\n        if isinstance(litellm_params, dict):\n            _candidate: Final = litellm_params.get(ANTHROPIC_TOOL_NAME_REVERSE_MAP_KEY)\n            if isinstance(_candidate, dict):\n                tool_name_reverse_map = _candidate\n\n        model_response = self.transform_parsed_response(\n            completion_response=completion_response,\n            raw_response=raw_response,\n            model_response=model_response,\n            json_mode=json_mode,","sourceCodeStart":2472,"sourceCodeEnd":2508,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/anthropic/chat/transformation.py#L2472-L2508","documentation":"The HTTP call returned, but raw_response.json() failed — the body is not JSON. LiteLLM includes the original response text in the error so you can see exactly what came back. Typical culprits: an HTML error page from a gateway/proxy (502/504), an empty body from a crashed upstream, or a plain-text error from a misrouted endpoint.","triggerScenarios":"Non-streaming anthropic/ call where the response body is HTML (nginx '502 Bad Gateway', Cloudflare challenge page), empty (connection closed after headers), or plain text — most often because api_base points at a proxy/gateway that failed to reach Anthropic.","commonSituations":"Custom ANTHROPIC_API_BASE to an internal gateway that intermittently 502s; DNS/tls interceptors returning block pages; LiteLLM proxy misrouting to a dead backend; corporate proxies returning text errors.","solutions":["Read 'Original Response:' in the message — it reveals the actual body (usually an HTML 502/504 page naming the failing hop).","Fix or restart the gateway/proxy at that hop; verify the backend URL it forwards to.","If the body is empty, check upstream timeouts and connection resets between proxy and api.anthropic.com.","Add a retry with backoff for transient gateway failures (502/503/504).","Confirm ANTHROPIC_API_BASE is correct and not pointing at an OpenAI-only endpoint."],"exampleFix":"# before\nresp = litellm.completion(model=\"anthropic/claude-sonnet-4-5\", messages=msgs)\n\n# after\nfrom litellm.exceptions import AnthropicError\nimport time\nfor attempt in range(3):\n    try:\n        resp = litellm.completion(\n            model=\"anthropic/claude-sonnet-4-5\", messages=msgs,\n            num_retries=2,\n        )\n        break\n    except AnthropicError as e:\n        if \"Unable to get json response\" in str(e) and attempt < 2:\n            time.sleep(2 ** attempt)\n            continue\n        raise","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"from litellm.exceptions import AnthropicError\nfor attempt in range(3):\n    try:\n        resp = litellm.completion(model=MODEL, messages=messages)\n        break\n    except AnthropicError as e:\n        if \"Unable to get json response\" in str(e):\n            log.error(\"non-JSON body (attempt %d): %.200s\", attempt, e.message)\n            time.sleep(2 ** attempt)\n            continue\n        raise","preventionTips":["Health-check custom gateways before pointing production traffic at them.","Log the 'Original Response:' text — it identifies the failing hop instantly.","Retry only transient gateway failures (502/503/504); fix config for auth/404 pages."],"tags":["anthropic","non-json","proxy","gateway","http"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}