Repository navigation
Studio: image and video generation follow each model's defaults and prompting tips, and /v1/videos takes Unsloth Studio controls - #14078
Conversation
…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.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
…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.
|
UI before/after and GPU runs for this PR. The frontend is unchanged since Prompt tip on the Images and Video pagesmain on the left, this branch on the right. Both sides got the same status fixtures (CPU Unsloth Studio builds, Playwright Chromium, 1440x900, light).
No video family ships a tip yet, so the Video row only shows the wiring. No console errors in any scene. Per-scene shots and the measured facts are in the evidence folder. GPU runs through MCP (one B200)
Review follow-ups (
|
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.
…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.
|
@codex security review |
…in frame-count errors [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4782549b35
ℹ️ 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".
| or bool(body.reference_images or reference_audios), | ||
| ) | ||
| # Decoded here, like the images above, so unreadable audio never costs a switch. | ||
| _refuse_unreadable_audio(reference_audios) |
There was a problem hiding this comment.
Preflight audio when no model switch occurs
When model is omitted, auto-switching is disabled, or the requested model is already resident, maybe_auto_switch_media_model never invokes its before_switch callback, so this only production call to _refuse_unreadable_audio is skipped. A corrupt but valid-base64 reference_audios item then reaches PyAV in begin_generate; errors such as InvalidDataError/FFmpegError are not handled by this route's ValueError and RuntimeError catches, turning bad client media into a 500 instead of the intended 400. Run this preflight for every request before the optional switch.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not reachable as a 500: PyAV raises InvalidDataError for undecodable input, and in PyAV 19 that class subclasses ValueError (MRO InvalidDataError -> FFmpegError -> ValueError), so begin_generate's decode failure lands in this route's except ValueError and returns 400. The preflight only exists so unreadable audio never costs a model switch.
There was a problem hiding this comment.
Follow-up in 2528986: ordinary corrupt audio was already a 400, but a codec the FFmpeg build cannot decode raises a non-ValueError, so requests with no switch now run the same readability check before rendering.
…zes the body before uploading audio
…ads resident reference audio before rendering [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8bd89f7edb
ℹ️ 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".
| expected_load = load_identity( | ||
| status.get("repo_id"), status.get("base_repo"), status.get("family") | ||
| ) |
There was a problem hiding this comment.
Include the checkpoint file in the defaults pin
When omitted defaults are used and another load replaces a local model with a different GGUF/safetensors file in the same repo/base/family between this status read and generate, this pin still compares equal because LoadIdentity contains only those three values. _image_generation_defaults explicitly depends on gguf_filename and the checkpoint-content variant, so variants such as FLUX dev and schnell in one directory can run the replacement checkpoint with the previous variant's steps/guidance instead of taking the retry; include the selected checkpoint/build in the identity used for this pin.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not this PR's contract: the (repo, base, family) load identity and its pin predate this change (#9448) and the OpenAI image route already used it on main. Reaching it needs a concurrent load of a sibling checkpoint in the same repo between the status read and generate; widening the identity touches all three engines and belongs in its own change.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
# Conflicts: # studio/backend/routes/inference.py
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
# Conflicts: # studio/MCP.md
…ed local models (#14110) 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 (#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` (#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 #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-evidence:start --> ### 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.  <!-- ui-evidence:end --> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Han <[email protected]>

Summary
/api/inference/images/generateused fixed defaults of 9 steps and guidance 0 when a caller left them out. Those values only suit turbo models. Omittedstepsandguidancenow come from the loaded model: the diffusersgeneration_defaultswhen the status reports them, else the family table (sd.cpp and MLX)./v1/videosalso accepts the Unsloth Studio generation controls that/video/generatealready had, and the MCPgenerate_imageandgenerate_videotools expose the new options.Changes
Images:
/v1/imagesnow uses the same_image_generation_defaultshelper.stepsorguidancewhile another client's image load is still running is pinned to "nothing loaded". The engine then refuses the new model on its first try, and the retry reads that model's defaults instead of running it with 9 steps and guidance 0.generate_imagerefuses requests the engine cannot run before uploading anything:reference,editoroutpaintwithoutinit_image,outpaintwithoutmask_image, andupscale <= 1. It also takesseedsandpromptslists (up to 32) for a batch in one call.studio_statusimage now reportsworkflows,generation_defaults,dimension_multiple,max_condition_imagesandprompt_hint.Prompt hints:
prompt_hint, returned in the status of every engine (diffusers, sd.cpp, MLX, video). FLUX.1 Kontext, Qwen-Image-Edit, Qwen-Image 2.1 and Ideogram 4 have one. No video family sets one yet.image-promptingskill, off by default. It tells the model how to expand a short request into a detailed prompt, and to leave steps and guidance unset so the model's defaults apply.Videos:
/v1/videosacceptsseed,negative_prompt,steps,guidance,num_frames,fps,last_frame,reference_images(up to 9) andreference_audios(up to 3, as data URLs, uploads or Unsloth Studio audio ids{"input_id" | "clip_id" | "voice_id"}). These work in both JSON and multipart bodies.secondsandnum_framesreturns 400. So dolast_framewith references,input_referencewithreference_images, and reference audio with no reference image./v1/videosrefuses anotherfpsfor it (400,param: fps) rather than computing the frame count and the reported length from a rate the file never plays at. The check runs before a switch, on the resident model, and on the pinned snapshot that auto-switch and managed accounts use.last_frameand eachreference_imagesentry take the same 32 MB imageinput_referencetakes. Before, their base64 cap stopped images at about 24 MB. Each reference audio takes a full 24 MB clip as a data URL. A stored reference audio is sized before it is read. Multipart reference lists keep the order they were sent in, across thereference_imagesandreference_images[]spellings, and are counted before any file is encoded. Before an automatic switch, each reference audio is decoded the way the engine would, so an unreadable clip is a 400 instead of a failure after the new model loads.seed.generate_videoexposes the same controls, acceptssecondsas a number or a string, accepts fractionalseconds, refuses images over the 32 MB one/v1/videosrequest carries, audio over 24 MB, and field combinations the route refuses (before it uploads any audio), and returns the seed on the job and fromget_job.studio_statusvideo now reportsprompt_hint,supports_last_frame,referencesandsize_presets.studio/MCP.mdlists the new tool options.Testing
New tests:
test_image_generate_model_defaults.py,test_openai_videos_controls.py,test_mcp_tools_video_controls.py,test_mcp_status_media_slots.py,test_diffusion_prompt_hint.pyandstudio/frontend/tests/images-prompt-hint.test.ts. Updated in place:test_mcp_tools_images.py,test_mcp_tools_status.py,test_mcp_tools_video.py,test_skills.pyandtest_video_families.py./v1/videosrequest, which still sendsbegin_generateexactly its old arguments plusseed;seed: null;bef45ec6b9tob1d07abd7c) each come with tests that fail on the earlier head: the H3 fps refusal (before a switch, resident and pinned), multipart order and count, the audio size checks, the 32 MB image and 24 MB audio caps, the MCP image budget, body cap and pre-upload checks, fractional MCP seconds, the pre-switch audio decode and the in-flight load retry. Affected suites at the final head (image and video routes,/v1/videos,/v1/images, account pinning, MCP tools and server, skills, families, prompt hints): 825 passed, 1 failed. That one,test_video_download_plan_judges_a_quantized_reference_pick_per_partition, fails the same way onmainhere (no PyAV).mainin this environment (no PyAV, and a huggingface-hub version mismatch). The 15th,test_begin_load_answers_without_waiting_on_the_header_probe, is a timing test that passed 3 of 3 runs on its own.UNSLOTH_FREEZE_UPDATE=1regeneration of the diffusion family freeze leaves the fixture unchanged. It records accessor outputs, not family rows.npm run typecheck, and 230 image and video node tests pass.ruff check(0.16.10) is clean, and the repo formatter (ruff 0.6.9) is a no-op on the changed files.The UI before/after screenshots and the GPU harness runs (hero portrait plus same-character shots with no HTTP 400s; two same-seed video takes) follow in a comment.
UI before / after
With FLUX.1 Kontext loaded, the Images page Instruction field gains an info icon whose tooltip reads 'Write the change you want as an instruction, such as "make the sky orange", not a description of the whole picture.' Before this PR the field has no icon. BEFORE is the merge base, AFTER is this PR, two separate installs, each loading the same tiny FLUX.1 Kontext pipeline through the real load route.