Fix overiding of fastapi_response headers

This commit is contained in:
Sameer Kankute 2026-05-28 12:20:43 +05:30
parent 5699a06413
commit adb10dc938
No known key found for this signature in database
2 changed files with 41 additions and 1 deletions

View file

@ -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

View file

@ -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
):