Skip to content

Studio: the Images pill makes images and videos in chat with the loaded local models - #14110

Merged
danielhanchen merged 43 commits into
unslothai:mainfrom
Etherll:studio-g2-images-pill-local-media
Oct 19, 2026
Merged

danielhanchen merged 43 commits into
unslothai:mainfrom
Etherll:studio-g2-images-pill-local-media

Conversation

@Etherll

@Etherll Etherll commented Oct 18, 2026 •

Copy link
Copy Markdown
Collaborator

Includes #14078 (Studio: image and video generation follow each model's defaults and prompting tips, and /v1/videos takes Unsloth Studio controls); review the last 10 commits (bbee492..HEAD). The own commits are bbee492, 809dc77, c713699, 282a66e and d14fb5b, plus a pre-commit.ci formatting commit (1d466b6); the merges only bring in #14078's latest heads, main (the one conflict, the bundle budget, keeps both raises) and that formatting commit.

Summary

In chat, the Images pill only worked with a hosted provider's built-in image_generation. A local chat model, or any model that calls Unsloth Studio tools, could not use the image or video model loaded in Unsloth Studio. Now the Images pill and the + menu's Images item show for these models whenever a text-to-image or video model is loaded. With the pill on, a turn offers generate_image and/or generate_video, and the result shows inline in chat as an image or a playable video.

Neither tool loads, unloads or switches a model. Hosted image models keep their own image_generation as before.

Changes

Backend:

  • generate_image (core/inference/generate_image_tool.py) runs the Images route's local path on the server loop, bound to the chat's account. So the image lands in that account's gallery and goes through the same access checks as POST /api/inference/images/generate. The route is split in place: generate_diffusion_image keeps its provider branch and then calls _generate_diffusion_image_core(request), with the body unchanged. The tool builds the request only from the arguments the model sent, so omitted steps and guidance follow the loaded model's defaults (Studio: image and video generation follow each model's defaults and prompting tips, and /v1/videos takes Unsloth Studio controls #14078). negative_prompt is offered only when the loaded model supports it. The size is snapped to a multiple of 16 between 256 and 2752.
  • generate_video (core/inference/generate_video_tool.py) goes through the /v1/videos job path (_create_openai_video) with no model named, so the auto-switch returns at once and nothing loads. It polls the job and copies the finished .mp4 into the chat's files. Clip lengths and sizes in its description come from the loaded model's defaults.
  • Both tools append the model's prompt_hint (Studio: image and video generation follow each model's defaults and prompting tips, and /v1/videos takes Unsloth Studio controls #14078) to their description. They return Error: ... results for:
    • nothing loaded, or an edit-only image model;
    • another image or video already rendering;
    • another account on the GPU;
    • Stop;
    • a timeout. Image generation is stopped on the GPU too. A video is left rendering, and the result says to find it on the Video page.
  • The tools are offered only when the request names them in enabled_tools and a model the account may use is loaded. The server loop is captured lazily in _select_request_tools the first time a request offers them. There is no main.py or lifespan change.
  • Keep-warm counts each chat-tool call exactly once, in the tool wrapper. The HTTP route is still counted only by the middleware.
  • One MEDIA_CHAT_TOOLS set (core/inference/media_chat_tools.py) feeds:
    • the file and image sentinels;
    • _ALWAYS_SAFE_TOOLS and the Anthropic unprompted-safe list (the Anthropic path never selects these tools);
    • the dedupe exemption in the controller, llama_cpp.py and safetensors_agentic.py. Two identical calls are two images.

Frontend:

  • One rule decides whether the pill shows: the provider's built-in image generation, or a local tool, meaning the model calls Unsloth Studio tools and an image or video model is loaded. It applies in the thread composer, the compare composer and the + menu.
  • Readiness comes from the image and video status endpoints. It is re-read on mount, when a model load settles, when an image or video model is ejected, and on window focus. Another account's hidden model (yours: false) and an edit-only image model don't count.
  • Like RAG, the local pill takes composer room only while it is on, so the pill-count layout is unchanged while it is off.
  • The stored Images choice is limited by what the model can do, not by whether an image model is loaded right now. Loading and unloading an image model doesn't drop a chat's Images setting. Images stays one setting, as Search and Code are: turning it on for a local model also leaves it on for a hosted image model.
  • Gemini's image mode still turns off Code only for hosted image models.
  • The generate_image card fetches the PNG from the chat's sandbox. It has Download and no Edit, because Edit needs an OpenAI image-generation call id. The new generate_video card plays the .mp4 inline, with Download.
  • The frontend MEDIA_CHAT_TOOLS set feeds SANDBOX_FILE_TOOLS, IMAGE_SENTINEL_TOOLS, the tool-fold exemptions and the response-details labels. A test keeps it equal to the backend set.
  • The bundle budget goes up by 4,975 B raw and 1,658 B transfer (chunk count unchanged at 95). That is the image and video cards in the eager tool UI registry, plus the composer and adapter gates in the chat chunk. The measurement is in check-bundle-budget.ts.

Testing

  • New: test_generate_image_tool.py, test_generate_video_tool.py, studio/frontend/tests/chat-images-local-tool.test.ts and tests/studio/test_generate_image_frontend_contract.py. Extended: test_llama_cpp_tool_loop.py, test_nudge_tool_calls_wiring.py and image-sentinel-tools.test.ts. They cover:
    • which tools are offered, including a managed account that can't see the model;
    • negative_prompt per family;
    • a managed account's chat image landing in its own gallery and passing require_media_generation_access;
    • Stop and timeout stopping the engine;
    • GpuBusyForAnotherAccountError becoming an Error: string;
    • omitted steps and guidance resolving per model;
    • keep-warm counted once per call;
    • for video, model=None never switching or loading, the busy 409 mapping, and a timeout leaving the job running;
    • hosted vs local routing and the pill count;
    • the frontend and backend sets staying in step.
  • The new and touched backend tests, plus Studio: image and video generation follow each model's defaults and prompting tips, and /v1/videos takes Unsloth Studio controls #14078's image-defaults and /v1/videos controls tests on the merged tree: 106 passed. Frontend: typecheck clean, the targeted node tests pass (30), and an earlier full npm test run had 12,539 passing. The 2 failures there were load timeouts that pass when run alone. bundle:check is within the new budget. ESLint adds nothing new on the changed files.
  • End to end on one B200, with a resident Z-Image-Turbo GGUF image model and then a Wan2.2-TI2V-5B video model, both loaded under the default Off. The chat model was Qwen3-4B on CPU, served by llama-server and added as a Custom provider that uses Unsloth Studio tools:
    • From the Images pill, the model called generate_image. It rendered at 1024x1024 with 8 steps, Z-Image-Turbo's own default, and the image showed inline.
    • The model called generate_video with no model named, nothing switched or loaded, and a 2 s clip played inline.
    • In-process GGUF chat can't stay loaded beside an image or video model on this base: the GPU arbiter evicts the chat model for diffusion even with gpu_layers=0. Media co-residency comes with the multi-model loading PR. The in-process path is covered by the tests above.

UI before and after screenshots (both composers, the + menu, and the image and video cards) follow in a comment.

UI before / after

With a local GGUF chat model that uses Studio tools (unsloth/Qwen3-1.7B-GGUF Q4_K_M) and a text-to-image model loaded, the chat composer now shows an "Images" pill next to Tools; before this PR it never appeared for local models. BEFORE is the head of #14078 (this PR's base), AFTER is this PR, two separate installs with the same two models loaded.

Chat composer pills before and after

…rompting tips, and /v1/videos takes Unsloth Studio controls

- /api/inference/images/generate resolves omitted steps and guidance from the
  loaded model (diffusers generation_defaults, else the family table for
  sd.cpp and MLX), per retry attempt, and pins the model it read.
  /v1/images shares the same helper.
- MCP generate_image refuses workflows the engine cannot run (reference or
  edit or outpaint without init_image, outpaint without mask_image,
  upscale <= 1) before any upload, takes seeds/prompts batches, and
  studio_status image carries workflows, generation_defaults,
  dimension_multiple, max_condition_images and prompt_hint.
- Image and video families gain an optional prompt_hint, exposed in every
  engine's status, shown as the Prompt field's info hint on the Images and
  Video pages, plus an off-by-default image-prompting bundled skill.
- /v1/videos accepts seed, negative_prompt, steps, guidance, num_frames,
  fps, last_frame, reference_images and reference_audios (data URLs or
  Unsloth Studio audio ids), keeps the family checks before any model
  switch, and draws, stores and echoes a seed when none is sent. Plain
  OpenAI-shaped requests are unchanged apart from the added seed.
- MCP generate_video exposes those controls, takes seconds as a number or
  a string, and echoes the seed on the job and in get_job.
…g a load

- MiniMax-H3 writes at a fixed 24 fps, so /v1/videos refuses another fps instead of misreporting the clip length.
- last_frame and each reference image take the same 32 MB image input_reference takes.
- The MCP generate_video budget matches what one /v1/videos request carries (32 MB of images, body cap checked before the call).
- Stored reference audio is sized before it is read; multipart reference lists keep their wire order.
- /images/generate with omitted steps or guidance during another client's load retries on the new model's defaults.
With auto-switch on or a managed account, the route reads generation_snapshot(), which carries no family key, so the fixed-fps check read None and let fps through. Read the family off the pinned state instead.
…uploading it

/v1/videos takes at most 24 MB per reference audio, but audio_ref uploads up to 200 MB first, so a large local file was read, sent and stored only to be refused.
…ed local models

A chat model that calls Unsloth Studio tools can now render with the image or
video model already loaded in Unsloth Studio. The Images pill and + menu item
show for it, and a turn with the pill on offers generate_image and/or
generate_video.

- generate_image runs the Images route's local core on the server loop, bound
  to the chat's account, so the image lands in that account's gallery and goes
  through the same access checks. Omitted steps and guidance follow the loaded
  model's defaults.
- generate_video goes through the /v1/videos job path with no model named, so
  nothing loads or switches; the tool polls the job and copies the .mp4 into the
  chat's files.
- Neither tool loads or unloads a model. Busy, stopped, timed-out and
  other-account cases come back as "Error: ..." results.
- One MEDIA_CHAT_TOOLS set (backend and frontend) feeds the sentinel,
  always-safe, dedupe and fold registries.
…combinations before uploading audio

- Each reference audio string now fits a 24 MB clip as a base64 data URL with its header, the size the upload and audio id paths accept.
- reference_images and reference_audios say one request body carries about 32 MB of inline media, and that larger or several clips go as Unsloth Studio audio ids.
- MCP generate_video refuses seconds with num_frames, last_frame with references and first_frame with reference_images before it uploads any audio.
…econds take fractions

- Before an automatic switch, each reference audio is decoded the way the engine would, so an unreadable clip is a 400 on reference_audios instead of a failure after the requested model loaded.
- MCP generate_video's seconds accepts a fractional number such as 2.5, as /v1/videos does.
More than 9 reference_images (or 3 reference_audios) in a multipart body were each base64-encoded before the request model refused the count, so ten modest files could grow past 50 MB in memory.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 18, 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 ⚠️ Failed 2026-10-18T13:42:54.490559Z edab958 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.

@Etherll

Etherll commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator Author

UI evidence

Both sides ran the same steps on one B200 (GPU 2). Unsloth Studio was started fresh from each tree with update checks off. The image model was Z-Image-Turbo GGUF (Q4_K_M), loaded under the default Off. The chat model was Qwen3-4B (UD-Q4_K_XL) on CPU, served by llama-server with 8 threads and added as a Custom provider that uses Unsloth Studio tools. Before is #14078's head (c376d4b50a); after is this PR (21266e1a5d).

Thread composer and + menu. Before, there is no Images pill or menu item for this model. After, both show.

Thread composer and + menu

Compare composer (both panes on the same model):

Compare composer

Compare + menu

The same request, "Make an image of a lighthouse at dusk, painted in oils.", with the Images pill on in the after run. Before, the model can only describe a painting. After, it calls generate_image, which renders at 1024x1024 with 8 steps (Z-Image-Turbo's own default, from #14078's fill), and the card shows the PNG from the chat's files with the prompt it wrote.

Image request before and after

Video. With Wan2.2-TI2V-5B loaded instead, "Make a 2 second video of waves rolling onto a beach at sunset." calls generate_video with no model named. Nothing switched or loaded, the job ran 20 steps on the resident model, and the clip plays inline.

Video card

Design check (report-only, against the Unsloth Studio design rules): no violations. Its nits were fixed in 809dc77: the loading frame is a status, the caption clamps to 4 lines like the hosted card, and a failed image or video says so in the card label. The Download overlay and the shared media sizing match the hosted image card.

Etherll and others added 7 commits October 17, 2026 23:43
…s while stopping is kept, and media cards name a stop

- generate_video no longer cancels a create still in flight after its wait: past
  begin_generate the job already runs, and the cancel left it with no record on
  the Video page. Stop during the create stops the job once it exists.
- generate_image returns the image when the render finishes inside the stop window.
- The image and video cards treat any plain-string result as "not generated" and
  show its text, so a stopped call no longer reads "Generated ...".
- The Images pill's readiness is also re-read on window focus, so an idle unload or
  a load from another tab or MCP client shows up.
@danielhanchen danielhanchen self-assigned this Oct 18, 2026
@danielhanchen
danielhanchen merged commit 56d1204 into unslothai:main Oct 19, 2026
danielhanchen added a commit that referenced this pull request Oct 19, 2026
…eset the startup budget to the build (#14168)

## Summary

Main is over the startup bundle budget, so `Frontend build + bundle
sanity` fails on every PR based on it:

```
over the startup budget: transfer 2005.9 KB > 2003.5 KB, raw 6665.4 KB > 6659.1 KB      (main at 2fee36f)
```

This PR takes two steps. It moves #14137's prompt upsampler client out
of the startup chunks, which is real growth with a lazy home. It then
resets both limits to the measured build. Main has gone over this budget
repeatedly, and the root of that is a shortfall in how the limits were
raised, which this corrects.

## Cause

**#14137 (FLUX.3 prompt upsampling).** The Images page is lazy
(`ImagesPage = lazy(...)` in `__root.tsx`), but `features/images/api.ts`
is not. Startup modules import it for other calls
(`loaded-models-api.ts`, `loaded-models-sources.ts`,
`use-media-companion-bytes.ts`). #14137 put the four upsampler calls
there, with value imports from `lib/prompt-enhance.ts`, so that module
joined the startup chunks. Only `prompt-enhance-panel.tsx`, inside the
lazy Images page, calls them.

**The limits themselves.** Each raise since #14073 was sized from its
own PR's delta and added to the previous limit. That previous limit
already sat below the build, starting at #14073:
- `475a02ac` measures 2,047,700 B transfer against the 2,047,608 B limit
#14073 set.
- By `2fee36ff` the gap is 6,500 B raw and 2,409 B transfer.

The raises since (#14072, #14075, #14110, #14108, #14113) each added
their delta, so the shortfall carried forward. CI agrees with my machine
to the byte: the first run of this PR read 1999.7 KB transfer on both.

## Change

- **New `features/images/prompt-upsampler-api.ts`:** holds
`getPromptUpsamplerStatus`, `startPromptUpsamplerDownload`,
`cancelPromptUpsamplerDownload` and `previewEnhancedPrompt`, moved
unchanged, with the same `parseJson` error handling.
- **`api.ts`:** now imports only the `PromptEnhancement` type from
`lib/prompt-enhance` (erased at build).
- **`prompt-enhance-panel.tsx`:** imports the calls from the new module.
- **No behavior change** in the app.
- **`check-bundle-budget.ts`:** both limits are reset to the measured
build plus the usual margin (160 B raw, 189 B transfer), with an entry
in the BUDGET comment block explaining why the reset is absolute rather
than by delta.
  - `rawBytes`: 6,819,206 (main after #14113) -> 6,825,885.
  - `transferBytes`: 2,051,727 -> 2,054,261.

## Testing

One machine and build, Node 24, exact byte totals from the budget
script, 95 chunks throughout:

| tree | raw B | transfer B |
|---|---|---|
| main `2fee36ff` | 6,825,378 | 2,053,999 |
| main `2fee36ff` + the move | 6,821,006 | 2,052,708 |
| main `78e5eefa4d` (adds #14113) + the move | 6,821,334 | 2,052,857 |
| + #14183 | 6,821,410 | 2,052,614 |
| + #14172 (one tooltip design) | 6,825,307 | 2,053,990 |
| this PR (main `b227654927`, adds #14184, + the move) | 6,825,725 |
2,054,072 |

- **The move saves:** 4,372 B raw and 1,291 B transfer.
- **#14113 adds:** 328 B raw and 149 B transfer on this build.
- **Landed while main was over, with no raise:** #14183 (+76 B raw, -243
B transfer), #14172 (+3,897 B raw, +1,376 B transfer, the shared tooltip
is in the eager shell) and #14184 (+418 B raw, +82 B transfer). This is
the treadmill the absolute reset stops: while main is red on this check,
growth lands unaccounted.
- **With the new limits:** `within budget (0.2 KB transfer, 0.2 KB raw
to spare)`.
- **`npm run typecheck`:** clean.
- **`bundle-budget-cli.test.ts`, `bundle-budget-closure.test.ts`,
`image-prompt-enhance.test.ts` and
`loaded-models-platform-matrix.test.ts`:** 74 passed.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
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.

2 participants