Skip to content

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.

Studio: keep built-in tools on when an MLX chat has several images (#17423)

Nilayd80e9a5e7fc1
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index ef097842f1c..32e216f4001 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -34600,8 +34600,7 @@ async def produce_openai_chat_completions(
# Images stay in the loop only for vision models without a client catalog, so client
# tools are not swapped for built-ins. Replayed MCP images count too.
and (not _sf_has_any_image or (bool(_sf_model_info.get("is_vision")) and not payload.tools))
- # A caller's several images and a video clip stay out of the loop, as on main.
- and not images
+ # A video clip stays out of the loop, as on main.
and not _video_clips
and not _sf_is_gptoss
and _sf_tool_budget > 0
@@ -34751,6 +34750,11 @@ async def produce_openai_chat_completions(
_pil_to_png_b64(image),
placed_at = _sf_attachment_at,
)
+ elif images:
+ # Promotion already interleaved the caller's decoded images in marker order.
+ _sf_attachment_at = [
+ at for at, one in enumerate(_sf_loop_images) if any(one is own for own in images)
+ ]
# Request-scoped usage/timings receptacle (filled at gen_done).
_sf_stats_holder: dict = {}
diff --git a/studio/backend/tests/test_openai_multi_image_route.py b/studio/backend/tests/test_openai_multi_image_route.py
index b65ca43c967..98cf57bd535 100644
--- a/studio/backend/tests/test_openai_multi_image_route.py
+++ b/studio/backend/tests/test_openai_multi_image_route.py
@@ -5,6 +5,7 @@
import base64
import io
+import json
import os
import sys
@@ -212,6 +213,63 @@ def test_a_16_bit_image_on_a_multi_image_turn_keeps_its_levels_too(monkeypatch):
assert [levels.getpixel(at)[0] for at in ((0, 0), (1, 0), (0, 1), (1, 1))] == [0, 77, 155, 255]
+def _tool_picture(size: int) -> list:
+ data = _sized(size)["image_url"]["url"].split(",", 1)[1]
+ call = {
+ "id": "call_1",
+ "type": "function",
+ "function": {"name": "mcp__a__shot", "arguments": "{}"},
+ }
+ return [
+ ChatMessage(role = "assistant", content = "", tool_calls = [call]),
+ ChatMessage(
+ role = "tool",
+ tool_call_id = "call_1",
+ name = "mcp__a__shot",
+ content = "[1 image returned]\n__MCP_IMAGES__:"
+ + json.dumps([{"mimeType": "image/png", "data": data}]),
+ ),
+ ChatMessage(role = "assistant", content = "taken"),
+ ]
+
+
+ "between, sizes, own",
+ [
+ ([ChatMessage(role = "assistant", content = "seen")], [2, 4], (0, 1)),
+ (_tool_picture(8), [2, 8, 4], (0, 2)),
+ ],
+ ids = ["two turns", "a tool's picture between them"],
+)
+def test_built_in_tools_stay_on_once_a_conversation_carries_several_images(
+ monkeypatch, between, sizes, own
+):
+ backend = passthrough._ToolLoopBackend(passthrough._fixed("done"))
+ backend.models["sf-model"].update(
+ is_vision = True,
+ chat_template_info = {
+ "template": "<tool_call> chatml",
+ "renders_image": True,
+ "accepts_multiple_images": True,
+ },
+ )
+ messages = [
+ ChatMessage(role = "user", content = [_sized(2), _text("one")]),
+ *between,
+ ChatMessage(role = "user", content = [_sized(4), _text("two")]),
+ ChatMessage(role = "assistant", content = "seen"),
+ ChatMessage(role = "user", content = "search the web for both"),
+ ]
+ payload = passthrough._request(messages = messages, stream = False, enable_tools = True)
+
+ passthrough._call(payload, monkeypatch, backend)
+
+ [call] = backend.calls
+ assert call.get("caller_image_indexes") == own
+ assert [_as_image(image).width for image in call["images"]] == sizes
+ assert sum(len(_markers(message)) for message in call["messages"]) == len(sizes)
+
+
_PDF = {
"type": "input_document",
"file_data": "data:application/pdf;base64,JVBERi0xLjQK",