Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: accept zstd-compressed requests from the TypeSafe SDK (#16929)
Nilay52524597b344
diff --git a/pyproject.toml b/pyproject.tomlindex e5a84d89ba2..2c1b0beaa8e 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -161,6 +161,7 @@ studio = [# Windows on ARM: 46.0.7 publishes no win_arm64 wheel and the sdist needs Rust."cryptography>=42.0.0,<=46.0.3; sys_platform == 'win32' and platform_machine == 'ARM64'","httpx>=0.27.0",+ "backports.zstd>=1.0.0 ; python_version >= '3.10' and python_version < '3.14'","fastmcp>=4.1.5,<5","gguf==0.19.0 ; python_version >= '3.10'","gguf==0.18.0 ; python_version < '3.10'",diff --git a/studio/backend/main.py b/studio/backend/main.pyindex 0c412adf616..0df1c55cd96 100644--- a/studio/backend/main.py+++ b/studio/backend/main.py@@ -1721,6 +1721,35 @@ async def _send_413(send, total_bytes: int, max_bytes: int) -> None:await send({"type": "http.response.body", "body": payload, "more_body": False})+_ZSTD_BODY_PATH = "/v1/systemone"+_ZSTD_MIN_DECODED_BYTES = 8 * 1024 * 1024+_ZSTD_MAX_RATIO = 64+_ZSTD_MAX_FRAMES = 1024+++def _zstd_decode(zstd, data: bytes, limit: int) -> Optional[bytes]:+ view = memoryview(data)+ frames = []+ total = 0+ pos = 0+ while True:+ if len(frames) == _ZSTD_MAX_FRAMES:+ raise zstd.ZstdError("too many zstd frames")+ # Slice by get_frame_size, not unused_data: that copies the rest per frame (quadratic).+ end = pos + zstd.get_frame_size(view[pos:])+ decompressor = zstd.ZstdDecompressor()+ frame = decompressor.decompress(view[pos:end], max_length = limit + 1 - total)+ total += len(frame)+ if total > limit:+ return None+ if not decompressor.eof:+ raise zstd.ZstdError("truncated zstd frame")+ frames.append(frame)+ pos = end+ if pos == len(data):+ return b"".join(frames)++class MaxBodyMiddleware:"""Reject oversized bodies on protected POST/PUT/PATCH; raw ASGI so chunked uploads cannot bypass the cap."""@@ -1845,6 +1874,49 @@ class MaxBodyMiddleware:if not msg.get("more_body", False):break+ body = b"".join(chunks)+ headers = scope.get("headers", [])+ if (+ path.rstrip("/") == _ZSTD_BODY_PATH+ and dict(headers).get(b"content-encoding", b"").strip().lower() == b"zstd"+ ):+ try:+ from compression import zstd # novermin+ except ImportError:+ try:+ from backports import zstd+ except ImportError:+ await JSONResponse(+ {+ "detail": "zstd request bodies need Python 3.14 or the backports.zstd package."+ },+ status_code = 415,+ )(scope, receive, send)+ return+ limit = min(max_bytes, max(_ZSTD_MIN_DECODED_BYTES, len(body) * _ZSTD_MAX_RATIO))+ try:+ body = await asyncio.to_thread(_zstd_decode, zstd, body, limit)+ except zstd.ZstdError:+ await JSONResponse({"detail": "Request body is not valid zstd."}, status_code = 400)(+ scope, receive, send+ )+ return+ if body is None:+ await JSONResponse(+ {"detail": f"Decoded zstd request body is larger than {limit:,} bytes."},+ status_code = 413,+ )(scope, receive, send)+ return+ scope = dict(+ scope,+ headers = [+ (name, value)+ for name, value in headers+ if name not in (b"content-encoding", b"content-length")+ ]+ + [(b"content-length", str(len(body)).encode("ascii"))],+ )+replayed = {"sent": False}async def replay_receive():@@ -1852,7 +1924,7 @@ class MaxBodyMiddleware:replayed["sent"] = Truereturn {"type": "http.request",- "body": b"".join(chunks),+ "body": body,"more_body": False,}# After replay, fall through so http.disconnect still propagates.diff --git a/studio/backend/requirements/studio.txt b/studio/backend/requirements/studio.txtindex 5ec4073bd7b..198fab7d1c6 100644--- a/studio/backend/requirements/studio.txt+++ b/studio/backend/requirements/studio.txt@@ -48,6 +48,8 @@ cryptography>=42.0.0; sys_platform != "win32" or platform_machine != "ARM64"# constraints.txt, which a plain pip install of the extra never reaches.cryptography>=42.0.0,<=46.0.3; sys_platform == "win32" and platform_machine == "ARM64"httpx>=0.27.0+# zstd request bodies (TypeSafe SDK clients compress JSON of 1 KB or more); stdlib on 3.14.+backports.zstd>=1.0.0; python_version >= "3.10" and python_version < "3.14"fastmcp>=4.1.5,<5# Local image generation (Images page): diffusers' GGUFQuantizationConfig needs# the gguf reader. diffusers itself is torch-bound and comes via the ML stack;diff --git a/studio/backend/tests/test_middleware.py b/studio/backend/tests/test_middleware.pyindex 5154014e8df..ed5b07b7ffe 100644--- a/studio/backend/tests/test_middleware.py+++ b/studio/backend/tests/test_middleware.py@@ -9,6 +9,7 @@ import jsonimport osimport reimport sys+import typesfrom pathlib import Pathimport pytest@@ -29,6 +30,15 @@ def main_module():return _main[email protected](scope = "module")+def zstd():+ if sys.version_info >= (3, 14):+ from compression import zstd # novermin+ else:+ from backports import zstd+ return zstd++def _make_protected_app(max_bytes: int,main_module,@@ -44,6 +54,8 @@ def _make_protected_app(max_bytes_getter = lambda: max_bytes,protected_prefixes = ("/v1/chat/completions",+ "/v1/systemone",+ "/api/auth","/api/inference","/api/settings","/api/train",@@ -59,6 +71,14 @@ def _make_protected_app(async def chat(payload: dict):return {"ok": True, "n": len(payload.get("text", ""))}+ @app.post("/v1/systemone")+ async def systemone(payload: dict):+ return {"ok": True, "n": len(payload.get("text", ""))}++ @app.post("/api/auth/login")+ async def login(request: Request):+ return {"ok": True, "total": len(await request.body())}+@app.post("/api/other")async def other(payload: dict):return {"ok": True, "unprotected": True}@@ -103,6 +123,82 @@ class TestMaxBodyMiddleware:assert r.status_code == 413assert "too large" in r.json()["detail"].lower()+ def test_zstd_body_is_decoded_on_systemone(self, main_module, zstd):+ c = TestClient(_make_protected_app(4096, main_module))+ headers = {"Content-Type": "application/json", "Content-Encoding": "zstd"}+ payload = json.dumps({"text": "x" * 3000}).encode()++ decoded = c.post("/v1/systemone", content = zstd.compress(payload), headers = headers)+ frames = c.post(+ "/v1/systemone",+ content = zstd.compress(payload[:1500]) + zstd.compress(payload[1500:]),+ headers = headers,+ )+ oversized = c.post(+ "/v1/systemone",+ content = zstd.compress(json.dumps({"text": "x" * 100_000}).encode()),+ headers = headers,+ )+ garbage = c.post("/v1/systemone", content = b"not zstd", headers = headers)+ truncated = c.post("/v1/systemone", content = zstd.compress(payload)[:-4], headers = headers)++ assert decoded.status_code == 200+ assert decoded.json()["n"] == 3000+ assert frames.status_code == 200+ assert frames.json()["n"] == 3000+ assert oversized.status_code == 413+ assert garbage.status_code == 400+ assert "zstd" in garbage.json()["detail"]+ assert truncated.status_code == 400++ def test_zstd_bomb_is_not_decoded_off_systemone(self, main_module, zstd):+ c = TestClient(_make_protected_app(1 << 30, main_module))+ bomb = zstd.compress(b"\0" * (64 * 1024 * 1024))+ r = c.post(+ "/api/auth/login",+ content = bomb,+ headers = {"Content-Type": "application/json", "Content-Encoding": "zstd"},+ )+ assert r.status_code == 200+ assert r.json()["total"] == len(bomb)++ def test_zstd_bomb_on_systemone_hits_decoded_cap(self, main_module, zstd):+ c = TestClient(_make_protected_app(1 << 30, main_module))+ bomb = zstd.compress(json.dumps({"text": "x" * (9 * 1024 * 1024)}).encode())+ r = c.post(+ "/v1/systemone",+ content = bomb,+ headers = {"Content-Type": "application/json", "Content-Encoding": "zstd"},+ )+ assert len(bomb) < 64 * 1024+ assert r.status_code == 413+ assert "zstd" in r.json()["detail"]++ def test_zstd_frame_flood_reads_each_byte_once(self, main_module, zstd):+ fed = []++ class CountingDecompressor:+ def __init__(self):+ self._d = zstd.ZstdDecompressor()++ def decompress(self, data, *args, **kwargs):+ fed.append(len(data))+ return self._d.decompress(data, *args, **kwargs)++ def __getattr__(self, name):+ return getattr(self._d, name)++ counting = types.SimpleNamespace(+ ZstdDecompressor = CountingDecompressor,+ ZstdError = zstd.ZstdError,+ get_frame_size = zstd.get_frame_size,+ )+ flood = zstd.compress(b"") * 1023 + zstd.compress(b"{}")+ assert main_module._zstd_decode(counting, flood, 1 << 20) == b"{}"+ assert sum(fed) == len(flood)+ with pytest.raises(zstd.ZstdError):+ main_module._zstd_decode(zstd, zstd.compress(b"") + flood, 1 << 20)+def test_unprotected_prefix_passes_large_body(self, main_module):app = _make_protected_app(1024, main_module)c = TestClient(app)