litellm/tests/unit/llms/custom_httpx
linhqm 68821d740c
fix(http): stop advertising zstd so streamed responses survive per-event frames (#53439)
* fix(http_handler): stop advertising zstd so streamed responses survive per-event frames

httpx builds one zstd decompressor per response and reuses it across chunk
boundaries, so an upstream that compresses each streamed event as its own zstd
frame dies on the second event with "cannot use a decompressobj multiple times".
httpx offers zstd whenever the optional zstandard package is importable, which is
any install that also carries langchain, langsmith or httpx[zstd], so litellm now
pins Accept-Encoding to the encodings that survive chunked reads on both the sync
and async handlers, disable_default_headers included.

* fix(http_handler): guard the private httpx accept-encoding import

Reading ACCEPT_ENCODING straight off httpx._client makes importing litellm fail
outright if a future httpx renames it, and the dependency accepts any release
under 1.0. Fall back to gzip and deflate, which httpx can always decode

Move the streaming regression onto an httpx MockTransport that hands back one
zstd frame per event, so tests/test_litellm keeps its mocked-networking rule and
the suite drops a listening socket along with its sleeps

* fix(http): drop zstd from every litellm-built httpx client

httpx reuses one zstd decompressor across a response's chunks, so a stream
framed one zstd frame per event dies on the second frame. The accept-encoding
narrowing now lives in a shared module and reaches the OpenAI SDK, MCP, Azure
and base client factories, not just the two custom_httpx handlers.

LITELLM_ACCEPT_ENCODING overrides the header for deployments that want zstd back.

* fix(openai): route the SDK-built clients through litellm's http client

The text completion, files, batches, assistants, fine-tuning and image
variation handlers passed litellm.client_session straight to the OpenAI
SDK, which is None unless the caller sets it, so the SDK built its own
httpx client with httpx's own Accept-Encoding. POST /v1/completions with
"stream": true therefore still asked upstreams for zstd and still died on
the second event with "cannot use a decompressobj multiple times". Same
for Azure through the Cloudflare AI Gateway, which also handed a sync
client to AsyncAzureOpenAI.

Moderation keeps its own client and only gains the header.

A blank LITELLM_ACCEPT_ENCODING now leaves the default in place rather
than sending an empty header.

* fix: send the Accept-Encoding header through the OpenAI and Azure SDK clients

The previous commit reached the SDK-built clients by handing them the
client litellm builds, which also swapped in litellm's aiohttp transport
and its SSL, proxy-mount and timeout settings. That is a much larger
change than the header needs, and it took the files, batches and
assistants calls off httpx entirely.

Pass `default_headers` on the SDK constructors instead. The header is the
only thing that changes; every client keeps the transport, SSL settings,
proxy mounts and timeouts it had before.

Two `http_client` arguments are corrected along the way: the async image
variations client and the async Cloudflare AI Gateway client were both
handed the sync `litellm.client_session`, so a deployment that set one
would give an async client a sync transport.

The header helpers now return a `Mapping` rather than a fresh dict, which
keeps the new call sites off the mutable-structure budget.

* fix(image variations): give each SDK client the session that matches it

The async image variations path builds its client params and hands them
to `AsyncOpenAI`, and the sync path hands its own to `OpenAI`, but the two
param blocks sit in the opposite order from the calls that consume them.
The previous commit read them in source order and put `aclient_session`
on the sync one, so a deployment that sets either session got a
`TypeError: Invalid http_client argument` on every image variation.

Each block now carries the session its SDK class accepts. The async path
also stops getting the sync session, which it had since before this
branch.

The new test drives both entry points against a loopback upstream with
both sessions set, and fails on either wiring being crossed.

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-11-21 23:58:04 +00:00
..
__init__.py
test_accept_encoding.py fix(http): stop advertising zstd so streamed responses survive per-event frames (#53439) 2026-11-21 23:58:04 +00:00
test_aiohttp_cleanup_closed.py refactor(llms): expose public names for private provider helpers (#45037) 2026-10-07 08:49:25 -07:00
test_aiohttp_handler.py fix(http): diagnose aiohttp connection pool saturation (#49499) 2026-10-31 16:12:30 -07:00
test_aiohttp_so_keepalive.py refactor(llms): expose public names for private provider helpers (#45037) 2026-10-07 08:49:25 -07:00
test_aiohttp_transport.py test(aiohttp): verify streaming timeouts without wall-clock delays (#53189) 2026-11-20 21:20:48 +00:00
test_asgi_handler.py
test_async_client_cleanup.py test: move 76 live top-level tests to offline unit and integration coverage (#45352) 2026-10-08 10:33:35 -07:00
test_container_handler.py test: catch regressions in the validation lines #44798 added (#47420) 2026-10-20 01:06:37 +00:00
test_credential_leak_prevention.py
test_gemini_session_leak.py test(unit): stop test modules from putting their own directory on sys.path (#43421) 2026-09-28 12:20:20 -07:00
test_http2_connection_spread.py feat(vertex_ai)!: use HTTP/2 httpx client for search_api vector store (#40631) 2026-10-29 10:31:18 -07:00
test_http_handler.py fix(httpx): repair surrogate code points in raw text request bodies (#49599) 2026-11-19 11:48:18 -08:00
test_llm_http_handler.py fix(realtime): keep push-to-talk for transcription sessions opened without intent under a transcript guardrail (#50594) 2026-11-19 16:44:21 -08:00
test_mock_transport.py
test_sse_lines.py fix(streaming): keep merged think tags balanced and split SSE lines only on CR/LF (#48755) 2026-10-28 17:00:29 -07:00