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: stop inflating chat images by re-encoding every one to PNG (#11889)
oobabooga996b6a13d917
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex c58a5e43f27..60e424c15de 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -3661,6 +3661,7 @@ from utils.utils import is_hf_authentication_error, safe_error_detail, log_and_himport ioimport base64+import zlibfrom utils.current_date_prompt_settings import (contains_current_date_prompt_line,@@ -33341,11 +33342,7 @@ def _pil_to_png_b64(img) -> str:def _image_bytes_to_png_b64(raw: bytes) -> str:- """Decode raw image bytes and re-encode to a base64-ascii PNG string.-- llama-server's stb_image only handles a few formats (JPEG/PNG/BMP/...); re-- encoding to PNG keeps JPEG/WebP/... inputs loadable. Raises on undecodable- input; callers wrap the call in ``try`` -> HTTPException(400)."""+ """Convert image bytes to base64 PNG for formats llama-server cannot decode."""from PIL import Imageimg = _scaled_from_16_bit(Image.open(io.BytesIO(raw))).convert("RGB")@@ -33354,6 +33351,90 @@ def _image_bytes_to_png_b64(raw: bytes) -> str:return base64.b64encode(buf.getvalue()).decode("ascii")+_PNG_SIGNATURE = b"\x89PNG\r\n\x1a\n"+++def _stb_reads_jpeg(raw: bytes) -> bool:+ """Accept 8-bit SOF0-SOF2 JPEG frames with one or three components."""+ i = 2+ while i + 4 <= len(raw):+ if raw[i] != 0xFF:+ return False+ marker = raw[i + 1]+ if marker == 0xFF: # Fill byte before a marker.+ i += 1+ continue+ if 0xC0 <= marker <= 0xCF and marker not in (0xC4, 0xC8, 0xCC):+ if marker not in (0xC0, 0xC1, 0xC2) or i + 10 > len(raw):+ return False+ height = int.from_bytes(raw[i + 5 : i + 7], "big")+ width = int.from_bytes(raw[i + 7 : i + 9], "big")+ return raw[i + 4] == 8 and height > 0 and width > 0 and raw[i + 9] in (1, 3)+ if marker in (0xD9, 0xDA): # EOI or scan data before any frame header.+ return False+ i += 2 + int.from_bytes(raw[i + 2 : i + 4], "big")+ return False+++def _stb_reads_png(raw: bytes) -> bool:+ """Accept PNGs whose chunks end at IEND and whose IDAT zlib stream is complete.++ Pillow decodes a PNG whose deflate stream or IEND is cut short once the rows are+ complete; stb_image rejects those, and unknown critical chunks. Pillow also stops+ at the last row, so inflation is capped near the IHDR size: a compressed tail past+ it would otherwise cost unbounded CPU here and memory in stb_image.+ """+ i = len(_PNG_SIGNATURE)+ inflater = zlib.decompressobj()+ budget = 0+ try:+ while i + 12 <= len(raw):+ kind = raw[i + 4 : i + 8]+ end = i + 12 + int.from_bytes(raw[i : i + 4], "big")+ if end > len(raw) or (kind == b"IHDR") != (i == len(_PNG_SIGNATURE)):+ return False+ if kind == b"IHDR":+ if end - i != 25:+ return False+ width = int.from_bytes(raw[i + 8 : i + 12], "big")+ height = int.from_bytes(raw[i + 12 : i + 16], "big")+ bits = raw[i + 16] * {0: 1, 2: 3, 3: 1, 4: 2, 6: 4}.get(raw[i + 17], 4)+ # Doubled for Adam7, whose pass rows each add a filter byte and padding.+ budget = 2 * height * ((width * bits + 7) // 8 + 1) + 64+ elif kind == b"IEND":+ return inflater.eof+ elif kind == b"IDAT":+ data = raw[i + 8 : end - 4]+ while data and not inflater.eof:+ budget -= len(inflater.decompress(data, 1 << 20))+ if budget < 0:+ return False+ data = inflater.unconsumed_tail+ elif kind != b"PLTE" and not kind[0] & 0x20:+ return False+ i = end+ except zlib.error:+ pass+ return False+++def _llama_image_data_url(raw: bytes) -> str:+ """Preserve PNG and JPEG bytes stb_image reads; convert other images to PNG.++ Avoid inflating photos while still rejecting corrupt images with Pillow:+ stb_image silently accepts some truncated JPEGs. Callers map failures to HTTP 400.+ """+ from PIL import Image++ with Image.open(io.BytesIO(raw)) as img:+ img.load()+ if raw.startswith(_PNG_SIGNATURE) and _stb_reads_png(raw):+ return f"data:image/png;base64,{base64.b64encode(raw).decode('ascii')}"+ if raw.startswith(b"\xff\xd8") and _stb_reads_jpeg(raw):+ return f"data:image/jpeg;base64,{base64.b64encode(raw).decode('ascii')}"+ return f"data:image/png;base64,{_image_bytes_to_png_b64(raw)}"++# Match llama-server's per-image limit and add a per-request budget._REMOTE_IMAGE_MAX_BYTES = 10 * 1024 * 1024_REMOTE_IMAGE_REQUEST_BUDGET_BYTES = 32 * 1024 * 1024@@ -33435,7 +33516,7 @@ def _inline_remote_image_url(rejection = _remote_image_scheme_rejection(scheme)if rejection is not None:raise HTTPException(status_code = rejection[0], detail = rejection[1])- # llama-server never read the content type, and the bytes are decoded and re-encoded here.+ # Detect the MIME type from the fetched bytes.fetched = safe_fetch_remote_image_sync(url,"image/png",@@ -33493,15 +33574,11 @@ def _inline_request_remote_images(payload) -> None:part.image_url.url = fetches.inline(part.image_url.url)-def _normalize_openai_image_parts_to_png(openai_messages: list[dict], on_image = None) -> bool:- """Re-encode every base64-data-URL ``image_url`` part to PNG, in place.+def _normalize_openai_image_parts_for_llama(openai_messages: list[dict], on_image = None) -> bool:+ """Fetch and normalize image URLs for llama-server in place.- Remote URLs are fetched here so llama-server receives bytes rather than a URL. All images- are converted to PNG for llama-server's limited image decoder.-- ``on_image`` runs once per image part before conversion, so a caller can- apply its own guard. Returns ``True`` when any image part was seen. Raises- HTTPException(400) when an image cannot be decoded or fetched.+ Calls ``on_image`` before processing each image. Returns whether any image was+ seen; raises HTTP 400 if fetching or decoding fails."""has_image = Falsefetches = _RemoteImageFetches()@@ -33529,15 +33606,14 @@ def _normalize_openai_image_parts_to_png(openai_messages: list[dict], on_image =try:_, b64data = url.split(",", 1)raw = base64.b64decode(b64data)- png_b64 = _image_bytes_to_png_b64(raw)+ data_url = _llama_image_data_url(raw)except Exception:raise HTTPException(status_code = 400,detail = "Failed to process image.",)- # Only the url is re-encoded; `detail` is the client's request, not- # part of the encoding, and replacing the object would drop it.- image_url["url"] = f"data:image/png;base64,{png_b64}"+ # Preserve the client's `detail` setting.+ image_url["url"] = data_urlreturn has_image@@ -33550,7 +33626,7 @@ def _normalize_anthropic_openai_images(openai_messages: list[dict], is_vision: bdetail = "Image provided but current GGUF model does not support vision.",)- return _normalize_openai_image_parts_to_png(openai_messages, on_image = _guard)+ return _normalize_openai_image_parts_for_llama(openai_messages, on_image = _guard)def _validate_anthropic_client_tools(tools) -> None:@@ -34604,8 +34680,7 @@ async def anthropic_messages(openai_messages = _sanitize_anthropic_openai_messages(openai_messages, llama_backend)- # Enforce vision guard + re-encode embedded images to PNG so the Anthropic- # endpoint matches /v1/chat/completions.+ # Apply the same vision guard and image normalization as /v1/chat/completions.# Promoted parts count too: _anthropic_has_image was read off the original# blocks, so a replay-only request took the synchronous branch and re-decoded up# to eight promoted PNGs on the shared loop.@@ -37123,31 +37198,14 @@ def _openai_messages_for_passthrough(normalize_images: bool = True,promote_mcp_images: bool = True,) -> list[dict]:- """Build OpenAI-format message dicts for the /v1/chat/completions- passthrough path.-- ``payload.messages`` are dumped through Pydantic (dropping unset optional- fields), so they're already standard OpenAI format -- including- ``role="tool"`` tool-result messages and assistant messages carrying- structured ``tool_calls``. Base64-data-URL images already in the list are- re-encoded to PNG exactly as ``_openai_messages_for_gguf_chat`` does, so- turning tools on does not change which formats llama-server can decode, and- a remote URL is fetched here rather than by llama-server, for the same- reason and by the same helper. The vision guard lives in the callers,- which reject a non-vision model before the body is built.-- ``normalize_images=False`` is for callers that decode these bytes themselves: re-encoding- converts to RGB, which the resize they apply would then resample.-- When a client uses Unsloth's legacy ``image_base64`` top-level field, the- image is spliced into the last user message as an OpenAI ``image_url`` content part -- as PNG- while normalizing, since llama-server's stb_image is limited, byte-for-byte otherwise.-- Only when it is a real second image, matching ``_openai_messages_for_gguf_chat`` and- what admission charges for. Studio echoes the current image into both spellings, so- splicing that unconditionally sent a tools or response_format request two copies- against a reservation for one: 4417 reserved against 8466 charged, which overcommits- the KV cache wherever the slot count lets that gap accumulate.+ """Build /v1/chat/completions messages, preserving tool calls and results.++ Uses the same image normalizer as GGUF chat; callers enforce vision support.+ ``normalize_images=False`` preserves bytes for callers that decode and resize+ images themselves, avoiding an intermediate RGB conversion.++ Adds legacy ``image_base64`` to the last user message only if it is distinct,+ matching GGUF chat and admission accounting to avoid duplicate images."""messages = _strip_provider_synthetic_tool_history(_drop_empty_assistant_sentinels([m.model_dump(exclude_none = True) for m in payload.messages])@@ -37158,7 +37216,7 @@ def _openai_messages_for_passthrough(messages = promote_mcp_history_images(messages, vision = vision)if normalize_images:- _normalize_openai_image_parts_to_png(messages)+ _normalize_openai_image_parts_for_llama(messages)if not _legacy_image_is_distinct(payload):return messages@@ -37166,13 +37224,12 @@ def _openai_messages_for_passthrough(if normalize_images:try:raw = base64.b64decode(payload.image_base64)- png_b64 = _image_bytes_to_png_b64(raw)+ data_url = _llama_image_data_url(raw)except Exception:raise HTTPException(status_code = 400,detail = "Failed to process image.",)- data_url = f"data:image/png;base64,{png_b64}"else:# As it arrived; the media type is never read back.data_url = f"data:application/octet-stream;base64,{payload.image_base64}"@@ -37256,8 +37313,7 @@ def _openai_messages_for_gguf_chat(# part already here and must not be spliced twice, but a legacy image the thread# does not hold is a real attachment. Admission charges on the same predicate.if _legacy_image_is_distinct(payload):- # Legacy bytes can be any format; the normalizer below sniffs and- # re-encodes to PNG, so the declared mime is rewritten anyway.+ # The normalizer detects the MIME type from the bytes.image_part = {"type": "image_url","image_url": {diff --git a/studio/backend/tests/test_anthropic_messages.py b/studio/backend/tests/test_anthropic_messages.pyindex b315e0f11ed..9e5c83af8ec 100644--- a/studio/backend/tests/test_anthropic_messages.py+++ b/studio/backend/tests/test_anthropic_messages.py@@ -2597,7 +2597,7 @@ class TestNormalizeAnthropicOpenAIImages:_normalize_anthropic_openai_images(msgs, is_vision = False)assert exc.value.status_code == 400- def test_reencodes_jpeg_data_url_to_png(self):+ def test_forwards_jpeg_data_url_unchanged(self):original_url = _jpeg_data_url()msgs = [{@@ -2609,9 +2609,17 @@ class TestNormalizeAnthropicOpenAIImages:}]_normalize_anthropic_openai_images(msgs, is_vision = True)- new_url = msgs[0]["content"][1]["image_url"]["url"]- assert new_url.startswith("data:image/png;base64,")- assert new_url != original_url+ assert msgs[0]["content"][1]["image_url"]["url"] == original_url++ def test_reencodes_webp_data_url_to_png(self):+ from PIL import Image++ buf = _BytesIO()+ Image.new("RGB", (2, 2), (255, 0, 0)).save(buf, format = "WEBP")+ url = "data:image/webp;base64," + _b64.b64encode(buf.getvalue()).decode("ascii")+ msgs = [{"role": "user", "content": [{"type": "image_url", "image_url": {"url": url}}]}]+ _normalize_anthropic_openai_images(msgs, is_vision = True)+ assert msgs[0]["content"][0]["image_url"]["url"].startswith("data:image/png;base64,iVBOR")def test_remote_url_is_replaced_by_the_bytes_we_fetched(self, monkeypatch):import core.inference.external_provider as ep@@ -2633,7 +2641,8 @@ class TestNormalizeAnthropicOpenAIImages:}]_normalize_anthropic_openai_images(msgs, is_vision = True)- assert msgs[0]["content"][0]["image_url"]["url"].startswith("data:image/png;base64,")+ # The bytes are JPEG whatever the server declared, and are labelled as such.+ assert msgs[0]["content"][0]["image_url"]["url"] == _jpeg_data_url()def test_bad_base64_raises_400(self):msgs = [diff --git a/studio/backend/tests/test_llama_image_passthrough.py b/studio/backend/tests/test_llama_image_passthrough.pynew file mode 100644index 00000000000..998b567960e--- /dev/null+++ b/studio/backend/tests/test_llama_image_passthrough.py@@ -0,0 +1,199 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved.++"""Images reach llama-server as the client sent them when its stb_image can decode them."""++import base64+import os+import sys+import zlib+from io import BytesIO++_backend = os.path.join(os.path.dirname(__file__), "..")+sys.path.insert(0, _backend)++import pytest+from fastapi import HTTPException+from PIL import Image++from models.inference import ChatCompletionRequest+from routes.inference import (+ _llama_image_data_url,+ _normalize_openai_image_parts_for_llama,+ _openai_messages_for_passthrough,+ _stb_reads_jpeg,+ _stb_reads_png,+)+++def _photo(mode = "RGB", size = (64, 48)):+ img = Image.effect_noise(size, 60).convert("L")+ if mode == "RGB":+ img = Image.merge("RGB", (img, img.rotate(90, expand = False), img.transpose(0)))+ return img.convert(mode) if img.mode != mode else img+++def _encode(img, fmt, **kw) -> bytes:+ buf = BytesIO()+ img.save(buf, format = fmt, **kw)+ return buf.getvalue()+++def _chunk(kind: bytes, data: bytes) -> bytes:+ crc = zlib.crc32(kind + data).to_bytes(4, "big")+ return len(data).to_bytes(4, "big") + kind + data + crc+++def _split(url: str) -> tuple[str, bytes]:+ head, b64 = url.split(",", 1)+ return head, base64.b64decode(b64)+++def _patched_frame(+ raw: bytes,+ marker: int = None,+ precision: int = None,+) -> bytes:+ """The same JPEG with its SOF marker or sample precision rewritten."""+ i = raw.index(b"\xff\xc0")+ out = bytearray(raw)+ if marker is not None:+ out[i + 1] = marker+ if precision is not None:+ out[i + 4] = precision+ return bytes(out)+++ "img, kw",+ [+ (_photo(), {}),+ (_photo(), {"progressive": True}),+ (_photo(), {"subsampling": 0}),+ (_photo("L"), {}),+ ],+ ids = ["baseline", "progressive", "444", "grey"],+)+def test_stb_readable_jpeg_is_forwarded_byte_for_byte(img, kw):+ raw = _encode(img, "JPEG", quality = 90, **kw)+ head, out = _split(_llama_image_data_url(raw))+ assert head == "data:image/jpeg;base64"+ assert out == raw++[email protected]("mode", ["RGB", "RGBA", "L", "P", "I;16"])+def test_png_is_forwarded_byte_for_byte(mode):+ raw = _encode(_photo("L").convert(mode), "PNG")+ head, out = _split(_llama_image_data_url(raw))+ assert head == "data:image/png;base64"+ assert out == raw+++def test_jpeg_is_not_inflated():+ # PNG conversion can inflate photos past llama-server's request limit.+ raw = _encode(_photo(size = (640, 480)), "JPEG", quality = 90)+ assert len(_split(_llama_image_data_url(raw))[1]) == len(raw)+++ "raw",+ [+ _encode(_photo(), "WEBP"),+ _encode(_photo(), "GIF"),+ _encode(_photo(), "BMP"),+ _encode(_photo(), "TIFF"),+ _encode(_photo("CMYK"), "JPEG"),+ ],+ ids = ["webp", "gif", "bmp", "tiff", "cmyk-jpeg"],+)+def test_other_formats_are_still_reencoded_to_png(raw):+ head, out = _split(_llama_image_data_url(raw))+ assert head == "data:image/png;base64"+ assert out.startswith(b"\x89PNG\r\n\x1a\n")+ assert Image.open(BytesIO(out)).mode == "RGB"+++def test_jpeg_frames_stb_rejects_are_not_passed_through():+ raw = _encode(_photo(), "JPEG")+ assert _stb_reads_jpeg(raw)+ assert _stb_reads_jpeg(_patched_frame(raw, marker = 0xC1)) # Extended sequential.+ assert not _stb_reads_jpeg(_patched_frame(raw, marker = 0xC3)) # Lossless.+ assert not _stb_reads_jpeg(_patched_frame(raw, marker = 0xC9)) # Arithmetic.+ assert not _stb_reads_jpeg(_patched_frame(raw, precision = 12))+ assert not _stb_reads_jpeg(_encode(_photo("CMYK"), "JPEG"))+ assert not _stb_reads_jpeg(raw[:20]) # Ends before any frame header.+++# The 64x64 PNG from the GGUF vision smoke test: its deflate stream is cut short and it has no+# IEND. Pillow decodes it; stb_image rejects it.+_UNTERMINATED_PNG = base64.b64decode(+ "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAAYklEQVR4nO3PMQ0AIADAMEAI/k"+ "UhBhEcDcmqYJtn7/GzpQNeNaA1oDWgNaA1oDWgNaA1oDWgNaA1oDWgNaA1oDWgNaA1oDWgNaA"+ "1oDWgNaA1oDWgNaA1oDWgNaA1oDWgNaA1oDWgNaBdCJ0BmMJ25zMAAAAASUVORK5CYII="+)+++def test_png_chunks_stb_rejects_are_reencoded():+ raw = _encode(_photo(), "PNG")+ assert _stb_reads_png(raw)+ assert not _stb_reads_png(raw[: raw.rindex(b"IEND") - 4]) # No IEND.+ assert not _stb_reads_png(_UNTERMINATED_PNG)+ head, out = _split(_llama_image_data_url(_UNTERMINATED_PNG))+ assert head == "data:image/png;base64"+ assert _stb_reads_png(out)+ assert Image.open(BytesIO(out)).size == (64, 64)+++def test_png_with_a_compressed_tail_past_its_rows_is_not_inflated_to_the_end():+ # 1x1 greyscale: Pillow stops after its two-byte row, a megabyte of zeros follows it.+ deflate = zlib.compressobj(9)+ body = deflate.compress(b"\0\0") + deflate.compress(bytes(1 << 20)) + deflate.flush()+ raw = _encode(Image.new("L", (1, 1)), "PNG")+ start = raw.index(b"IDAT") - 4+ idat_len = int.from_bytes(raw[start : start + 4], "big")+ raw = raw[:start] + _chunk(b"IDAT", body) + raw[start + 12 + idat_len :]+ Image.open(BytesIO(raw)).load()+ assert not _stb_reads_png(raw)+++ "raw",+ [+ b"not an image",+ b"\x89PNG\r\n\x1a\n" + b"\0" * 32,+ b"\xff\xd8\xff\xe0garbage",+ # Reject truncated data even when stb_image would accept it.+ _encode(_photo(size = (640, 480)), "JPEG", quality = 90)[:4000],+ _encode(_photo(size = (640, 480)), "PNG")[:4000],+ ],+ ids = ["text", "png-signature-only", "jpeg-soi-only", "truncated-jpeg", "truncated-png"],+)+def test_undecodable_bytes_still_fail_as_400(raw):+ url = "data:image/png;base64," + base64.b64encode(raw).decode("ascii")+ msgs = [{"role": "user", "content": [{"type": "image_url", "image_url": {"url": url}}]}]+ with pytest.raises(HTTPException) as exc:+ _normalize_openai_image_parts_for_llama(msgs)+ assert exc.value.status_code == 400+++def test_mislabelled_jpeg_gets_its_real_mime_and_keeps_detail():+ raw = _encode(_photo(), "JPEG")+ b64 = base64.b64encode(raw).decode("ascii")+ part = {+ "type": "image_url",+ "image_url": {"url": f"data:image/png;base64,{b64}", "detail": "low"},+ }+ _normalize_openai_image_parts_for_llama([{"role": "user", "content": [part]}])+ assert part["image_url"] == {"url": f"data:image/jpeg;base64,{b64}", "detail": "low"}+++def test_legacy_image_base64_jpeg_is_forwarded_unchanged():+ raw = _encode(_photo(), "JPEG")+ b64 = base64.b64encode(raw).decode("ascii")+ payload = ChatCompletionRequest(+ messages = [{"role": "user", "content": "describe"}],+ image_base64 = b64,+ )+ messages = _openai_messages_for_passthrough(payload, vision = True)+ parts = [p for p in messages[-1]["content"] if p.get("type") == "image_url"]+ assert [p["image_url"]["url"] for p in parts] == [f"data:image/jpeg;base64,{b64}"]