Skip to content

Fix ZlibError by removing compression headers after httpx decompression#3

Merged
chopratejas merged 2 commits intochopratejas:mainfrom
pythoninthegrasses:main
Jan 15, 2026
Merged

Fix ZlibError by removing compression headers after httpx decompression#3
chopratejas merged 2 commits intochopratejas:mainfrom
pythoninthegrasses:main

Conversation

@pythoninthegrass
Copy link
Copy Markdown
Contributor

Description

httpx automatically decompresses responses when reading response.content, but the proxy was forwarding the original Content-Encoding headers. This caused clients to attempt double-decompression, resulting in ZlibError.

Fixes #2

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Performance improvement
  • Code refactoring (no functional changes)

Changes Made

Fixes compression header handling in:

  • Non-streaming Anthropic/OpenAI responses
  • Cached responses
  • Passthrough handler

Testing

Describe the tests you ran to verify your changes:

  • Unit tests pass (pytest)
  • Linting passes (ruff check .)
  • Type checking passes (mypy headroom)
  • New tests added for new functionality
  • Manual testing performed

Test Output

λ uv run pytest tests/test_proxy_compression_headers.py -v
==================================================================== test session starts =====================================================================
platform darwin -- Python 3.13.5, pytest-9.0.2, pluggy-1.6.0 -- /Users/lance/git/headroom/.venv/bin/python3
cachedir: .pytest_cache
rootdir: /Users/lance/git/headroom
configfile: pyproject.toml
plugins: anyio-4.12.1, asyncio-1.3.0, cov-7.0.0
asyncio: mode=Mode.AUTO, debug=False, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function
collected 6 items

tests/test_proxy_compression_headers.py::TestCompressionHeaderRemoval::test_compression_headers_are_removed_from_dict PASSED                           [ 16%]
tests/test_proxy_compression_headers.py::TestCompressionHeaderRemoval::test_response_body_is_decompressed_not_compressed PASSED                        [ 33%]
tests/test_proxy_compression_headers.py::TestCompressionHeaderRemoval::test_headers_with_wrong_content_length_cause_issues PASSED                      [ 50%]
tests/test_proxy_compression_headers.py::TestCompressionHeaderRemoval::test_removing_headers_fixes_length_mismatch PASSED                              [ 66%]
tests/test_proxy_compression_headers.py::TestNoRegressionForUncompressedResponses::test_pop_on_missing_keys_is_safe PASSED                             [ 83%]
tests/test_proxy_compression_headers.py::TestNoRegressionForUncompressedResponses::test_dict_conversion_preserves_headers PASSED                       [100%]

===================================================================== 6 passed in 1.54s ======================================================================

Checklist

To qualify the below, inline comments and existing errors weren't relevant to the fix.

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the CHANGELOG.md if applicable

Screenshots (if applicable)

N/A

Additional Notes

You should consider release-please by Google re: changelogs.

pythoninthegrass and others added 2 commits January 14, 2026 19:36
httpx automatically decompresses responses when reading response.content,
but the proxy was forwarding the original Content-Encoding headers. This
caused clients to attempt double-decompression, resulting in ZlibError.

Fixes compression header handling in:
- Non-streaming Anthropic/OpenAI responses
- Cached responses
- Passthrough handler

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Test cases verify:
- Content-Encoding and Content-Length headers are correctly removed
- Response bodies are already decompressed by httpx
- Keeping compression headers causes length mismatch issues
- The fix doesn't break uncompressed responses

These tests will catch regressions of the ZlibError bug.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@chopratejas chopratejas merged commit fbb5375 into chopratejas:main Jan 15, 2026
3 of 6 checks passed
chopratejas added a commit that referenced this pull request Jan 21, 2026
Fix ZlibError by removing compression headers after httpx decompression
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Decompression error: ZlibError

2 participants