fix(models): odpowiedz modelu bez kompresji

Bajty ida do przegladarki surowo, bez naszego Content-Encoding; api.anthropic.com
gzipuje strumien domyslnie i czat dostawal smieci. Accept-Encoding: identity.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016uwxqJbh3fPYdfdLG3MU6f
This commit is contained in:
tobayah
2026-11-06 21:01:33 +01:00
co-authored by Claude Opus 5.5
parent ab8de6dc26
commit e330fd2cf3
2 changed files with 22 additions and 1 deletions
+4 -1
View File
@@ -93,8 +93,11 @@ async def stream_chat(root: str, key: str | None, model: str, messages: list[dic
body = {"model": model, "messages": messages, "max_tokens": max_tokens, "stream": True, **extra}
async with httpx.AsyncClient(timeout=LOAD_TIMEOUT) as client:
try:
# Identity: the raw bytes pass through to the browser with no Content-Encoding of ours, and a
# cloud API gzips by default (api.anthropic.com did, 2026-11-06) — the chat read garbage.
async with client.stream("POST", f"{root}/v1/chat/completions",
headers=_headers(key), json=body) as resp:
headers={**_headers(key), "Accept-Encoding": "identity"},
json=body) as resp:
if resp.status_code != 200:
# The server's own reason (OpenAI-shaped `error.message`), capped: a bare "HTTP 400"
# left a refused parameter invisible. The key travels in a header, never echoed here.
+18
View File
@@ -176,3 +176,21 @@ def test_a_refused_call_says_why(monkeypatch):
pass
with pytest.raises(model_server.GatewayError, match="temperature"):
asyncio.run(run())
def test_the_answer_is_asked_for_uncompressed(monkeypatch):
# The bytes are passed through raw with no Content-Encoding: a gzipped stream reached the chat
# as garbage (api.anthropic.com, 2026-11-06).
import httpx
seen = []
def handler(req):
seen.append(req.headers.get("accept-encoding"))
return httpx.Response(200, stream=httpx.ByteStream(b"data: [DONE]\n\n"))
real = httpx.AsyncClient
monkeypatch.setattr(model_server.httpx, "AsyncClient",
lambda **kw: real(transport=httpx.MockTransport(handler), **kw))
async def run():
return [c async for c in model_server.stream_chat("http://h", "k", "m", [], 64)]
assert asyncio.run(run()) == [b"data: [DONE]\n\n"] and seen == ["identity"]