The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(codex): inline SVG images are rasterized to PNG before the Responses send
teknium13dad039a7461
diff --git a/agent/codex_responses_adapter.py b/agent/codex_responses_adapter.pyindex 46c9cf6336e..dda5da3134e 100644--- a/agent/codex_responses_adapter.py+++ b/agent/codex_responses_adapter.py@@ -227,7 +227,8 @@ def _input_image_part(part: Dict[str, Any], role: str = "user", *, keep_empty_ur"""Responses image part from a chat/Responses image part (``image_url`` may be a str or``{url, detail}``). Assistant → text placeholder (an assistant ``input_image`` 400s everyreplay); user → ``input_image``, None for an empty url unless ``keep_empty_url``; an inline- image of an unsupported subtype (e.g. ``image/svg+xml``) → text placeholder."""+ SVG is rasterized to PNG when a rasterizer is installed, any other unsupported inline+ subtype (or an SVG with no rasterizer) → text placeholder."""if role == "assistant":return {"type": "output_text", "text": _ASSISTANT_IMAGE_PLACEHOLDER}url, detail = part.get("image_url"), part.get("detail")@@ -236,7 +237,15 @@ def _input_image_part(part: Dict[str, Any], role: str = "user", *, keep_empty_urif not _nonempty_str(url) and not keep_empty_url:return Noneurl = str(url or "")- if (mime := _unsupported_inline_image_subtype(url)) is not None:+ mime = _unsupported_inline_image_subtype(url)+ if mime == "image/svg+xml":+ # Rasterize so the model still sees the drawing; the placeholder is the fallback only+ # when no rasterizer (cairosvg / svglib / rsvg-convert / inkscape) is available.+ from tools.vision_tools_image_prep import rasterize_svg_data_url+ png_url = rasterize_svg_data_url(url)+ if png_url is not None:+ url, mime = png_url, None+ if mime is not None:return {"type": "input_text", "text": f"[image omitted: {mime} is not a supported image format]"}image_part: Dict[str, Any] = {"type": "input_image", "image_url": url}if _nonblank(detail):diff --git a/tests/agent/test_codex_responses_adapter.py b/tests/agent/test_codex_responses_adapter.pyindex 1bec7ce3f7b..04cbf2a474a 100644--- a/tests/agent/test_codex_responses_adapter.py+++ b/tests/agent/test_codex_responses_adapter.py@@ -97,10 +97,16 @@ _SVG_DATA_URL = "data:image/svg+xml;base64,PHN2Zy8+"_PNG_DATA_URL = "data:image/png;base64,iVBORw0KGgo="-def test_unsupported_inline_image_downgrades_to_text_in_message_and_tool_output():+def _no_rasterizer(monkeypatch):+ import tools.vision_tools_image_prep as prep+ monkeypatch.setattr(prep, "_rasterize_svg_to_png", lambda svg_path, out_path: False)+++def test_unsupported_inline_image_downgrades_to_text_in_message_and_tool_output(monkeypatch):"""#29711: a data:image/svg+xml part 400s the whole Codex request ('does not represent a validimage') on every replay. Both carriers — user message content and the persisted vision_analyzefunction_call_output — must send a text placeholder while the valid PNG still goes as input_image."""+ _no_rasterizer(monkeypatch)messages = [{"role": "user", "content": [{"type": "image_url", "image_url": {"url": _PNG_DATA_URL, "detail": "high"}},@@ -122,9 +128,25 @@ def test_unsupported_inline_image_downgrades_to_text_in_message_and_tool_output(assert "image/svg+xml" in tool_output["output"][1]["text"]-def test_preflight_downgrades_unsupported_inline_image_but_keeps_remote_urls():+def test_inline_svg_is_rasterized_to_png_when_a_rasterizer_exists(monkeypatch):+ """#29711 follow-up: with a rasterizer installed the model still sees the drawing — the SVG part+ goes out as a PNG input_image instead of the text placeholder; the SVG itself is never sent."""+ import tools.vision_tools_image_prep as prep++ def fake_rasterize(svg_path, out_path):+ assert svg_path.read_bytes() == b"<svg/>"+ out_path.write_bytes(b"\x89PNG\r\n\x1a\n")+ return True+ monkeypatch.setattr(prep, "_rasterize_svg_to_png", fake_rasterize)+ parts = _chat_content_to_responses_parts(+ [{"type": "image_url", "image_url": {"url": _SVG_DATA_URL, "detail": "high"}}], role="user")+ assert parts == [{"type": "input_image", "image_url": "data:image/png;base64,iVBORw0KGgo=", "detail": "high"}]+++def test_preflight_downgrades_unsupported_inline_image_but_keeps_remote_urls(monkeypatch):"""The preflight validator is the last seam before the wire: an svg data URL in alreadyResponses-shaped input becomes text; https URLs are the provider's to validate and pass through."""+ _no_rasterizer(monkeypatch)items = _preflight_codex_input_items([{"role": "user", "content": [{"type": "input_image", "image_url": _SVG_DATA_URL},diff --git a/tools/vision_tools_image_prep.py b/tools/vision_tools_image_prep.pyindex dac11878c6b..153a9c954c0 100644--- a/tools/vision_tools_image_prep.py+++ b/tools/vision_tools_image_prep.py@@ -141,6 +141,34 @@ def _rasterize_svg_to_png(svg_path: Path, out_path: Path) -> bool:return False+def rasterize_svg_data_url(url: str) -> Optional[str]:+ """``data:image/svg+xml[;base64],...`` → ``data:image/png;base64,...`` through the same+ soft-dependency rasterizers vision_analyze uses; None when the payload does not decode or no+ rasterizer is available. Request-path callers decide the fallback (Responses backends 400 on+ SVG source, so the caller must never forward the SVG itself)."""+ import base64+ from contextlib import suppress+ from urllib.parse import unquote+ header, _, payload = url.partition(",")+ try:+ raw = base64.b64decode(payload) if ";base64" in header.lower() else unquote(payload).encode()+ except Exception:+ return None+ out_dir = get_hermes_dir("cache/vision", "temp_vision_images")+ out_dir.mkdir(parents=True, exist_ok=True)+ stem = out_dir / f"inline_{uuid.uuid4()}"+ svg_path, png_path = stem.with_suffix(".svg"), stem.with_suffix(".png")+ try:+ svg_path.write_bytes(raw)+ if not _rasterize_svg_to_png(svg_path, png_path):+ return None+ return "data:image/png;base64," + base64.b64encode(png_path.read_bytes()).decode("ascii")+ finally:+ for path in (svg_path, png_path):+ with suppress(OSError):+ path.unlink()++def _normalize_to_supported_image(image_path: Path, detected_mime: str) -> tuple[Optional[Path], Optional[str], Optional[str]]:"""Ensure an image is in a provider-supported format. Returns ``(path, mime, error)``: the inputdiff --git a/website/docs/user-guide/features/vision.md b/website/docs/user-guide/features/vision.mdindex 744fce67055..4e777c42e34 100644--- a/website/docs/user-guide/features/vision.md+++ b/website/docs/user-guide/features/vision.md@@ -223,6 +223,10 @@ The `vision_analyze` tool itself follows the same routing. When the active mainFor text-only main models (or providers whose tool-result channel doesn't carry images), `vision_analyze` falls back to the legacy path: it asks the configured auxiliary vision model to describe the image and returns the description as plain text. Either way the calling tool signature is the same — the tool decides which path to take at runtime based on the active model.+### SVG and other non-raster images on Responses backends++Responses-style backends (for example `openai-codex`) accept only inline JPEG, PNG, GIF and WebP; any other `data:image/*` part makes them reject the **whole** request, and because the part stays in history every later turn fails the same way. Hermes handles this at the send layer: an inline **SVG** is rasterized to PNG when a rasterizer is installed (`cairosvg`, `svglib`+`reportlab`, `rsvg-convert`, or `inkscape` — the same soft dependencies `vision_analyze` uses), so the model still sees the drawing. Without a rasterizer, an SVG — and any other unsupported inline format such as BMP or TIFF — is replaced by a short text placeholder (`[image omitted: image/svg+xml is not a supported image format]`) while the valid images in the same message are still sent.+### Native embeds ride the session: `vision.embed_target_bytes` and `vision.max_calls_per_image`A native `vision_analyze` result bakes the image into the tool result, and that result is re-sent on every later API call of the session. Two `config.yaml` keys bound the recurring cost: