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: send the most recent chat image to vision models (#9638)

Nilay7df3f14757ad
diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.py
index ea65ffcde00..136b1337c69 100644
--- a/studio/backend/routes/inference.py
+++ b/studio/backend/routes/inference.py
@@ -14074,11 +14074,12 @@ def _extract_content_parts(messages: list) -> tuple[str, list[dict], "Optional[s
system_prompt: System message text (empty string if none).
chat_messages: Non-system messages with content flattened to strings and
assistant reasoning_content preserved.
- image_base64: Base64 of the *first* image found, or ``None``.
+ image_base64: Base64 of the most recent image found, or ``None``.
"""
system_parts: list[str] = []
chat_messages: list[dict] = []
- first_image_b64: Optional[str] = None
+ latest_image_b64: Optional[str] = None
+ latest_user_image_b64: Optional[str] = None
for msg in messages:
# ── System / developer messages → extract as system_prompt ────────
@@ -14098,16 +14099,32 @@ def _extract_content_parts(messages: list) -> tuple[str, list[dict], "Optional[s
elif isinstance(msg.content, list):
# Multimodal content parts
text_parts: list[str] = []
+ message_image_b64: Optional[str] = None
for part in msg.content:
if part.type == "text":
text_parts.append(part.text)
- elif part.type == "image_url" and first_image_b64 is None:
+ elif part.type == "image_url":
url = part.image_url.url
if url.startswith("data:"):
- # data:image/png;base64,<DATA> -> extract <DATA>
- first_image_b64 = url.split(",", 1)[1] if "," in url else None
+ # A payloadless data URL is not an image, so it must not
+ # displace a real one from an earlier turn.
+ part_b64 = url.partition(",")[2]
+ if part_b64:
+ # First within a message, matching the frontend's
+ # findLatestUserImageBase64 (chat-adapter.ts).
+ if message_image_b64 is None:
+ message_image_b64 = part_b64
+ else:
+ logger.warning(
+ f"Ignoring image URL with no base64 payload: {url[:80]}..."
+ )
else:
logger.warning(f"Remote image URLs not yet supported: {url[:80]}...")
+ # Latest message wins: send the image just attached, not the thread's first.
+ if message_image_b64 is not None:
+ latest_image_b64 = message_image_b64
+ if msg.role == "user":
+ latest_user_image_b64 = message_image_b64
combined_text = "\n".join(text_parts) if text_parts else ""
elif msg.role == "assistant" and msg.reasoning_content:
# A reasoning-only turn has no visible content, but still needs a
@@ -14121,7 +14138,13 @@ def _extract_content_parts(messages: list) -> tuple[str, list[dict], "Optional[s
chat_message["reasoning_content"] = msg.reasoning_content
chat_messages.append(chat_message)
- return "\n\n".join(p for p in system_parts if p), chat_messages, first_image_b64
+ # A user's own attachment outranks an assistant-generated one, as the frontend's
+ # legacy image_base64 field does. An assistant-only history still falls back.
+ return (
+ "\n\n".join(p for p in system_parts if p),
+ chat_messages,
+ latest_user_image_b64 or latest_image_b64,
+ )
# ── External provider proxy ──────────────────────────────────────
diff --git a/studio/backend/tests/test_openai_tool_passthrough.py b/studio/backend/tests/test_openai_tool_passthrough.py
index c2a944204a3..9af6cce87bf 100644
--- a/studio/backend/tests/test_openai_tool_passthrough.py
+++ b/studio/backend/tests/test_openai_tool_passthrough.py
@@ -1858,6 +1858,238 @@ class TestOpenAICompatibilityHelpers:
assert chat_messages == [{"role": "user", "content": "hi"}]
assert image_b64 is None
+ def test_latest_image_wins_over_earlier_turns(self):
+ def turn(letter):
+ return {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": f"what is {letter}?"},
+ {
+ "type": "image_url",
+ "image_url": {"url": f"data:image/png;base64,{letter}"},
+ },
+ ],
+ }
+
+ payload = ChatCompletionRequest(
+ messages = [
+ turn("AAAA"),
+ {"role": "assistant", "content": "the first one"},
+ turn("BBBB"),
+ ]
+ )
+
+ _, _, image_b64 = _extract_content_parts(payload.messages)
+
+ assert image_b64 == "BBBB"
+
+ def test_single_image_still_returned(self):
+ payload = ChatCompletionRequest(
+ messages = [
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "what is this?"},
+ {
+ "type": "image_url",
+ "image_url": {"url": "data:image/png;base64,AAAA"},
+ },
+ ],
+ }
+ ]
+ )
+
+ _, _, image_b64 = _extract_content_parts(payload.messages)
+
+ assert image_b64 == "AAAA"
+
+ def test_payloadless_later_image_keeps_earlier_one(self):
+ def turn(url):
+ return {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "what is this?"},
+ {"type": "image_url", "image_url": {"url": url}},
+ ],
+ }
+
+ for empty_url in ("data:image/png;base64,", "data:image/png;base64"):
+ payload = ChatCompletionRequest(
+ messages = [
+ turn("data:image/png;base64,AAAA"),
+ {"role": "assistant", "content": "a cat"},
+ turn(empty_url),
+ ]
+ )
+
+ _, _, image_b64 = _extract_content_parts(payload.messages)
+
+ assert image_b64 == "AAAA", empty_url
+
+ def test_user_attachment_outranks_assistant_generated_image(self):
+ payload = ChatCompletionRequest(
+ messages = [
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "what is this?"},
+ {
+ "type": "image_url",
+ "image_url": {"url": "data:image/png;base64,USERPHOTO"},
+ },
+ ],
+ },
+ {
+ "role": "assistant",
+ "content": [
+ {"type": "text", "text": "here is a cartoon of it"},
+ {
+ "type": "image_url",
+ "image_url": {"url": "data:image/png;base64,GENERATED"},
+ },
+ ],
+ },
+ {"role": "user", "content": "what colour was the shirt?"},
+ ]
+ )
+
+ _, _, image_b64 = _extract_content_parts(payload.messages)
+
+ assert image_b64 == "USERPHOTO"
+
+ def test_assistant_only_image_is_still_returned(self):
+ payload = ChatCompletionRequest(
+ messages = [
+ {"role": "user", "content": "show me something"},
+ {
+ "role": "assistant",
+ "content": [
+ {"type": "text", "text": "here you go"},
+ {
+ "type": "image_url",
+ "image_url": {"url": "data:image/png;base64,GENERATED"},
+ },
+ ],
+ },
+ {"role": "user", "content": "describe it"},
+ ]
+ )
+
+ _, _, image_b64 = _extract_content_parts(payload.messages)
+
+ assert image_b64 == "GENERATED"
+
+ def test_first_image_wins_within_one_message(self):
+ # The composer allows multi-select, and findLatestUserImageBase64
+ # (chat-adapter.ts) names the first of them, so this must agree.
+ payload = ChatCompletionRequest(
+ messages = [
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "compare these"},
+ {
+ "type": "image_url",
+ "image_url": {"url": "data:image/png;base64,LEFT"},
+ },
+ {
+ "type": "image_url",
+ "image_url": {"url": "data:image/png;base64,RIGHT"},
+ },
+ ],
+ }
+ ]
+ )
+
+ _, _, image_b64 = _extract_content_parts(payload.messages)
+
+ assert image_b64 == "LEFT"
+
+ def test_later_turn_still_wins_over_a_multi_image_turn(self):
+ # Per-message first, per-thread latest: the two rules compose.
+ payload = ChatCompletionRequest(
+ messages = [
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "compare these"},
+ {
+ "type": "image_url",
+ "image_url": {"url": "data:image/png;base64,LEFT"},
+ },
+ {
+ "type": "image_url",
+ "image_url": {"url": "data:image/png;base64,RIGHT"},
+ },
+ ],
+ },
+ {"role": "assistant", "content": "two cats"},
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "and this?"},
+ {
+ "type": "image_url",
+ "image_url": {"url": "data:image/png;base64,LATER"},
+ },
+ ],
+ },
+ ]
+ )
+
+ _, _, image_b64 = _extract_content_parts(payload.messages)
+
+ assert image_b64 == "LATER"
+
+ def test_payloadless_part_does_not_claim_the_message_slot(self):
+ # An empty data URL is not an image, so the first real one still wins.
+ payload = ChatCompletionRequest(
+ messages = [
+ {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "compare these"},
+ {
+ "type": "image_url",
+ "image_url": {"url": "data:image/png;base64,"},
+ },
+ {
+ "type": "image_url",
+ "image_url": {"url": "data:image/png;base64,RIGHT"},
+ },
+ ],
+ }
+ ]
+ )
+
+ _, _, image_b64 = _extract_content_parts(payload.messages)
+
+ assert image_b64 == "RIGHT"
+
+ def test_earlier_real_image_survives_a_payloadless_opening_turn(self):
+ # The old helper latched "" here and suppressed every later image, so
+ # the request reached the model with none.
+ def turn(url):
+ return {
+ "role": "user",
+ "content": [
+ {"type": "text", "text": "what is this?"},
+ {"type": "image_url", "image_url": {"url": url}},
+ ],
+ }
+
+ payload = ChatCompletionRequest(
+ messages = [
+ turn("data:image/png;base64,"),
+ {"role": "assistant", "content": "I cannot see an image"},
+ turn("data:image/png;base64,REAL"),
+ ]
+ )
+
+ _, _, image_b64 = _extract_content_parts(payload.messages)
+
+ assert image_b64 == "REAL"
+
# =====================================================================
# _friendly_error — httpx transport failures