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:
co-authored by
Claude Opus 5.5
parent
ab8de6dc26
commit
e330fd2cf3
@@ -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.
|
||||
|
||||
@@ -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"]
|
||||
|
||||
Reference in New Issue
Block a user