Skip to content

Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.

Hy4 server: KYOJIN_IMAGE_PARTS=note turns image and audio content parts into a text note

YanissAmza146ce0e5005
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 443988d..443011d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -15,6 +15,7 @@
- Hy4-flash: online speculative draft-depth gate, `HY4_SPEC2_GATE=1` (default off), mode `HY4_SPEC2_MODE=learn` (default, the measured S7 arm O), KB 2, THETA 0.6 for the `static` mode.
Base draft graph of 2 steps plus a confidence-gated third step. Fires counter per request: depth chosen per round and rounds where the gate changed the depth.
The gate's tail graph uses the same bucket helper as `HY4_GRAPH_BUCKET_REL`. See `doc/env_vars.md`.
+- Hy4-flash server: `KYOJIN_IMAGE_PARTS=reject|note` (default `reject`, the 400 of 1.9.2 is unchanged). With `note`, each image part (`image_url`, `input_image`, `image`) and audio part (`input_audio`, `audio`) of a message of any role (user, assistant, `tool`) becomes a one-line text note, so an agent that attaches a screenshot to a tool result does not stop on a 400 (issue #222); text parts keep their order, other part types are still a 400. CPU tests: `tests/test_hy4_image_parts.py`. See `doc/env_vars.md`.
## 1.9.2
diff --git a/doc/env_vars.md b/doc/env_vars.md
index 03aa454..74cf8a2 100644
--- a/doc/env_vars.md
+++ b/doc/env_vars.md
@@ -1076,6 +1076,13 @@ reset at every request, so a repeated request repeats its decisions), `static` (
(depth chosen per round; `changed` = rounds where the gate cut the chain below the shipped depth; 0 means the gate did not fire) and `fires {...}` (rounds, depth2, gate_skip, gate_run).
Rollback: unset the flag. See `tools/hy4/depth_gate.py`.
+#### `KYOJIN_IMAGE_PARTS` (default `reject`)
+
+Hy4-flash server only. The model reads text only. `reject` answers a chat request that has an image part (`image_url`, `input_image`, `image`) or an audio part
+(`input_audio`, `audio`) with a 400, as in 1.9.2. `note` replaces each such part by one line of text and serves the request: `[an image was attached; this model cannot see images]`
+or `[audio was attached; this model cannot hear audio]`. The text parts stay in order. It applies to every role, including `tool` results and assistant messages.
+Other part types (video, file) are still a 400. Any other value keeps `reject`. Read at each request. For agents that attach screenshots to tool results and must not stop.
+
#### Hy4-flash 1.9.1 levers (all on by default; `=0` restores 1.9.0)
Hy4-flash server only. `tools/hy4/serve.py` sets these in `SHIPPED_ENV` without overriding a value you set, so `HY4_<FLAG>=0 python tools/hy4/serve.py ...` is the rollback of one lever to the 1.9.0 behaviour. The levers are independent of each other. Guide: [hy4.md](hy4.md).
diff --git a/doc/hy4.md b/doc/hy4.md
index 100b1fb..a2d0477 100644
--- a/doc/hy4.md
+++ b/doc/hy4.md
@@ -48,7 +48,7 @@ curl http://127.0.0.1:8300/v1/chat/completions -H 'content-type: application/jso
Hy4-flash thinks first by default (`reasoning_effort` high): the reasoning is returned in `reasoning_content`, the answer in `content`, and a small `max_tokens` can end inside the reasoning with `content` empty. `"reasoning_effort": "no_think"` (or `"chat_template_kwargs": {"reasoning_effort": "no_think"}`) answers directly; otherwise leave room, for example `max_tokens` 1024.
-Tool calling (1.9.2): `tools` / `tool_choice` / `tool_calls` / `tool` messages work as in the OpenAI API, streamed or not (README "Tool calling"). `python tools/hy4/tool_smoke.py --base http://127.0.0.1:8300` runs the served check. A client that closes a stream stops the generation. Not supported: `n` > 1, `response_format` `json_object` / `json_schema` (400, no constrained decoding), images and audio.
+Tool calling (1.9.2): `tools` / `tool_choice` / `tool_calls` / `tool` messages work as in the OpenAI API, streamed or not (README "Tool calling"). `python tools/hy4/tool_smoke.py --base http://127.0.0.1:8300` runs the served check. A client that closes a stream stops the generation. Not supported: `n` > 1, `response_format` `json_object` / `json_schema` (400, no constrained decoding), images and audio (a 400; `KYOJIN_IMAGE_PARTS=note` turns each image or audio part into a one-line text note instead, see `doc/env_vars.md`).
The 1.9.1 levers are listed with their rollback switches in the [env_vars.md](env_vars.md) table "Hy4-flash 1.9.1 levers". After updating from 1.9.0 run `bash tools/hy4/install_strix.sh` again (the Q3_K V2 kernel needs the rebuilt MoE extension; without it the old kernel runs).
diff --git a/tests/test_hy4_image_parts.py b/tests/test_hy4_image_parts.py
new file mode 100644
index 0000000..bd4880f
--- /dev/null
+++ b/tests/test_hy4_image_parts.py
@@ -0,0 +1,89 @@
+"""CPU tests of KYOJIN_IMAGE_PARTS (tools/hy4/serve_core.py _content_text / parse_chat): `reject` (default) answers 400 for image and audio content
+parts, `note` replaces each by a one-line text note. No GPU, no model, no server.
+Run: python -m pytest tests/test_hy4_image_parts.py (needs jinja2)"""
+import os
+import sys
+import unittest
+from unittest import mock
+
+HERE = os.path.dirname(os.path.abspath(__file__))
+sys.path.insert(0, os.path.join(os.path.dirname(HERE), "tools", "hy4"))
+import serve_core as C # noqa: E402
+
+IMG = "[an image was attached; this model cannot see images]"
+AUD = "[audio was attached; this model cannot hear audio]"
+IMAGE_PARTS = ({"type": "image_url", "image_url": {"url": "data:image/png;base64,AAAA"}}, {"type": "input_image", "image_url": "x"}, {"type": "image", "source": {}})
+AUDIO_PARTS = ({"type": "input_audio", "input_audio": {"data": "AAAA", "format": "wav"}}, {"type": "audio", "audio": {}})
+
+
+def env(mode):
+ e = {k: v for k, v in os.environ.items() if k != "KYOJIN_IMAGE_PARTS"}
+ if mode is not None:
+ e["KYOJIN_IMAGE_PARTS"] = mode
+ return mock.patch.dict(os.environ, e, clear=True)
+
+
+def chat(*msgs):
+ return C.parse_chat({"messages": list(msgs)})["messages"]
+
+
+class ImageParts(unittest.TestCase):
+ def test_default_and_reject_are_a_400(self):
+ for mode in (None, "", "reject", "REJECT", "typo"):
+ with env(mode):
+ for part in IMAGE_PARTS + AUDIO_PARTS:
+ with self.assertRaises(C.RequestError, msg=f"{mode} {part}") as cm:
+ C._content_text([{"type": "text", "text": "a"}, part])
+ self.assertEqual(cm.exception.status, 400)
+ self.assertIn("only text content parts", str(cm.exception))
+
+ def test_note_replaces_each_part_in_order(self):
+ with env("note"):
+ for part in IMAGE_PARTS:
+ self.assertEqual(C._content_text([part]), IMG)
+ for part in AUDIO_PARTS:
+ self.assertEqual(C._content_text([part]), AUD)
+ mixed = [{"type": "text", "text": "look: "}, IMAGE_PARTS[0], {"type": "text", "text": " and "}, AUDIO_PARTS[0], IMAGE_PARTS[1], None, "end"]
+ self.assertEqual(C._content_text(mixed), f"look: {IMG} and {AUD}{IMG}end")
+ self.assertEqual(C._content_text("plain"), "plain")
+ self.assertEqual(C._content_text(None), "")
+
+ def test_note_mode_is_read_per_call(self):
+ part = [IMAGE_PARTS[0]]
+ with env("note"):
+ self.assertEqual(C._content_text(part), IMG)
+ with env("reject"), self.assertRaises(C.RequestError):
+ C._content_text(part)
+
+ def test_unknown_parts_still_rejected_in_note_mode(self):
+ with env("note"):
+ for bad in ([{"type": "video_url", "video_url": {}}], [{"type": "file", "file": {}}], [{"type": "text", "text": 5}], [{}], [{"type": "image_url"}, {"type": "refusal"}], 5):
+ with self.assertRaises(C.RequestError, msg=str(bad)):
+ C._content_text(bad)
+
+ def test_every_role_in_a_chat(self):
+ tool_call = {"id": "c1", "type": "function", "function": {"name": "shot", "arguments": "{}"}}
+ msgs = [{"role": "user", "content": [{"type": "text", "text": "what is this?"}, IMAGE_PARTS[0]]},
+ {"role": "assistant", "content": [{"type": "text", "text": "let me look"}, IMAGE_PARTS[1]], "tool_calls": [tool_call]},
+ {"role": "tool", "tool_call_id": "c1", "content": [IMAGE_PARTS[0], {"type": "text", "text": "screenshot"}, AUDIO_PARTS[0]]},
+ {"role": "user", "content": [{"type": "text", "text": "and now?"}]}]
+ with env("note"):
+ out = chat(*msgs)
+ self.assertEqual(out[0]["content"], f"what is this?{IMG}")
+ self.assertEqual(out[1]["content"], f"let me look{IMG}")
+ self.assertEqual(out[1]["tool_calls"][0]["function"]["name"], "shot")
+ self.assertEqual(out[2], {"role": "tool", "content": f"{IMG}screenshot{AUD}", "tool_call_id": "c1"})
+ self.assertEqual(out[3]["content"], "and now?")
+ with env(None): # default: the same chat is a 400, whichever role carries the part
+ for m in msgs[:3]:
+ with self.assertRaises(C.RequestError, msg=m["role"]):
+ chat({"role": "user", "content": "q"}, m, {"role": "user", "content": "x"})
+
+ def test_system_message_with_image_part(self):
+ with env("note"):
+ out = chat({"role": "system", "content": [{"type": "text", "text": "rules "}, IMAGE_PARTS[0]]}, {"role": "user", "content": "hi"})
+ self.assertEqual(out[0], {"role": "system", "content": f"rules {IMG}"})
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tools/hy4/serve_core.py b/tools/hy4/serve_core.py
index 9450dd3..eb4f378 100644
--- a/tools/hy4/serve_core.py
+++ b/tools/hy4/serve_core.py
@@ -276,10 +276,20 @@ def parse_effort(body: dict) -> str:
TEXT_PARTS = ("text", "input_text", "output_text")
+IMAGE_PARTS = ("image_url", "input_image", "image")
+AUDIO_PARTS = ("input_audio", "audio")
+IMAGE_NOTE = "[an image was attached; this model cannot see images]"
+AUDIO_NOTE = "[audio was attached; this model cannot hear audio]"
+
+
+def _image_parts_note() -> bool:
+ """KYOJIN_IMAGE_PARTS=note: image / audio parts become a short text note. Anything else (unset, `reject`, a typo) keeps the 400. Read per call."""
+ return os.environ.get("KYOJIN_IMAGE_PARTS", "").strip().lower() == "note"
def _content_text(c: Any) -> str:
- """OpenAI content (a string, null, or a list of {type: text} parts) -> text. Null parts and null / empty text are empty text."""
+ """OpenAI content (a string, null, or a list of {type: text} parts) -> text. Null parts and null / empty text are empty text.
+ Image and audio parts are a 400, or with KYOJIN_IMAGE_PARTS=note one short text note each (every role: user, assistant, tool)."""
if c is None:
return ""
if isinstance(c, str):
@@ -293,6 +303,8 @@ def _content_text(c: Any) -> str:
out.append(p)
elif isinstance(p, dict) and (p.get("type") in TEXT_PARTS or (p.get("type") is None and "text" in p)) and (p.get("text") is None or isinstance(p["text"], str)):
out.append(p.get("text") or "")
+ elif isinstance(p, dict) and p.get("type") in IMAGE_PARTS + AUDIO_PARTS and _image_parts_note():
+ out.append(IMAGE_NOTE if p["type"] in IMAGE_PARTS else AUDIO_NOTE)
else:
raise RequestError("only text content parts are supported (no images / audio)")
return "".join(out)