{"record":{"id":"66e55544c4f4097c","repo":"BerriAI/litellm","slug":"error-e-received-response-raw-response-t","errorCode":null,"errorMessage":"error - {e}, Received response - {raw_response}, Type of response - {type(raw_response)}","messagePattern":"error - (.+?), Received response - (.+?), Type of response - (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"litellm/llms/openai/openai.py","lineNumber":484,"sourceCode":"                headers = dict(raw_response.headers)\n            else:\n                headers = {}\n            response: Final = raw_response.parse()\n            if not data.get(\"stream\") and not hasattr(response, \"model_dump\"):\n                raise OpenAIError(\n                    status_code=500,\n                    message=f\"Empty or invalid response from LLM endpoint. Received: {response!r}. Check the reverse proxy or model server configuration.\",\n                )\n            return headers, response\n        except OpenAIError:\n            raise\n        except openai.BadRequestError as e:\n            if not is_output_token_limit_error(e):\n                raise\n            return build_output_token_limit_response(e=e, data=data, is_async=False)\n        except Exception as e:\n            if raw_response is not None:\n                raise Exception(\n                    f\"error - {e}, Received response - {raw_response}, Type of response - {type(raw_response)}\"\n                )\n            else:\n                raise e\n\n    async def _call_agentic_completion_hooks_openai(\n        self,\n        response: object,\n        model: str,\n        messages: list[dict],\n        optional_params: dict,\n        logging_obj: LiteLLMLoggingObj,\n        stream: bool,\n        litellm_params: dict,\n    ) -> object | None:\n        \"\"\"\n        Call agentic completion hooks for all custom loggers (OpenAI Chat Completions API).\n","sourceCodeStart":466,"sourceCodeEnd":502,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/openai/openai.py#L466-L502","documentation":"Generic exception path at the end of the sync make_openai_chat_completion_request (litellm/llms/openai/openai.py:484). If any exception fires after a raw_response was already received — typically raw_response.parse() failing on a malformed body — litellm re-raises a bare Exception (not OpenAIError) whose message embeds the original exception, the repr of the raw response, and its type. Because it is a plain Exception, upstream status codes and headers are lost, and only a broad except Exception catches it.","triggerScenarios":"The OpenAI SDK received a 200 response whose body cannot be parsed into a ChatCompletion: invalid JSON, wrong schema, BOM or whitespace-prefixed payloads; proxies injecting content; version skew where parse() yields a non-pydantic object that fails a later attribute access.","commonSituations":"Homegrown API gateways and legacy serving stacks emitting non-standard fields; SSL-inspecting middleboxes corrupting bodies; pinned old openai SDK versions against newer server payloads.","solutions":["Read the message — the repr of raw_response shows the exact bytes the server sent","Reproduce with curl and validate the body against the OpenAI ChatCompletion schema","Align the openai SDK version with what litellm expects (pip install -U openai litellm)","Fix the server or proxy emitting the malformed body","If the repr shows an HTML page, the api_base path is wrong — fix the route, not the parser"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"import httpx, json\n\ndef upstream_body_is_valid_json(api_base: str, api_key: str, model: str) -> bool:\n    r = httpx.post(\n        f'{api_base.rstrip(\"/\")}/chat/completions',\n        headers={'Authorization': f'Bearer {api_key}'},\n        json={'model': model, 'messages': [{'role': 'user', 'content': 'ping'}]},\n        timeout=30,\n    )\n    try:\n        body = json.loads(r.text)\n    except json.JSONDecodeError:\n        return False\n    return isinstance(body, dict) and 'choices' in body","typeGuard":null,"tryCatchPattern":"from litellm.llms.openai.common_utils import OpenAIError\n\ntry:\n    resp = litellm.completion(model=m, messages=msgs)\nexcept OpenAIError:\n    raise\nexcept Exception as e:  # error 1784 is raised as a bare Exception\n    msg = str(e)\n    if 'Received response' in msg and 'Type of response' in msg:\n        capture_for_debugging(msg)  # message embeds the raw payload repr\n    raise","preventionTips":["Contract-test custom servers against the OpenAI ChatCompletion schema in CI","Pin openai and litellm versions together and upgrade them together","Log full exception messages — the embedded raw response repr is the only diagnostic","Watch for BOM- or whitespace-prefixed JSON from gateways; strip it server-side"],"tags":["response-parsing","openai-compat","exception-wrapping","bare-exception"],"backgroundTag":"response-parse-failed","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}