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
CLI: inference and chat take images for vision models (#16347)
Debojyoti Banerjeeac8e86ea54d7
diff --git a/unsloth_cli/_inference.py b/unsloth_cli/_inference.pyindex e583dfd72d6..0ef9772a1be 100644--- a/unsloth_cli/_inference.py+++ b/unsloth_cli/_inference.py@@ -406,6 +406,102 @@ def stats_hit_token_limit(stats) -> bool:return bool(stats.get("truncated")) or stats.get("finish_reason") == "length"+_IMAGE_MIME_TYPES = {+ "BMP": "image/bmp",+ "GIF": "image/gif",+ "JPEG": "image/jpeg",+ "MPO": "image/jpeg",+ "PNG": "image/png",+ "TIFF": "image/tiff",+ "WEBP": "image/webp",+}+++def image_part(path) -> dict:+ """An OpenAI image_url part carrying the file as a data URL, the shape Studio's chat sends."""+ import base64+ import io++ file = Path(path).expanduser()+ try:+ data = file.read_bytes()+ except OSError as exc:+ raise ValueError(f"can't read image {file}: {exc.strerror or exc}") from None+ # Without Pillow nothing could tell a real picture from a renamed text file.+ try:+ from PIL import Image+ except ImportError:+ raise ValueError("reading images needs Pillow: pip install pillow") from None+ try:+ with Image.open(io.BytesIO(data)) as picture:+ image_format = picture.format+ picture.verify()+ except Exception:+ raise ValueError(f"{file} is not an image file Unsloth can read") from None+ mime = _IMAGE_MIME_TYPES.get(image_format) or Image.MIME.get(image_format, "image/png")+ encoded = base64.b64encode(data).decode("ascii")+ return {"type": "image_url", "image_url": {"url": f"data:{mime};base64,{encoded}"}}+++def user_turn(text: str, images: Optional[list] = None) -> dict:+ """A user message; attached images go ahead of the text, as Studio orders them."""+ if not images:+ return {"role": "user", "content": text}+ return {"role": "user", "content": [*images, {"type": "text", "text": text}]}+++def _image_payload(part) -> Optional[str]:+ if not isinstance(part, dict) or part.get("type") != "image_url":+ return None+ url = (part.get("image_url") or {}).get("url") or ""+ if not url.startswith("data:"):+ return None+ return url.partition(",")[2] or None+++def _local_image_request(messages: list, several: bool):+ """Studio's local route shape: an image marker where each image stood, plus the payloads in order.++ A model that reads one image per conversation gets only the newest turn's, like the app.+ """+ owners = [] # (message index, user ordinal, payloads)+ ordinal = 0+ for index, message in enumerate(messages):+ if message.get("role") != "user":+ continue+ content = message.get("content")+ if isinstance(content, list):+ payloads = [p for p in map(_image_payload, content) if p]+ if payloads:+ owners.append((index, ordinal, payloads))+ ordinal += 1+ if not owners:+ return messages, None, None+ if not several:+ if len(owners[-1][2]) > 1:+ raise ValueError("this model reads one image per message; attach one image at a time")+ owners = owners[-1:]+ kept = {index: len(payloads) for index, _, payloads in owners}+ rebuilt = []+ for index, message in enumerate(messages):+ content = message.get("content")+ if not isinstance(content, list):+ rebuilt.append(message)+ continue+ text = "\n".join(+ part.get("text") or ""+ for part in content+ if isinstance(part, dict) and part.get("type") == "text"+ )+ if index in kept:+ markers = [{"type": "image"} for _ in range(kept[index])]+ rebuilt.append({**message, "content": [*markers, {"type": "text", "text": text}]})+ else:+ rebuilt.append({**message, "content": text})+ images = [payload for _, _, payloads in owners for payload in payloads]+ return rebuilt, images, owners[-1][1]++class ChatBackend:"""Uniform stream()/close() over the llama-server and Unsloth backends."""@@ -414,6 +510,18 @@ class ChatBackend:self._backend = backendself.reply_hit_token_limit = False+ def _model_entry(self) -> dict:+ backend = self._backend+ return (getattr(backend, "models", None) or {}).get(+ getattr(backend, "active_model_name", None)+ ) or {}++ @property+ def supports_images(self) -> bool:+ if self._kind == "gguf":+ return bool(getattr(self._backend, "is_vision", False))+ return bool(self._model_entry().get("is_vision"))+def stream(self,messages: list,@@ -490,6 +598,10 @@ class ChatBackend:))holder: dict = {}+ several = bool(+ (self._model_entry().get("chat_template_info") or {}).get("accepts_multiple_images")+ )+ messages, images, image_ordinal = _local_image_request(messages, several)gen_kwargs = dict(messages = messages,system_prompt = system_prompt,@@ -498,6 +610,8 @@ class ChatBackend:stats_holder = holder,**sampling,)+ if images:+ gen_kwargs.update(images = images, image_ordinal = image_ordinal)if use_adapter is not None:stream = self._backend.generate_with_adapter_control(use_adapter = use_adapter, **gen_kwargs@@ -916,6 +1030,7 @@ class HttpChatBackend:self._token = tokenself.reply_hit_token_limit = Falseself.gguf_variant: Optional[str] = None+ self.supports_images = Falsedef _request(self,@@ -973,13 +1088,14 @@ class HttpChatBackend:self.gguf_variant = resident_varianttry:# Read the whole body, not just headers: closing early discards the only in-band report of a late load failure.- read_json_checking_deferred_error(+ loaded = read_json_checking_deferred_error(self._base + "/api/inference/load",self._request("POST", "/api/inference/load", payload),)except Exception as exc:typer.echo(f"Model load failed: {exc}", err = True)raise typer.Exit(code = 1)+ self.supports_images = isinstance(loaded, dict) and bool(loaded.get("is_vision"))def _resident_gguf_variant(self, model: str) -> Optional[str]:if model.lower().endswith(".gguf"):diff --git a/unsloth_cli/commands/chat.py b/unsloth_cli/commands/chat.pyindex b5893be6293..2912c4eaf8d 100644--- a/unsloth_cli/commands/chat.py+++ b/unsloth_cli/commands/chat.py@@ -2,6 +2,7 @@# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0import sys+from pathlib import Pathfrom types import SimpleNamespacefrom typing import List, Optional@@ -13,6 +14,7 @@ from unsloth_cli._inference import (configure_quiet_logging,connect_studio_server,ensure_studio_backend_path,+ image_part,load_chat_backend,mlx_distributed_info,mlx_distributed_uses_mpi,@@ -22,15 +24,25 @@ from unsloth_cli._inference import (resolve_model_config,server_load_opts,stream_markdown,+ user_turn,visible_text,)_HELP = ("Commands: /exit (quit), /reset (clear history), "- "/think (toggle reasoning), /compare (base vs tuned), /help"+ "/think (toggle reasoning), /compare (base vs tuned), "+ "/image <path> (attach to your next message), /help")+def _image_path(argument: str) -> str:+ # A path dragged into a terminal arrives quoted when it has spaces.+ argument = argument.strip()+ if len(argument) >= 2 and argument[0] == argument[-1] and argument[0] in "\"'":+ argument = argument[1:-1]+ return argument++def _you_prompt(colors: bool) -> str:# Must go through input(): readline redraws erase text they did not draw. GNU readline wants# \001/\002 around colors; libedit (macOS) prints those literally.@@ -167,6 +179,12 @@ def chat(model: Optional[str] = typer.Argument(None, help = "HF model id or local path. Omit to pick one of your local models."),+ image: Optional[List[Path]] = typer.Option(+ None,+ "--image",+ help = "Image file to attach to your first message, for vision models. Repeat for "+ "several; attach more later with /image <path>.",+ ),hf_token: Optional[str] = typer.Option(None, "--hf-token", envvar = "HF_TOKEN", help = "Hugging Face token if needed."),@@ -275,6 +293,21 @@ def chat()raise typer.Exit(code = 1)+ if image and is_mlx_distributed:+ if should_print:+ err.print(+ "Distributed `unsloth chat` does not take images yet.",+ style = "red",+ markup = False,+ )+ raise typer.Exit(code = 1)+ # Read the images before the slow load, so a bad path fails at once.+ try:+ pending_images = [image_part(path) for path in image or ()]+ except ValueError as exc:+ err.print(f"Error: {exc}", style = "red", markup = False)+ raise typer.Exit(code = 1)+if model is None:if is_mlx_distributed:if should_print:@@ -368,6 +401,15 @@ def chat(else:base = model.replace("\\", "/").rstrip("/").rsplit("/", 1)[-1]name = f"{base} ({kept})"+ supports_images = bool(getattr(chat_backend, "supports_images", False))+ if pending_images and not supports_images:+ chat_backend.close()+ err.print(+ f"Error: {name} is not a vision model, so it can't read --image.",+ style = "red",+ markup = False,+ )+ raise typer.Exit(code = 1)show_thinking = thinkcompare_mode = comparemessages = []@@ -469,9 +511,37 @@ def chat(breakif user == "/reset":messages = []+ pending_images = []if should_print:console.print("(history cleared)", style = "bright_black")continue+ if user == "/image" or user.startswith("/image "):+ if not should_print:+ continue+ path = _image_path(user[len("/image") :])+ if is_mlx_distributed:+ console.print("(distributed chat does not take images yet)", style = "yellow")+ elif not supports_images:+ console.print(+ f"({name} is not a vision model, so it can't read images)",+ style = "yellow",+ markup = False,+ )+ elif not path:+ console.print("(usage: /image <path>)", style = "yellow")+ else:+ try:+ pending_images.append(image_part(path))+ except ValueError as exc:+ console.print(f"({exc})", style = "yellow", markup = False)+ else:+ count = len(pending_images)+ noun = "image" if count == 1 else "images"+ console.print(+ f"({count} {noun} attached to your next message)",+ style = "bright_black",+ )+ continueif user == "/think":show_thinking = not show_thinkingif should_print:@@ -495,7 +565,8 @@ def chat(console.print(_HELP, style = "bright_black")continue- messages.append({"role": "user", "content": user})+ messages.append(user_turn(user, pending_images))+ sent_images, pending_images = pending_images, []try:if compare_mode:@@ -520,10 +591,11 @@ def chat(else:answer = collect_stream(generate(), show_thinking)except KeyboardInterrupt:- # Ctrl-C aborts this answer only; drop the unanswered turn.+ # Ctrl-C aborts this answer only; drop the unanswered turn, keep its images for a retry.if should_print:console.print("\n(interrupted)", style = "bright_black")messages.pop()+ pending_images = sent_imagescontinueexcept Exception as exc:if should_print:diff --git a/unsloth_cli/commands/inference.py b/unsloth_cli/commands/inference.pyindex b2b6f9c412e..abb43a5cad6 100644--- a/unsloth_cli/commands/inference.py+++ b/unsloth_cli/commands/inference.py@@ -1,6 +1,7 @@# SPDX-License-Identifier: AGPL-3.0-only# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0+from pathlib import Pathfrom typing import List, Optionalimport typer@@ -10,12 +11,14 @@ from unsloth_cli._inference import (collect_stream,configure_quiet_logging,connect_studio_server,+ image_part,load_chat_backend,mlx_distributed_info,mlx_distributed_uses_mpi,raise_on_streamed_error,server_load_opts,stream_to_stdout,+ user_turn,)@@ -23,6 +26,11 @@ def inference(ctx: typer.Context,model: str = typer.Argument(..., help = "HF model id or local path."),prompt: str = typer.Argument(..., help = "Prompt to send to the model."),+ image: Optional[List[Path]] = typer.Option(+ None,+ "--image",+ help = "Image file to send with the prompt, for vision models. Repeat for several.",+ ),hf_token: Optional[str] = typer.Option(None, "--hf-token", envvar = "HF_TOKEN", help = "Hugging Face token if needed."),@@ -113,6 +121,13 @@ def inference(if not verbose:configure_quiet_logging()+ # Read the images before the slow load, so a bad path fails at once.+ try:+ images = [image_part(path) for path in image or ()]+ except ValueError as exc:+ typer.echo(f"Error: {exc}", err = True)+ raise typer.Exit(code = 1)+is_mlx_distributed, rank, _world_size = mlx_distributed_info()if is_mlx_distributed and mlx_distributed_uses_mpi():if rank == 0:@@ -154,16 +169,28 @@ def inference(if chat_backend is None:chat_backend = load_chat_backend(model, **load_opts)try:- stream = chat_backend.stream(- [{"role": "user", "content": prompt}],- system_prompt = system_prompt,- temperature = temperature,- top_p = top_p,- top_k = top_k,- max_new_tokens = max_new_tokens,- repetition_penalty = repetition_penalty,- enable_thinking = think,- )+ if images and not getattr(chat_backend, "supports_images", False):+ if rank == 0:+ typer.echo(+ f"Error: {model} is not a vision model, so it can't read --image.",+ err = True,+ )+ raise typer.Exit(code = 1)+ try:+ stream = chat_backend.stream(+ [user_turn(prompt, images)],+ system_prompt = system_prompt,+ temperature = temperature,+ top_p = top_p,+ top_k = top_k,+ max_new_tokens = max_new_tokens,+ repetition_penalty = repetition_penalty,+ enable_thinking = think,+ )+ except ValueError as exc:+ if rank == 0:+ typer.echo(f"Error: {exc}", err = True)+ raise typer.Exit(code = 1)stream = raise_on_streamed_error(stream)if rank == 0:typer.echo("Assistant:")diff --git a/unsloth_cli/tests/test_cli_image_input.py b/unsloth_cli/tests/test_cli_image_input.pynew file mode 100644index 00000000000..03328c3630f--- /dev/null+++ b/unsloth_cli/tests/test_cli_image_input.py@@ -0,0 +1,520 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""`unsloth inference --image` and `unsloth chat --image` / `/image` (#16340)."""++from __future__ import annotations++import base64+import io+import json+import sys+import threading+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer++import pytest+import typer+from PIL import Image+from typer.testing import CliRunner++import unsloth_cli.commands.chat as chatmod+import unsloth_cli.commands.inference as infermod+from unsloth_cli._inference import (+ ChatBackend,+ HttpChatBackend,+ _local_image_request,+ ensure_studio_backend_path,+ image_part,+ user_turn,+)+++class _FakeConfig:+ is_gguf = False+ is_lora = False+ display_name = "fake-vlm"+ base_model = None+ path = None+++def _png(path, color = (255, 0, 0)):+ Image.new("RGB", (8, 8), color).save(path, format = "PNG")+ return path+++def _decoded(part) -> Image.Image:+ url = part["image_url"]["url"]+ return Image.open(io.BytesIO(base64.b64decode(url.partition(",")[2])))+++def _app(command):+ cli = typer.Typer()+ cli.command()(command)+ return cli+++class _RecordingBackend:+ def __init__(self, supports_images = True):+ self.supports_images = supports_images+ self.streams = []+ self.closed = False++ def stream(self, messages, **kwargs):+ self.streams.append([dict(m) for m in messages])+ return iter(["a red square"])++ def close(self):+ self.closed = True+++_STREAM_KWARGS = dict(+ system_prompt = "",+ temperature = 0.7,+ top_p = 0.9,+ top_k = 40,+ max_new_tokens = 8,+ repetition_penalty = 1.0,+ enable_thinking = False,+)+++# ── image_part ────────────────────────────────────────────────────+++def test_image_part_carries_the_file_as_a_data_url(tmp_path):+ part = image_part(_png(tmp_path / "red.png"))++ assert part["type"] == "image_url"+ assert part["image_url"]["url"].startswith("data:image/png;base64,")+ assert _decoded(part).getpixel((0, 0)) == (255, 0, 0)+++def test_image_part_names_jpeg_by_its_content_not_its_extension(tmp_path):+ path = tmp_path / "photo.png"+ Image.new("RGB", (4, 4), (0, 0, 255)).save(path, format = "JPEG")++ assert image_part(path)["image_url"]["url"].startswith("data:image/jpeg;base64,")+++def test_image_part_rejects_a_missing_or_non_image_file(tmp_path):+ with pytest.raises(ValueError, match = "can't read image"):+ image_part(tmp_path / "missing.png")+ notes = tmp_path / "notes.png"+ notes.write_text("not a picture", encoding = "utf-8")+ with pytest.raises(ValueError, match = "not an image file"):+ image_part(notes)+++def test_image_part_refuses_rather_than_guesses_without_pillow(monkeypatch, tmp_path):+ path = _png(tmp_path / "red.png")+ # None in sys.modules makes `from PIL import Image` raise ImportError.+ monkeypatch.setitem(sys.modules, "PIL", None)++ with pytest.raises(ValueError, match = "needs Pillow"):+ image_part(path)+++def test_user_turn_puts_images_ahead_of_the_text(tmp_path):+ part = image_part(_png(tmp_path / "a.png"))++ assert user_turn("hi") == {"role": "user", "content": "hi"}+ assert user_turn("what is this?", [part]) == {+ "role": "user",+ "content": [part, {"type": "text", "text": "what is this?"}],+ }+++# ── unsloth inference --image ─────────────────────────────────────+++def _local_inference(monkeypatch, backend):+ monkeypatch.setattr(infermod, "connect_studio_server", lambda *a, **k: None)+ monkeypatch.setattr(infermod, "load_chat_backend", lambda *a, **k: backend)+++def test_inference_sends_each_image_with_the_prompt(monkeypatch, tmp_path):+ backend = _RecordingBackend()+ _local_inference(monkeypatch, backend)+ red, blue = _png(tmp_path / "red.png"), _png(tmp_path / "blue.png", (0, 0, 255))++ result = CliRunner().invoke(+ _app(infermod.inference),+ ["fake-vlm", "compare these", "--image", str(red), "--image", str(blue)],+ )++ assert result.exit_code == 0, result.output+ assert "a red square" in result.stdout+ [[turn]] = backend.streams+ *images, text = turn["content"]+ assert text == {"type": "text", "text": "compare these"}+ assert [_decoded(part).getpixel((0, 0)) for part in images] == [(255, 0, 0), (0, 0, 255)]+ assert backend.closed+++def test_inference_without_image_keeps_a_plain_text_turn(monkeypatch):+ backend = _RecordingBackend(supports_images = False)+ _local_inference(monkeypatch, backend)++ result = CliRunner().invoke(_app(infermod.inference), ["fake-model", "hello"])++ assert result.exit_code == 0, result.output+ assert backend.streams == [[{"role": "user", "content": "hello"}]]+++def test_inference_reports_a_bad_image_path_before_loading(monkeypatch, tmp_path):+ loads = []+ monkeypatch.setattr(infermod, "connect_studio_server", lambda *a, **k: loads.append(1))+ monkeypatch.setattr(infermod, "load_chat_backend", lambda *a, **k: loads.append(1))++ result = CliRunner().invoke(+ _app(infermod.inference),+ ["fake-vlm", "hi", "--image", str(tmp_path / "missing.png")],+ )++ assert result.exit_code == 1+ assert "can't read image" in result.output+ assert loads == []+++def test_inference_refuses_an_image_for_a_text_only_model(monkeypatch, tmp_path):+ backend = _RecordingBackend(supports_images = False)+ _local_inference(monkeypatch, backend)++ result = CliRunner().invoke(+ _app(infermod.inference),+ ["text-model", "hi", "--image", str(_png(tmp_path / "a.png"))],+ )++ assert result.exit_code == 1+ assert "not a vision model" in result.output+ assert backend.streams == []+ assert backend.closed+++def test_inference_and_chat_list_the_image_option():+ for command in (infermod.inference, chatmod.chat):+ result = CliRunner().invoke(_app(command), ["--help"])+ assert "--image" in result.output+++# ── unsloth chat --image and /image ───────────────────────────────+++def _local_chat(monkeypatch, backend):+ monkeypatch.setattr(chatmod, "resolve_model_config", lambda *a, **k: _FakeConfig())+ monkeypatch.setattr(chatmod, "connect_studio_server", lambda *a, **k: None)+ monkeypatch.setattr(chatmod, "load_chat_backend", lambda *a, **k: backend)+++def _user_turns(stream):+ return [m for m in stream if m["role"] == "user"]+++def test_chat_image_flag_rides_on_the_first_message_only(monkeypatch, tmp_path):+ backend = _RecordingBackend()+ _local_chat(monkeypatch, backend)++ result = CliRunner().invoke(+ _app(chatmod.chat),+ ["fake-vlm", "--image", str(_png(tmp_path / "red.png"))],+ input = "what colour is it?\nand now?\n/exit\n",+ )++ assert result.exit_code == 0, result.output+ first, second = backend.streams+ [turn] = _user_turns(first)+ assert turn["content"][0]["type"] == "image_url"+ assert turn["content"][-1] == {"type": "text", "text": "what colour is it?"}+ # The history keeps the picture; the follow-up question carries none of its own.+ assert _user_turns(second)[0] == turn+ assert _user_turns(second)[1] == {"role": "user", "content": "and now?"}+++def test_chat_slash_image_attaches_to_the_next_message(monkeypatch, tmp_path):+ backend = _RecordingBackend()+ _local_chat(monkeypatch, backend)+ folder = tmp_path / "my pictures"+ folder.mkdir()+ blue = _png(folder / "blue.png", (0, 0, 255))++ result = CliRunner().invoke(+ _app(chatmod.chat),+ ["fake-vlm"],+ input = f'hello\n/image "{blue}"\nwhat is this?\n/exit\n',+ )++ assert result.exit_code == 0, result.output+ assert "(1 image attached to your next message)" in result.output+ first, second = backend.streams+ assert _user_turns(first) == [{"role": "user", "content": "hello"}]+ attached = _user_turns(second)[1]["content"]+ assert _decoded(attached[0]).getpixel((0, 0)) == (0, 0, 255)+ assert attached[1] == {"type": "text", "text": "what is this?"}+++def test_chat_reset_drops_a_pending_image(monkeypatch, tmp_path):+ backend = _RecordingBackend()+ _local_chat(monkeypatch, backend)++ result = CliRunner().invoke(+ _app(chatmod.chat),+ ["fake-vlm"],+ input = f"/image {_png(tmp_path / 'a.png')}\n/reset\nhi\n/exit\n",+ )++ assert result.exit_code == 0, result.output+ assert backend.streams == [[{"role": "user", "content": "hi"}]]+++def test_chat_slash_image_explains_a_bad_path_and_keeps_chatting(monkeypatch, tmp_path):+ backend = _RecordingBackend()+ _local_chat(monkeypatch, backend)++ result = CliRunner().invoke(+ _app(chatmod.chat),+ ["fake-vlm"],+ input = f"/image {tmp_path / 'missing.png'}\n/image\nhi\n/exit\n",+ )++ assert result.exit_code == 0, result.output+ assert "can't read image" in result.output+ assert "(usage: /image <path>)" in result.output+ assert backend.streams == [[{"role": "user", "content": "hi"}]]+++def test_chat_text_only_model_refuses_images(monkeypatch, tmp_path):+ picture = _png(tmp_path / "a.png")++ backend = _RecordingBackend(supports_images = False)+ _local_chat(monkeypatch, backend)+ result = CliRunner().invoke(_app(chatmod.chat), ["fake-model", "--image", str(picture)])+ assert result.exit_code == 1+ assert "not a vision model" in result.output+ assert backend.closed and backend.streams == []++ backend = _RecordingBackend(supports_images = False)+ _local_chat(monkeypatch, backend)+ result = CliRunner().invoke(+ _app(chatmod.chat), ["fake-model"], input = f"/image {picture}\nhi\n/exit\n"+ )+ assert result.exit_code == 0, result.output+ assert "not a vision model, so it can't read images" in result.output+ assert backend.streams == [[{"role": "user", "content": "hi"}]]+++def test_chat_help_lists_the_image_command():+ assert "/image <path>" in chatmod._HELP+++# ── backends ──────────────────────────────────────────────────────+++class _FakeOrchestrator:+ def __init__(+ self,+ *,+ is_vision = True,+ several = False,+ ):+ self.active_model_name = "fake/vlm"+ self.models = {+ "fake/vlm": {+ "is_vision": is_vision,+ "chat_template_info": {"accepts_multiple_images": several},+ }+ }+ self.calls = []++ def generate_chat_response(self, **kwargs):+ self.calls.append(kwargs)+ return iter(["ok"])+++def _parts(tmp_path, *colors):+ return [+ image_part(_png(tmp_path / f"{index}.png", color)) for index, color in enumerate(colors)+ ]+++def _payload(part):+ return part["image_url"]["url"].partition(",")[2]+++def test_chatbackend_reports_vision_support_per_backend():+ assert ChatBackend("unsloth", _FakeOrchestrator()).supports_images+ assert not ChatBackend("unsloth", _FakeOrchestrator(is_vision = False)).supports_images++ class _Gguf:+ is_vision = True++ assert ChatBackend("gguf", _Gguf()).supports_images+ assert not ChatBackend("gguf", object()).supports_images+++def test_unsloth_backend_gets_markers_and_payloads_like_the_studio_route(tmp_path):+ red, blue = _parts(tmp_path, (255, 0, 0), (0, 0, 255))+ fake = _FakeOrchestrator(several = True)+ messages = [+ user_turn("first", [red]),+ {"role": "assistant", "content": "a red square"},+ user_turn("second", [blue]),+ ]++ list(ChatBackend("unsloth", fake).stream(messages, **_STREAM_KWARGS))++ [call] = fake.calls+ assert call["messages"] == [+ {"role": "user", "content": [{"type": "image"}, {"type": "text", "text": "first"}]},+ {"role": "assistant", "content": "a red square"},+ {"role": "user", "content": [{"type": "image"}, {"type": "text", "text": "second"}]},+ ]+ assert call["images"] == [_payload(red), _payload(blue)]+ assert call["image_ordinal"] == 1+++def test_single_image_model_gets_only_the_newest_image(tmp_path):+ red, blue = _parts(tmp_path, (255, 0, 0), (0, 0, 255))+ fake = _FakeOrchestrator(several = False)+ messages = [+ user_turn("first", [red]),+ {"role": "assistant", "content": "a red square"},+ user_turn("second", [blue]),+ {"role": "assistant", "content": "a blue square"},+ user_turn("third"),+ ]++ list(ChatBackend("unsloth", fake).stream(messages, **_STREAM_KWARGS))++ [call] = fake.calls+ assert call["messages"][0] == {"role": "user", "content": "first"}+ assert call["messages"][2] == {+ "role": "user",+ "content": [{"type": "image"}, {"type": "text", "text": "second"}],+ }+ assert call["messages"][4] == {"role": "user", "content": "third"}+ assert call["images"] == [_payload(blue)]+ assert call["image_ordinal"] == 1+++def test_single_image_model_refuses_two_images_in_one_message(tmp_path):+ fake = _FakeOrchestrator(several = False)+ two = user_turn("compare", _parts(tmp_path, (255, 0, 0), (0, 0, 255)))++ with pytest.raises(ValueError, match = "one image per message"):+ ChatBackend("unsloth", fake).stream([two], **_STREAM_KWARGS)+ assert fake.calls == []+++def test_text_only_conversation_reaches_the_unsloth_backend_unchanged():+ fake = _FakeOrchestrator()++ list(ChatBackend("unsloth", fake).stream([user_turn("hi")], **_STREAM_KWARGS))++ assert fake.calls[0]["messages"] == [{"role": "user", "content": "hi"}]+ assert "images" not in fake.calls[0]+++def test_gguf_backend_gets_openai_image_parts_for_llama_server(tmp_path):+ calls = []++ class _Gguf:+ is_vision = True++ def generate_chat_completion(self, **kwargs):+ calls.append(kwargs)+ return iter(["ok"])++ [red] = _parts(tmp_path, (255, 0, 0))+ turn = user_turn("what is this?", [red])++ list(ChatBackend("gguf", _Gguf()).stream([turn], **_STREAM_KWARGS))++ assert calls[0]["messages"] == [turn]+++def test_markers_survive_studios_real_vision_message_assembly(tmp_path):+ """The shapes the CLI builds go through the same helpers the transformers backend runs."""+ ensure_studio_backend_path()+ from core.inference.chat_template_helpers import messages_with_attached_image+ from core.inference.mcp_images import image_marker_parts, top_up_image_markers++ red, blue = _parts(tmp_path, (255, 0, 0), (0, 0, 255))+ history = [+ user_turn("first", [red]),+ {"role": "assistant", "content": "a red square"},+ user_turn("second", [blue]),+ {"role": "assistant", "content": "a blue square"},+ user_turn("which was first?"),+ ]+ for several, expected in ((True, 2), (False, 1)):+ messages, images, ordinal = _local_image_request(history, several)+ assembled = top_up_image_markers(+ messages_with_attached_image(+ messages, fallback_user_text = "which was first?", structured_content = True+ ),+ len(images),+ ordinal = ordinal,+ )+ # One marker per picture, each still on the turn that sent it; the question carries none.+ assert len(image_marker_parts(assembled)) == len(images) == expected+ assert assembled[-1]["content"] == [{"type": "text", "text": "which was first?"}]+++# ── a running Unsloth server ──────────────────────────────────────+++class _StudioStub(BaseHTTPRequestHandler):+ received: list = []++ def log_message(self, *args):+ pass++ def do_POST(self):+ body = json.loads(self.rfile.read(int(self.headers["Content-Length"])))+ type(self).received.append((self.path, body))+ if self.path == "/api/inference/load":+ reply = json.dumps({"status": "loaded", "model": "m", "is_vision": True}).encode()+ self.send_response(200)+ self.send_header("Content-Type", "application/json")+ self.send_header("Content-Length", str(len(reply)))+ self.end_headers()+ self.wfile.write(reply)+ return+ # Answer with what the server decoded, so the test sees the picture arrive intact.+ sizes = []+ for message in body["messages"]:+ for part in message["content"] if isinstance(message["content"], list) else ():+ if part["type"] == "image_url":+ sizes.append("x".join(map(str, _decoded(part).size)))+ self.send_response(200)+ self.send_header("Content-Type", "text/event-stream")+ self.end_headers()+ chunk = {"choices": [{"delta": {"content": f"saw {','.join(sizes)}"}}]}+ self.wfile.write(f"data: {json.dumps(chunk)}\n\ndata: [DONE]\n\n".encode())+++def studio_stub():+ _StudioStub.received = []+ server = ThreadingHTTPServer(("127.0.0.1", 0), _StudioStub)+ thread = threading.Thread(target = server.serve_forever, daemon = True)+ thread.start()+ try:+ yield f"http://127.0.0.1:{server.server_address[1]}"+ finally:+ server.shutdown()+ server.server_close()+++def test_server_backend_posts_images_as_openai_parts(studio_stub, tmp_path):+ backend = HttpChatBackend(studio_stub, "token")+ backend.ensure_loaded("m", hf_token = None, max_seq_length = 0, load_in_4bit = None)+ assert backend.supports_images++ part = image_part(_png(tmp_path / "a.png"))+ answer = list(backend.stream([user_turn("what is this?", [part])], **_STREAM_KWARGS))++ assert answer[-1] == "saw 8x8"+ path, body = _StudioStub.received[-1]+ assert path == "/v1/chat/completions"+ assert body["messages"] == [user_turn("what is this?", [part])]