From adb10dc938bc372e34f4f6fb2f3cb85f5908f0f6 Mon Sep 17 00:00:00 2001 From: Sameer Kankute Date: Thu, 28 May 2026 12:20:43 +0530 Subject: [PATCH] Fix overiding of fastapi_response headers --- .../pass_through_endpoints.py | 8 ++++- .../test_pass_through_unit_tests.py | 34 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py index 36a389d233d..a9fb02a1c95 100644 --- a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py +++ b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py @@ -344,7 +344,13 @@ class HttpPassThroughEndpointHelpers(BasePassthroughUtils): if litellm_call_id: return_headers["x-litellm-call-id"] = litellm_call_id if custom_headers: - return_headers.update(custom_headers) + # Ensure custom headers don't override actual upstream response headers or let framework defaults (like content-length: 0) interfere. + sanitized_custom_headers = { + key: value + for key, value in custom_headers.items() + if key.lower() not in excluded_headers + } + return_headers.update(sanitized_custom_headers) return return_headers diff --git a/tests/pass_through_unit_tests/test_pass_through_unit_tests.py b/tests/pass_through_unit_tests/test_pass_through_unit_tests.py index 1b16177b755..65448c6281e 100644 --- a/tests/pass_through_unit_tests/test_pass_through_unit_tests.py +++ b/tests/pass_through_unit_tests/test_pass_through_unit_tests.py @@ -114,6 +114,40 @@ def test_update_metadata_with_tags_in_header_with_tags(mock_request): assert result == {"existing": "value", "tags": ["tag1", "tag2", "tag3"]} +def test_get_response_headers_filters_excluded_custom_headers(): + """ + Regression test: + Ensure excluded headers from FastAPI defaults (e.g. content-length: 0) + do not override passthrough response headers. + """ + upstream_headers = httpx.Headers( + { + "content-type": "application/json", + "x-amzn-requestid": "req-123", + "content-length": "999", # should be excluded + } + ) + + custom_headers = { + "x-litellm-version": "1.84.0", + "content-length": "0", # should be excluded + "server": "uvicorn", # should be excluded + } + + result = HttpPassThroughEndpointHelpers.get_response_headers( + headers=upstream_headers, + litellm_call_id="call-123", + custom_headers=custom_headers, + ) + + assert result["content-type"] == "application/json" + assert result["x-amzn-requestid"] == "req-123" + assert result["x-litellm-version"] == "1.84.0" + assert result["x-litellm-call-id"] == "call-123" + assert "content-length" not in result + assert "server" not in result + + def test_init_kwargs_for_pass_through_endpoint_basic( mock_request, mock_user_api_key_dict ):