Skip to content

Studio: convert pasted images when tools are turned on - #10094

Merged
Imagineer99 merged 6 commits into
unslothai:mainfrom
NilayYadav:passthrough-image-reencode
Sep 7, 2026
Merged

Imagineer99 merged 6 commits into
unslothai:mainfrom
NilayYadav:passthrough-image-reencode

Conversation

@NilayYadav

@NilayYadav NilayYadav commented Aug 31, 2026 •

Copy link
Copy Markdown
Collaborator

Studio converts images to PNG before sending them, because llama-server can only read a few image formats. That conversion was missing on the path used when tools or a response format are set, so the image went out in whatever format it arrived in and the request failed.

The odd part for a user is that turning tools on is what breaks it, which is hard to connect to the screenshot you just pasted. WebP is the common case, since that is what macOS screenshots and most browsers produce.

This runs the same conversion on both paths. Remote image links are still passed through untouched, and images sent through the older image_base64 field are not converted twice.

Worth saying plainly: Claude Code sends its images down the path that already converted them, so the gap is the OpenAI compatible endpoints with tools switched on. This is a consistency fix, not a widespread outage.

How to check: send a WebP image with and without tools enabled and compare what reaches llama-server.

@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the gap in studio/backend/routes/inference.py, where _openai_messages_for_passthrough leaves content-part data URLs alone while the GGUF chat builder re-encodes them to PNG. Will get this reviewed.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

https://github.com/unslothai/unsloth/blob/ea36bb7586dad817944440f2069fcd0c0bc45df6/studio/backend/routes/inference.py#L27055
P2 Badge Preserve the requested image detail during conversion

When a data-URL image specifies detail: "low", "high", or "original", replacing the entire image_url object discards that setting. Because this commit now runs the normalizer on the tools/response-format passthrough path, requests that previously forwarded an explicit detail level silently fall back to llama-server's default, potentially changing image token usage or visual accuracy. Rewrite only the url member while retaining the other fields.

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 22779f4f5a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

Before/after on a live Studio, plus a note that changes how I'd describe the impact.

The condition that decides this

llama.cpp b10715 does not decode WebP with stb_image. It shells out to ffmpeg:

#ifdef MTMD_VIDEO
    // stb_image does not support webp; decode it with ffmpeg as a single frame
    if (!result && is_webp_file(buf, len)) {
        result = decode_webp_with_ffmpeg(ctx, buf, len, placeholder, opt.video_params);

Unsloth does not install ffmpeg (PyAV bundles the libraries, not the CLI llama-server resolves). So with ffmpeg on PATH the WebP turn already works and this PR changes nothing observable; without it, the turn fails. Both runs are below.

Live before/after, ffmpeg absent on both sides

Two isolated installs, merge base 2da377132 vs head 22779f4f5, same gemma-4-E2B-it-UD-Q4_K_XL, same llama.cpp b10715-92cedc867 on both sides, both launched with PATH=/usr/bin:/bin:/usr/sbin:/sbin. Same WebP image sent twice per side: once with tools off (the control), once with tools on.

fact BEFORE 2da377132 AFTER 22779f4f5
control_tools_off_http_status 200 200
control_tools_off_reads_word true true
tools_on_http_status 400 200
tools_on_reads_word false true
tools_on_finish_reason — length

BEFORE returns llama-server's own refusal:

{"error":{"code":400,"message":"Failed to load image or audio file","type":"invalid_request_error"}}

The control is the part that makes this comparable: tools-off already went through _openai_messages_for_gguf_chat, which converted to PNG even on merge base, so each side proves on its own server that images decode and only the WebP-with-tools turn fails.

before/after

BEFORE: a red /chat/completions row carrying the 400, above a green tools-off row answering UNSLOTH. AFTER: both rows green.

With ffmpeg on PATH

Same two installs, unmodified PATH: tools_on_http_status 200 on both sides, both read the word. A null result, recorded rather than dropped. So the user-visible failure needs ffmpeg to be missing; what the PR always changes is that the two endpoints stop disagreeing, and the WebP path no longer depends on an ffmpeg subprocess.

Request-body A/B on Actions

Building the real passthrough body for a WebP + tools-on turn, identical workflow and probe, only the implementation differs:

  • merge base — run 33513282080: llama_server_receives_format: WEBP, stb_image_can_decode: false — fails at the intended assertion
  • head — run 33513287144: PNG, stb_image_can_decode: true, detail_forwarded: "high" — passes

Review fixes in this PR

  • detail is no longer dropped: only the url member is rewritten, so detail: "high" survives the conversion.
  • The re-encode is scoped to llama-server bodies. It was also reaching the safetensors/MLX client-tools path, where a payloadless data: URL — input extraction deliberately ignores with a warning — turned into a 400 on a path that used to answer.
  • Removed a duplicate test import that was failing Lint CI.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-07T16:33:55.282807Z cf44108 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 22779f4f5a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Imagineer99 Imagineer99 self-assigned this Sep 7, 2026
@Imagineer99

Copy link
Copy Markdown
Member

The WebP fix addresses a real gap in the tools and response-format paths. Review coverage includes 902 passing existing tests plus A/B checks through the request handlers and llama.cpp’s image decoder.

One improvement before merge: preserve 16-bit grayscale PNG precision. The new conversion clips these images to nearly all white. The A/B decoder comparison shows 1 white pixel before versus 255 after in a 256-pixel grayscale ramp. A sandbox fix passes across Windows and current and older Pillow versions. Native macOS, browser clipboard flows and GPU inference remain unverified.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

Good catch on the 16-bit grayscale clipping — reproduced and fixed in 8ce771955.

convert("RGB") reads a 16-bit source as 8-bit and clips everything above 255. Measured on a 256-px 16-bit grayscale ramp, against what stb_image yields when llama-server reads the same file itself:

distinct levels pure-white pixels
correct (stb_image v >> 8) 256 1
before the fix 2 255
after the fix 256 1

Worth noting this was not confined to the paths this PR adds: _image_bytes_to_png_b64 is shared, so the GGUF and Anthropic paths were already clipping 16-bit images before this PR. The fix is in that helper, so all three paths are covered.

One thing the fix needed beyond the straightforward scale: I;16B and I;16L raise ValueError: point operation not supported for this mode on both pinned Pillow versions, and the caller turns any exception into a 400 — so scaling without normalising the mode first would have been worse than the clipping, converting a degraded image into a failed request. PNG cannot carry those tags, but a 16-bit TIFF reopens as I;16B. They are normalised to I before scaling.

Verified on the two pinned Pillow versions (pillow==12.3.0 for py>=3.10, pillow==11.3.0 below it) across I, F, I;16, I;16B, I;16L — all restore the full 256-level ramp — and 8-bit RGB/RGBA/L/LA/CMYK are byte-for-byte unaffected. Three regression tests added; the two that matter fail without the fix. 781 tests pass across the passthrough, GGUF-image, vision, Anthropic and image-generation suites.

Not verified: native macOS paste and browser clipboard flows, and GPU inference.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

Closing the platform gap I left open on the 16-bit fix: it is now measured on Windows rather than inferred.

Matrix over the two pinned Pillow versions on all three platforms, importing the real _image_bytes_to_png_b64 and asserting against what stb_image yields for the same file (v >> 8) — run 34143072571, 6/6 green:

pillow 12.3.0 pillow 11.3.0
windows-latest pass pass
ubuntu-latest pass pass
macos-14 pass pass

Each job asserts the same four things: the 256-px 16-bit ramp keeps distinct=256 white=1; a 16-bit TIFF that opens as I;16B scales instead of raising (it rejects point(), which the caller would turn into a 400); and 8-bit RGB/RGBA/L come through byte-identical.

Separately, a correction to my own PR description rather than to the code. It says WebP is "what macOS screenshots ... produce", and that is not right: screencapture -h reports default is png, offers pdf, jpg, tiff as the alternatives, and cannot emit WebP at all. The WebP sources that actually matter here are browser copy/drag and Claude Code. I will fix the wording; the change itself is unaffected, since it converts on format rather than on where the image came from.

Still unverified: native macOS paste and browser clipboard flows. GPU is not in this path — the conversion is Pillow in the Studio process and the decode is llama-server's CPU stb_image/mtmd, so the bytes reaching the GPU are identical on both sides of the diff.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 7, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Sep 7, 2026
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

1 similar comment
@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: cf4410875b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Imagineer99 Imagineer99 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to merge. Playwright A/B checks confirm the WebP fix and grayscale preservation, with 68 passes, no failures and seven blocked clipboard cases. Trusted paste passes in Chrome and Edge, and current CI is green. Coverage uses a focused Studio code fixture; full Studio and native Safari remain unverified.

For the A/B comparison from 2da3771 to cf44108, WebP with tools reaches the decoder as WebP and fails before, then reaches it as PNG and succeeds after. The current head retains all 256 grayscale levels in plain chat, tools and JSON response format cases.

@Imagineer99
Imagineer99 merged commit 502c4d2 into unslothai:main Sep 7, 2026
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants