Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

serve: a tool's picture the server cannot read becomes a note, not a 400

sergiywith3709c15f1d68
diff --git a/serve/server.py b/serve/server.py
index 39685f88e..fb5d12d0c 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -2269,10 +2269,40 @@ class Service:
prompt, plain = unmark_think_literals(prompt)
return self.tok.encode(prompt, parse_special=True, plain=plain)
+ def _note_unreadable_tool_images(self, messages):
+ """A picture a tool returned (Claude Code's Read of an image file) that this server cannot read - it has no
+ image encoder, or the encoder refuses that picture - becomes a short note in its place instead of a 400.
+ Clients resend the whole history every turn, so one unreadable tool picture would fail every later request of
+ the conversation; a picture the user sends keeps the 400. A picture that encodes stays in the cache, so the
+ prompt's own encode below finds it."""
+ fetched = {} # a URL's bytes, so prepare() does not download the same picture again
+ for m in messages:
+ content = m.get("content")
+ if m.get("role") != "tool" or not isinstance(content, list):
+ continue
+ for n, item in enumerate(content):
+ if not isinstance(item, dict) or item.get("type") != "image":
+ continue
+ why = "this server has no image encoder" if self.vision is None else None
+ if why is None:
+ src = item["source"]
+ try:
+ if src.startswith(("http://", "https://")):
+ src = fetched[item["source"]] = Vision.download(src) # outside the FIFO, as in prepare()
+ with self.fifo: # the encoder takes its turn with the requests (see below)
+ self.vision.encode(src)
+ except (ValueError, OSError) as e:
+ why = str(e)
+ if why is not None:
+ print(f"[strata] a picture a tool returned was left out of the prompt: {why}", flush=True)
+ content[n] = {"type": "text", "text": f"[image omitted: {why}]"}
+ return fetched
+
def prepare(self, messages, tools, kwargs, max_new=None, force=None):
"""-> (ids, thinking, max_new). An unset or non-positive max_new (some clients send -1) means "unlimited":
the rest of the context. `force` (forced_call): without thinking the reply starts with it, so it ends the
prompt; with thinking, Service.run writes it once the thinking is over."""
+ fetched = self._note_unreadable_tool_images(messages)
ids = self.encode_prompt(messages, tools, kwargs)
if force and kwargs.get("enable_thinking", True) is False:
ids = ids + self.tok.encode(force, parse_special=True)
@@ -2286,7 +2316,8 @@ class Service:
start = self.tok.encode(VISION_START, parse_special=True)[0]
# An image URL is downloaded first, outside the FIFO: under it, a slow server held every other request
# for as long as urlopen waited.
- images = [Vision.download(src) if src.startswith(("http://", "https://")) else src for src in images]
+ images = [fetched[src] if src in fetched else Vision.download(src) if src.startswith(("http://", "https://"))
+ else src for src in images]
# Encode only while the engine is idle: the engine and the image encoder (a separate process) must not
# run on the GPU at the same time - an encode during a running request left that request stuck at
# "reading the prompt" with CPU and GPU busy, for good (reproduced). So encoding takes its turn in the
diff --git a/serve/test_server.py b/serve/test_server.py
index 2b6bc6749..e55323e03 100644
--- a/serve/test_server.py
+++ b/serve/test_server.py
@@ -268,6 +268,51 @@ class ImageMarkers(unittest.TestCase):
self.assertEqual("<|vision_start|>" in inside, not isinstance(result, str))
self.assertEqual("<|vision_start|>" in prompt.split("</tool_response>", 1)[1], bool(extra))
svc.embeddings.path.unlink(missing_ok=True)
+ # a tool's picture the server cannot read is a note, not a 400 (the client resends it every turn); the user's
+ # own picture still fails
+ class Refusing(self.FakeVision):
+ def encode(self, source):
+ if "bad" in str(source):
+ raise ValueError("this image format needs Pillow")
+ return super().encode(source)
+ good = {"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "AAAA"}}
+ bad = {"type": "image", "source": {"type": "base64", "media_type": "image/webp", "data": "badd"}}
+ tool_msgs = [{"role": "user", "content": "look"}, call,
+ {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "t1", "content": [bad, good]}]}]
+ with tempfile.TemporaryDirectory() as d:
+ for vision, n, note in ((None, 0, "[image omitted: this server has no image encoder]"),
+ (Refusing(d), 3, "[image omitted: this image format needs Pillow]")):
+ with self.subTest(vision=vision is not None):
+ svc = Service(MockEngine(tok, "ok", max_context=CTX), tok,
+ ChatTemplate(ROOT / "serve/chat_template.jinja"), vision=vision)
+ m, _, _ = anthropic_to_messages({"messages": tool_msgs})
+ ids, _, _ = svc.prepare(m, None, {})
+ self.assertEqual(ids.count(pad), n)
+ self.assertIn(note, tok.decode(ids))
+ if svc.embeddings.path:
+ svc.embeddings.path.unlink(missing_ok=True)
+ # a tool's picture at a URL is downloaded once (the check and the prompt share the bytes), and one that
+ # cannot be fetched is a note too
+ import serve.server as server
+ url = {"type": "image", "source": {"type": "url", "url": "http://x/ok.png"}}
+ url_msgs = [{"role": "user", "content": "look"}, call,
+ {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "t1", "content": [url]}]}]
+ svc = Service(MockEngine(tok, "ok", max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja"),
+ vision=self.FakeVision(d))
+ m, _, _ = anthropic_to_messages({"messages": url_msgs})
+ with mock.patch.object(server.Vision, "download", return_value=b"the picture") as dl:
+ ids, _, _ = svc.prepare(m, None, {})
+ self.assertEqual((dl.call_count, ids.count(pad)), (1, 3))
+ svc.embeddings.path.unlink(missing_ok=True)
+ m, _, _ = anthropic_to_messages({"messages": url_msgs})
+ with mock.patch.object(server.Vision, "download", side_effect=ValueError("the image URL could not be read")):
+ ids, _, _ = svc.prepare(m, None, {})
+ self.assertEqual(ids.count(pad), 0)
+ self.assertIn("[image omitted: the image URL could not be read]", tok.decode(ids))
+ svc = Service(MockEngine(tok, "ok", max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja"))
+ m, _, _ = anthropic_to_messages({"messages": [{"role": "user", "content": [good]}]})
+ with self.assertRaises(ValueError):
+ svc.prepare(m, None, {})
# a text-only tool result renders exactly as before: one string
msgs = [{"role": "user", "content": "q"}, call,
{"role": "user", "content": [{"type": "tool_result", "tool_use_id": "t1", "content": [{"type": "text", "text": "r"}]}]}]