Skip to content

Studio: let full-scope keyless callers auto-switch models, and explain the refusal elsewhere - #11180

Merged
danielhanchen merged 7 commits into
unslothai:mainfrom
Lyxot:fix/keyless-full-scope-auto-switch
Sep 27, 2026
Merged

danielhanchen merged 7 commits into
unslothai:mainfrom
Lyxot:fix/keyless-full-scope-auto-switch

Conversation

@Lyxot

@Lyxot Lyxot commented Sep 17, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #11140

Problem

With Model auto-switch on and keyless API access set to full, an OpenAI-compatible client that sends no key never got a model loaded. That includes the hermes-agent default, which sends the approved dummy bearer no-key-required:

  • nothing loaded → 400 No model loaded. Call POST /inference/load first., no load attempted
  • another model loaded → 503 model_switch_failed ("could not switch… Retry shortly", Retry-After: 5), no switch attempted

So every local agent failed its first request after Studio started, until someone loaded a model by hand.

Root cause

_maybe_auto_switch_model turned auto-switch off for every caller admitted by keyless access, in every scope (added in #9102 so a LAN caller under the inference scope cannot repeatedly evict the operator's model or fetch Hub repos). Under the full scope, which is loopback-only, the same keyless caller is already admitted to POST /api/inference/load, POST /api/inference/unload and POST /api/hub/download. The block therefore protected nothing there and only broke the automatic path. Because the global setting reads as enabled, the 400 also dropped the "enable auto-switch" hint, so nothing explained the failure.

Changes

1. fix(studio): let keyless callers under the full scope auto-switch models

  • keyless_request_may_load_models(request) evaluates the existing keyless admission predicate for POST /api/inference/load using this request's transport. It deliberately does not compare scope == "full", so the rule cannot drift from admission.
  • A keyless caller is held back (_keyless_caller_held_back) only when that predicate is false. It is used for both places that previously keyed off "keyless": the auto-switch gate and the idle-stash restore restriction.
  • Result: under full over loopback, a keyless request behaves like a keyed one, including auto-download when that setting is on. Auto-download only ever uses an HF token the caller sends itself, which is narrower than the direct download route. The inference scope is unchanged.
  • Usage examples in Settings stay pinned to the loaded model only under the inference scope.

2. fix(studio): tell a keyless caller that cannot switch models why

For a caller that is still held back (the inference scope) with auto-switch on:

Case Before After
a different downloaded model named while one is loaded 503 model_switch_failed, "Retry shortly", Retry-After: 5 404 model_not_found: keyless API access can only use the loaded model; an API key switches models by request (same shape as the existing "switch is off" refusal)
a downloaded model named while nothing is loaded 400 "Call POST /inference/load first." same status: "No model is loaded, and keyless API access cannot load '' by request. Load it in Unsloth Studio, or send an Unsloth API key."

Keyed callers, UI sessions and full-scope keyless callers keep the existing responses.

Risk / tradeoffs

  • This intentionally widens what a full-scope keyless caller gets automatically: switch, idle restore and auto-download. It adds nothing that caller could not already do by calling the load and download routes directly. If maintainers prefer to keep auto-download key-only even under full, that can be split out without affecting the switch fix.
  • The inference scope keeps the protection from Studio: add secure keyless localhost and private-LAN API access #9102. The only change there is the refusal wording and status: a 503 that invited endless retries becomes a 404.

Validation

Selected tests (the one failure also fails on main on a case-insensitive filesystem):

python -m pytest studio/backend/tests/test_keyless_api_access.py tests/studio/test_usage_examples_model_source_contract.py studio/backend/tests/test_openai_auto_download.py studio/backend/tests/test_openai_auto_switch.py -q -n 8
# 898 passed, 1 failed (test_hf_cache_entry_stays_within_the_scanned_case_variant, pre-existing)
  • New tests go through real keyless admission: full keyless loads while inference keyless does not; idle-stash restore is restricted under inference but not under full; held-back callers get the new cold (400) and warm (404, no Retry-After) refusals, while the same request under full keeps the generic text. Each new test was mutation-checked: reverting the production branch it covers makes exactly that case fail.
  • End to end through the real openai_chat_completions handler, matrix {full, inference} × {cold, warm} × {no header, no-key-required, real API key}: full keyless now loads in every case, inference keyless never loads, keyed callers are unchanged.
  • Live API-only Studio against real downloaded GGUFs, auto-switch on, no key:
full,      nothing loaded,   no Authorization          -> 200 (model auto-loaded)
full,      model loaded,     Bearer no-key-required    -> 200 (switched to the named model)
inference, nothing loaded,   no Authorization          -> 400 "No model is loaded, and keyless API access cannot load ..."
inference, model loaded,     other model named         -> 404 model_not_found, no Retry-After
inference, model loaded,     loaded model named        -> 200
  • Frontend: tsc -p tsconfig.app.json clean.

Before / after (Settings > API, keyless scope full, auto-switch on, nothing loaded)

Usage examples before and after

Two isolated Studio installs (merge base fdbc8676a vs head f10893bfd), same seeded cache holding only unsloth/SmolLM2-135M-Instruct-GGUF Q4_K_M, keyless request sent with no Authorization and no Origin header:

main this PR
Usage example model placeholder, no model named unsloth/SmolLM2-135M-Instruct-GGUF:Q4_K_M
POST /v1/chat/completions, scope full, no key 400 No model loaded. Call POST /inference/load first. 200, model auto-loaded
same request, scope inference 400 No model loaded. Call POST /inference/load first. 400 No model is loaded, and keyless API access cannot load ... Load it in Unsloth Studio, or send an Unsloth API key.

A /v1 request admitted by keyless API access had model auto-switch, auto-download and idle-stash restore turned off in every scope. Under the full scope the same loopback caller is already admitted to POST /api/inference/load and POST /api/hub/download, so the block guarded nothing, while local agents that send no key or a dummy bearer (hermes-agent sends no-key-required) got 400 "No model loaded" on a cold slot and a retryable 503 on a warm one.

A keyless caller is now held back only when keyless access would not also admit it to POST /api/inference/load, evaluated with the existing admission predicate for this request's transport. The inference scope keeps the current behaviour. Usage examples stay pinned to the loaded model only under the inference scope.

Fixes unslothai#11140
With model auto-switch on, a keyless caller that keyless access does not also admit to POST /api/inference/load (the inference scope) is kept on the loaded model. Naming a different downloaded model answered it with 503 model_switch_failed, "could not switch... Retry shortly" and Retry-After, although nothing failed and no retry can succeed; with nothing loaded it got the generic "No model loaded. Call POST /inference/load first.", a route it cannot call.

The warm case now returns 404 model_not_found, like the refusal when switching is off, saying keyless API access can only use the loaded model and that an API key switches models by request. The cold case keeps its status and says keyless access cannot load the named model. Keyed callers and callers under the full scope keep the existing responses.
@danielhanchen

Copy link
Copy Markdown
Member

Confirmed the gate in studio/backend/routes/inference.py still disables auto-switch for every keyless scope including full, and that deriving the rule from admission on POST /api/inference/load rather than a scope string comparison is the right shape. Will get this reviewed.

@unslothai unslothai deleted a comment from Lyxot Sep 27, 2026
@unslothai unslothai deleted a comment from Lyxot Sep 27, 2026
@unslothai unslothai deleted a comment from Lyxot Sep 27, 2026
@unslothai unslothai deleted a comment from Lyxot Sep 27, 2026
@unslothai unslothai deleted a comment from Lyxot Sep 27, 2026
@unslothai unslothai deleted a comment from Lyxot Sep 27, 2026
@unslothai unslothai deleted a comment from Lyxot Sep 27, 2026
@unslothai unslothai deleted a comment from Lyxot Sep 27, 2026
@unslothai unslothai deleted a comment from oobabooga Sep 27, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 27, 2026
danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 27, 2026
@shimmyshimmer

Copy link
Copy Markdown
Member

Review record for #11180, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#151.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror 2bf445bc25 (this PR's f10893bfd)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 2bf445bc25

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror 2bf445bc25 (this PR's f10893bfd).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

danielhanchen added a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Sep 27, 2026
@danielhanchen
danielhanchen merged commit 7be1d14 into unslothai:main Sep 27, 2026
68 of 70 checks passed
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
…n the refusal elsewhere (unslothai#11180)

* fix(studio): let keyless callers under the full scope auto-switch models

A /v1 request admitted by keyless API access had model auto-switch, auto-download and idle-stash restore turned off in every scope. Under the full scope the same loopback caller is already admitted to POST /api/inference/load and POST /api/hub/download, so the block guarded nothing, while local agents that send no key or a dummy bearer (hermes-agent sends no-key-required) got 400 "No model loaded" on a cold slot and a retryable 503 on a warm one.

A keyless caller is now held back only when keyless access would not also admit it to POST /api/inference/load, evaluated with the existing admission predicate for this request's transport. The inference scope keeps the current behaviour. Usage examples stay pinned to the loaded model only under the inference scope.

Fixes unslothai#11140

* fix(studio): tell a keyless caller that cannot switch models why

With model auto-switch on, a keyless caller that keyless access does not also admit to POST /api/inference/load (the inference scope) is kept on the loaded model. Naming a different downloaded model answered it with 503 model_switch_failed, "could not switch... Retry shortly" and Retry-After, although nothing failed and no retry can succeed; with nothing loaded it got the generic "No model loaded. Call POST /inference/load first.", a route it cannot call.

The warm case now returns 404 model_not_found, like the refusal when switching is off, saying keyless API access can only use the loaded model and that an API key switches models by request. The cold case keeps its status and says keyless access cannot load the named model. Keyed callers and callers under the full scope keep the existing responses.

* Take the keyless load-route probe off the event loop

* Read the keyless load probe through the retrying settings accessor

* Trim keyless auto-switch comments

---------

Co-authored-by: oobabooga <[email protected]>
Co-authored-by: Daniel Han <[email protected]>
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.

OpenAI API: auto-switch does not cold-load a downloaded GGUF, so /v1/chat/completions returns 400 "No model loaded" when nothing is loaded

4 participants