Skip to content

Studio: per-model launch flags, model discovery, and a settings UI for auto-switch - #6430

Merged
danielhanchen merged 10 commits into
unslothai:studio-openai-auto-switch-modelfrom
oobabooga:studio-openai-auto-switch-model
Jun 22, 2026
Merged

danielhanchen merged 10 commits into
unslothai:studio-openai-auto-switch-modelfrom
oobabooga:studio-openai-auto-switch-model

Conversation

@oobabooga

Copy link
Copy Markdown
Member

Builds on the opt-in OpenAI model auto-switch in this branch and adds the user-facing layer on top: per-model launch flags, model discovery via /v1/models, and a settings UI. Off by default, so drop-in behavior is unchanged until enabled.

What this adds

  • Per-model launch flags. llama_extra_args and max_seq_length are applied when a model is auto-switched, so a swapped model loads the way the user configured it instead of with bare defaults. Configured via GET/PUT /api/settings/openai-auto-switch/overrides, keyed by the model id /v1/models advertises. Stored with an atomic per-entry merge so two PUTs for different models can't drop each other.
  • /v1/models discovery. When auto-switch is on, /v1/models lists every downloaded GGUF it can swap to (and retrieve resolves them), so a client can discover what to request instead of seeing only the loaded model. Off keeps the default listing (loaded model only) unchanged.
  • Settings UI. A "Model auto-switch" section in Settings, General tab, with the enable toggle and idle auto-unload. Idle auto-unload is tied to the toggle: it only works as sleep/wake (an unloaded model reloads on the next request, which needs the swap), so the idle control is disabled while the toggle is off, and turning the toggle off clears the idle timer.

Screenshot

The "Model auto-switch" section, General tab (enable toggle, idle auto-unload, and the hint shown while it is off):

image

Verification

  • 27 backend tests in studio/backend/tests/test_openai_auto_switch.py: the existing suite plus new tests for the overrides (apply, partial apply, store round-trip, route validation), and the discovery list and retrieve paths. Frontend typecheck and biome clean.
  • Validated live against llama-swap v228 with the same downloaded GGUFs and the same llama-server under both. The two features this PR adds match llama-swap: /v1/models advertises every swap-eligible model (Studio listed all 7 downloaded GGUFs, llama-swap its 2 configured), and a per-model context override loads at that size (8192, same as llama-swap's --ctx-size). The auto-switch core (swap-by-model; an unknown model serves the loaded one, where llama-swap 404s) was re-confirmed unchanged after the rebase.

Notes

  • The per-model overrides ship as a backend route; there is no per-model editor in the UI yet (the enable and idle controls are the UI here). Configure overrides via the PUT route using the id /v1/models reports.
  • Idle auto-unload covers UI chat too: the Studio chat UI calls the same /v1/chat/completions, so active use resets the timer and a model is never unloaded mid-response.

@oobabooga
oobabooga requested a review from danielhanchen as a code owner June 18, 2026 09:15

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces per-model launch overrides (such as llama_extra_args and max_seq_length) when auto-switching models, along with corresponding API endpoints, database persistence, and a frontend settings UI. The feedback highlights three key improvements: explicitly checking for None on max_seq_length to avoid ignoring 0, making the model retrieval endpoint case-insensitive to align with the local resolver, and improving frontend input validation to prevent empty strings from being silently coerced to 0 seconds.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread studio/backend/routes/inference.py Resolved

@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

Here are some automated review suggestions for this pull request.

Reviewed commit: b8db6a7bc9

ℹ️ 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".

Comment thread studio/backend/routes/inference.py Resolved
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@danielhanchen

Copy link
Copy Markdown
Member

Thanks for this, it is a clean extension and the atomic per-entry override merge plus the validate_extra_args gate are nicely done. I reviewed it end to end (manual read, the two bot reviews, and a 5-reviewer pass) and ran the suite locally; the 27 backend tests pass.

I pushed three hardening fixes to the base branch (studio-openai-auto-switch-model, 2e5dfa73), so they will land here automatically on your next rebase:

  • count_tokens keep-warm. /v1/messages/count_tokens auto-switches but was not tracked by the keep-warm middleware, so idle auto-unload could free the model mid-count. It is now a tracked in-flight path.
  • Idle gated on auto-switch. get_auto_unload_idle_seconds() now returns 0 while auto-switch is off, so a stray TTL can never unload while the feature is disabled. This matches the coupling your UI already enforces (the idle control is disabled and cleared when the toggle is off).
  • Hidden models excluded from the index. The resolver now skips what Studio hides from its pickers (the llama.cpp stories260K validation probe and the RAG embedding weights) via _is_hidden_model. Since list_switch_eligible_ids() reads the same index, this also removes them from /v1/models discovery, so the discovery side of that finding is covered with no change needed here.

Two small things left for this PR:

1. Case-insensitive /v1/models/{id} retrieve. The resolver matches case-insensitively, so the retrieve route should too, otherwise GET /v1/models/unsloth/b-gguf 404s while the model is still swap-eligible.

    # Case-insensitive, matching the resolver: a name auto-switch can serve
    # (e.g. unsloth/b-gguf) must not 404 here over casing alone.
    for model in await _all_openai_model_objects():
        if model["id"].lower() == model_id.lower():
            return model

2. Empty idle field should not coerce to 0. Number("") === 0, so clearing the field and saving silently persists 0 (keep loaded) instead of showing the whole-number validation error.

  // Parse the idle-seconds draft to a non-negative integer, or null when invalid.
  // An empty or whitespace-only field is invalid, not a silent 0 (keep loaded).
  const parseIdleSeconds = (): number | null => {
    if (!draftIdleSeconds.trim()) return null;
    const parsed = Number(draftIdleSeconds);
    return Number.isInteger(parsed) && parsed >= 0 ? parsed : null;
  };

One design question (no change requested). Codex flagged that a partial override which sets only max_seq_length leaves llama_extra_args absent, so the load route inherits the previous same-model extra args. I looked at it: the backend preserves both extra_args and the requested n_ctx across unload, and max_seq_length uses 0 as its inherit sentinel, so it cannot be made authoritative symmetrically with extra_args. I would rather leave the current inherit behavior than make the two asymmetric, but flagging in case you want an override to fully define the launch. Happy to go either way.

The other automated suggestions (storing an explicit max_seq_length=0, per-variant discovery) read as optional to me; the current behavior is fine.

@oobabooga

Copy link
Copy Markdown
Member Author

Thanks! Merged your three fixes in. The two small ones (case-insensitive retrieve, empty idle field) were already in the branch. And I took your call on the partial override and kept the inherit behavior. Branch is updated.

@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

Here are some automated review suggestions for this pull request.

Reviewed commit: 4cff9c699e

ℹ️ 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".

Comment thread studio/backend/routes/inference.py Resolved
@danielhanchen
danielhanchen merged commit cd338b3 into unslothai:studio-openai-auto-switch-model Jun 22, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants