{"record":{"id":"a2633e140bbc1efd","repo":"BerriAI/litellm","slug":"body-masked-response-body","errorCode":null,"errorMessage":"_body (masked response body)","messagePattern":"_body \\(masked response body\\)","errorType":"http","errorClass":"MaskedHTTPStatusError","httpStatus":null,"severity":"error","filePath":"litellm/llms/custom_httpx/http_handler.py","lineNumber":460,"sourceCode":"            except Exception:\n                response.close()\n                return b\"\"\n        return response.read()\n    except Exception:\n        return b\"\"\n\n\ndef _raise_masked_sync_error(e: httpx.HTTPStatusError, stream: bool) -> None:\n    \"\"\"Raise a MaskedHTTPStatusError for sync HTTP handlers.\"\"\"\n    if stream:\n        try:\n            _body: Final = mask_sensitive_info(\n                _safe_read_response(\n                    e.response,\n                    timeout=_STREAMING_ERROR_BODY_READ_TIMEOUT_SECONDS,\n                )\n            )\n            raise MaskedHTTPStatusError(e, message=_body, text=_body) from None\n        finally:\n            try:\n                e.response.close()\n            except Exception:\n                pass\n    _text: Final = mask_sensitive_info(_safe_get_response_text(e.response))\n    raise MaskedHTTPStatusError(e, message=_text, text=_text) from None\n\n\nasync def _raise_masked_async_error(e: httpx.HTTPStatusError, stream: bool) -> None:\n    \"\"\"Raise a MaskedHTTPStatusError for async HTTP handlers.\"\"\"\n    if stream:\n        try:\n            _body: Final = mask_sensitive_info(\n                await _safe_aread_response(\n                    e.response,\n                    timeout=_STREAMING_ERROR_BODY_READ_TIMEOUT_SECONDS,\n                )","sourceCodeStart":442,"sourceCodeEnd":478,"githubUrl":"https://github.com/BerriAI/litellm/blob/6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d/litellm/llms/custom_httpx/http_handler.py#L442-L478","documentation":"When a sync streaming HTTP call in litellm's HTTPHandler gets a non-2xx response, litellm reads the body (with a bounded timeout), masks sensitive info such as keys in the URL/body via mask_sensitive_info, and raises MaskedHTTPStatusError. The message is the masked response body of the failed upstream request.","triggerScenarios":"Sync streaming completions where the upstream returns 4xx/5xx — invalid API key (401), model not found, context length exceeded, rate limits (429), or upstream 5xx — during httpx streaming.","commonSituations":"Wrong or expired API keys; hitting provider rate limits mid-stream; requesting a model name the provider does not serve; proxy servers returning HTML/JSON error pages. Masking means the exception text is safe to log.","solutions":["Catch httpx.HTTPStatusError (MaskedHTTPStatusError subclasses it) and inspect .response.status_code","Fix the root cause per status: 401/403 -> API key; 404 -> model name/api_base; 429 -> backoff or higher limits; 5xx -> provider health","If behind a proxy, verify it forwards auth headers and returns the provider's real status"],"exampleFix":"# before\nresp = client.post(url, json=payload)  # raises on non-2xx\n\n# after\nimport httpx\ntry:\n    resp = client.post(url, json=payload)\nexcept httpx.HTTPStatusError as e:  # includes MaskedHTTPStatusError\n    if e.response.status_code == 429:\n        await asyncio.sleep(backoff)\n    else:\n        raise","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"import httpx\nfrom litellm.llms.custom_httpx.http_handler import MaskedHTTPStatusError\ntry:\n    stream = client.stream(\"POST\", url, ...)\nexcept MaskedHTTPStatusError as e:\n    if e.response.status_code == 429:\n        retry_with_backoff()\n    raise","preventionTips":["Pre-check credentials and model names before opening streams","Treat 429/5xx as retryable and 4xx (except 429) as configuration bugs"],"tags":["http","streaming","masking","litellm"],"backgroundTag":null,"analyzedSha":"6c2dcb801bf2b75c18f1bb24140e7cf57465cc4d","analyzedAt":"2026-08-15T07:12:03.035Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}