{"record":{"id":"173486147a51adc3","repo":"openai/openai-python","slug":"expected-content-type-response-header-to-be-appli","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/_legacy_response.py","lineNumber":326,"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_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":308,"sourceCodeEnd":344,"githubUrl":"https://github.com/openai/openai-python/blob/9917c6e28e66e90e1227b3d223c06a8c5441515a/src/openai/_legacy_response.py#L308-L344","documentation":"When strict_response_validation is enabled on the client, the SDK asserts that JSON-expecting responses carry an application/json Content-Type header. Any other content type (e.g. text/plain, text/html from a proxy or error page) raises APIResponseValidationError instead of leniently attempting to parse the body.","triggerScenarios":"Setting client = OpenAI(strict_response_validation=True) and hitting a server/proxy that returns text/html, text/plain, or an unexpected MIME type for a JSON endpoint; API gateways returning HTML error pages or cloud-provider endpoints (Bedrock/Azure front-ends) that alter headers.","commonSituations":"Enabling strict validation in production behind a corporate proxy or WAF that rewrites responses; pointing the base_url at a gateway whose error responses are HTML; version changes in a provider's header behavior.","solutions":["Inspect response headers from the failing endpoint and fix the server/proxy to return application/json for JSON bodies","If the endpoint is legitimately non-JSON, cast to str or httpx.Response instead of a model/dict","As a last resort, construct the client with strict_response_validation=False to restore lenient parsing"],"exampleFix":"# before\nclient = OpenAI(strict_response_validation=True)\n# after\nclient = OpenAI()  # lenient content-type handling\n","handlingStrategy":"fallback","validationCode":"ct = response.headers.get('content-type', '').split(';')[0]\nif ct != 'application/json':\n    # handle non-JSON response explicitly (log, alert, or parse as text)\n    body = response.text","typeGuard":null,"tryCatchPattern":"from openai import APIResponseValidationError\ntry:\n    parsed = response.parse()\nexcept APIResponseValidationError as e:\n    if 'Content-Type' in str(e):\n        body = e.body  # inspect raw text; likely a proxy/HTML error page\n        raise","preventionTips":["Verify proxies/gateways preserve application/json on JSON endpoints","Only enable strict_response_validation once header behavior is confirmed"],"tags":["content-type","strict-validation","http","response"],"backgroundTag":"unexpected-content-type","analyzedSha":"9917c6e28e66e90e1227b3d223c06a8c5441515a","analyzedAt":"2026-08-28T11:46:34.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}