{"record":{"id":"4d29f569b1072beb","repo":"openai/openai-python","slug":"expected-content-type-response-header-to-be-appli-4d29f5","errorCode":null,"errorMessage":"Expected Content-Type response header to be `application/json` but received `{content_type}` instead.","messagePattern":"Expected Content-Type response header to be `application/json` but received `(.+?)` instead\\.","errorType":"validation","errorClass":"APIResponseValidationError","httpStatus":null,"severity":"error","filePath":"src/openai/_response.py","lineNumber":259,"sourceCode":"\n        # split is required to handle cases where additional information is included\n        # in the response, e.g. application/json; charset=utf-8\n        content_type, *_ = response.headers.get(\"content-type\", \"*\").split(\";\")\n        if not content_type.endswith(\"json\"):\n            if is_basemodel(cast(type, cast_to)):\n                try:\n                    data = response.json()\n                except Exception as exc:\n                    log.debug(\"Could not read JSON from response data due to %s\", type(exc).__name__)\n                else:\n                    return self._client._process_response_data(\n                        data=data,\n                        cast_to=cast_to,  # type: ignore\n                        response=response,\n                    )\n\n            if self._client._strict_response_validation:\n                raise APIResponseValidationError(\n                    response=response,\n                    message=f\"Expected Content-Type response header to be `application/json` but received `{content_type}` instead.\",\n                    body=response.text,\n                )\n\n            # If the API responds with content that isn't JSON then we just return\n            # the (decoded) text without performing any parsing so that you can still\n            # handle the response however you need to.\n            return response.text  # type: ignore\n\n        data = response.json()\n\n        return self._client._process_response_data(\n            data=data,\n            cast_to=cast_to,  # type: ignore\n            response=response,\n        )\n","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/openai/openai-python/blob/9917c6e28e66e90e1227b3d223c06a8c5441515a/src/openai/_response.py#L241-L277","documentation":"When the client is configured with strict_response_validation=True, any response whose Content-Type header is not exactly application/json triggers APIResponseValidationError. Normally non-JSON content types fall back to returning the raw text, but strict mode turns this into an error.","triggerScenarios":"Setting `openai.OpenAI(strict_response_validation=True)` and receiving a response with a Content-Type such as text/plain, text/html, or application/octet-stream from the API endpoint (often an HTML error page from a proxy or gateway).","commonSituations":"A corporate proxy, gateway, or captcha page returning HTML instead of JSON; pointing base_url at a wrong/self-hosted endpoint that returns non-JSON 200 responses; enabling strict validation in CI to catch contract drift.","solutions":["Inspect the response body (included in the exception) to see what the server actually returned and fix the endpoint/proxy returning HTML","Verify base_url points to the real OpenAI API or a compatible JSON API","Disable strict_response_validation if you want lenient text fallback for non-JSON content"],"exampleFix":"// before\nclient = openai.OpenAI(strict_response_validation=True)\n\n// after\nclient = openai.OpenAI()  # non-JSON falls back to raw text","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"from openai import APIResponseValidationError\ntry:\n    result = client.chat.completions.create(...)\nexcept APIResponseValidationError as e:\n    body = e.body  # inspect what the server actually returned (often HTML)\n    raise","preventionTips":["Only enable strict_response_validation when you control the endpoint","Verify base_url and proxy configuration return application/json responses","Log response bodies on unexpected content types before enabling strict mode"],"tags":["content-type","validation","strict-mode","http"],"backgroundTag":"unexpected-content-type","analyzedSha":"9917c6e28e66e90e1227b3d223c06a8c5441515a","analyzedAt":"2026-08-28T11:46:34.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}