Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: removed content-encoding header to avoid decoding errors #29

Merged
merged 2 commits into from
Nov 19, 2024

Conversation

adubovik
Copy link
Collaborator

@adubovik adubovik commented Nov 18, 2024

httpx library used by openai is sending Accept-Encoding: gzip, deflate by default:

https://github.com/encode/httpx/blob/7b19cd5f4b749064c6452892a5e58a3758da1d1b/httpx/_client.py#L305-L315

This results in responses from the upstream which are encoded via gzip and having Content-Encoding: gzip header.

In the adapter, all the responses from upstream are decoded, modified and sent to the user as plain text, without any encoding.

Therefore, we must either do the encoding when needed or return the response without Content-Encoding: gzip header pretending that the server doesn't support encoding.

Given that the latter is so much easier to implement and that the adapter only proxies the response headers for errors, it was chosen as a solution.

How to reproduce the bug

Send an erroneous request to the dial-adapter deployment:

    response = client.chat.completions.create(
        model=MODEL,
        messages=[{"content": "2+3=?", "role": "users"}],
        stream=True,
    )
    for chunk in response:
        print(chunk)

Observe the error message:

  File ".venv/lib/python3.11/site-packages/httpx/_models.py", line 805, in read
    self._content = b"".join(self.iter_bytes())
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/httpx/_models.py", line 824, in iter_bytes
    decoded = decoder.decode(raw_bytes)
              ^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".venv/lib/python3.11/site-packages/httpx/_decoders.py", line 78, in decode
    raise DecodingError(str(exc)) from exc
httpx.DecodingError: Error -3 while decompressing data: incorrect header check

resulted from an attempt to decode plain-text content using gzip algorithm since Content-Encoding header is set to gzip in the response.

@adubovik adubovik self-assigned this Nov 18, 2024
@adubovik adubovik merged commit 8a50570 into development Nov 19, 2024
6 checks passed
@adubovik adubovik deleted the fix/fixed-incorrent-handling-of-gzip-responses branch November 19, 2024 10:20
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.

2 participants