Skip to content

fix(models): honor model_name on /models/load and /models/unload (fixes #977) - #988

Closed
SEPURI-SAI-KRISHNA wants to merge 2 commits into
jamiepine:mainfrom
SEPURI-SAI-KRISHNA:fix/models-load-by-name
Closed

SEPURI-SAI-KRISHNA wants to merge 2 commits into
jamiepine:mainfrom
SEPURI-SAI-KRISHNA:fix/models-load-by-name

Conversation

@SEPURI-SAI-KRISHNA

@SEPURI-SAI-KRISHNA SEPURI-SAI-KRISHNA commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #977. Also addresses #448.

Problem

POST /models/load declared only a model_size query parameter and always dispatched to the default Qwen backend:

@router.post("/models/load")
async def load_model(model_size: str = "1.7B"):
    tts_model = tts.get_tts_model()          # always Qwen
    await tts_model.load_model_async(model_size)

Because no body model is declared, FastAPI silently discards any JSON body. So POST /models/load {"model_name": "kokoro"} returns 200 OK and then starts a 3.6 GB Qwen 1.7B download. There was no way to load Kokoro, Chatterbox, LuxTTS, Whisper, or the LLM through this endpoint — even though /models/download, /models/{name}/unload, and DELETE /models/{name} all dispatch correctly via get_model_config / get_model_load_func.

This is a documented-contract violation, not a feature request. docs/content/docs/developer/model-management.mdx already specifies:

POST /models/load
{ "model_name": "qwen-tts-1.7B" }

The route looks up the config, dispatches to get_model_load_func(config), and returns once the model is ready.

/models/unload had the identical bug. The same doc page documents POST /models/unload {"model_name": "chatterbox-tts"} → unload_model_by_config(config). That body was ignored too, so a user following the docs to free VRAM for Chatterbox would silently unload Qwen instead. Unfiled, same shape, same file, same doc page — fixed alongside.

Fix

Both routes now accept an optional ModelLoadRequest body. With model_name they resolve through the registry; an unknown name returns a 400 listing the valid ids instead of silently loading Qwen. Without one, behaviour is unchanged.

Backward compatibility was verified by request, not just by reading:

call result
POST /models/load (no body) loads Qwen 1.7B
POST /models/load?model_size=0.6B loads Qwen 0.6B
POST /models/load?model_size=0.6B + empty body + Content-Type: application/json loads Qwen 0.6B
POST /models/load {"model_name":"kokoro"} loads Kokoro
POST /models/unload (no body) unloads Qwen
POST /models/kokoro/unload unchanged, still works

No frontend change is needed: the app never calls /models/load, and apiClient.unloadModel uses the path-param route.

Tests

backend/tests/test_models_load_by_name.py — 12 tests covering per-engine dispatch across TTS/STT/LLM families, the unknown-model 400, and every legacy calling form.

python -m pytest backend/tests/test_models_load_by_name.py -v

6 of the 12 fail on main. The rest of the backend suite is unaffected.

Notes

  • docs/openapi.json and app/src/lib/api/services/DefaultService.ts are generated artifacts and are now stale for this route. Left alone — regenerating requires a running backend, and recent merged PRs that changed routes (e.g. fix(export): disambiguate export filenames with generation id #956) didn't touch them either. Happy to include a regenerated pair if you'd prefer.
  • CHANGELOG.md is deliberately untouched — its header states it is compiled automatically during the release workflow.

Summary by CodeRabbit

Release Notes

  • New Features

    • Load and unload registered TTS, Whisper, and LLM models by name.
    • Continue using the default model behavior when no model name is provided.
    • Preserve legacy model-size selection for default Qwen loading.
    • Receive clear errors for unknown model names and statuses indicating whether a model was unloaded.
  • Documentation

    • Updated model management guidance for name-based loading and unloading.

@coderabbitai

coderabbitai Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 59d92c71-f900-4df0-a174-cca0ad2c355d

📥 Commits

Reviewing files that changed from the base of the PR and between 41aa57b and 9260961.

📒 Files selected for processing (2)
  • backend/routes/models.py
  • backend/tests/test_models_load_by_name.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • backend/routes/models.py

📝 Walkthrough

Walkthrough

The model management API now accepts optional model names for loading and unloading. Named models use registry resolution and backend dispatch. Requests without a model name retain default Qwen behavior. Tests and documentation cover the new routing.

Changes

Model routing

Layer / File(s) Summary
Request contract and registry resolution
backend/models.py, backend/routes/models.py
ModelLoadRequest accepts optional model_name and model_size fields. Registered model names resolve through the model registry, while unknown names return HTTP 400.
Load and unload endpoint routing
backend/routes/models.py, backend/tests/test_models_load_by_name.py, docs/content/docs/developer/model-management.mdx
The endpoints dispatch named models through registry loaders and unloaders. Default Qwen loading, legacy model_size handling, error wrapping, and named unload responses remain supported. Tests and documentation cover named and default behavior.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant ModelsRoute
  participant ModelRegistry
  participant BackendLoader
  Client->>ModelsRoute: POST /models/load with model_name
  ModelsRoute->>ModelRegistry: Resolve model configuration
  ModelRegistry-->>ModelsRoute: Return model configuration
  ModelsRoute->>BackendLoader: Load resolved model
  BackendLoader-->>ModelsRoute: Return load result
  ModelsRoute-->>Client: Return response
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the fix: honoring model_name for the /models/load and /models/unload endpoints.
Linked Issues check ✅ Passed The changes satisfy issue #977 by resolving model_name through the registry, preserving legacy behavior, and rejecting invalid names.
Out of Scope Changes check ✅ Passed The route, request model, tests, and documentation changes directly support the linked issue objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai 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.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
docs/content/docs/developer/model-management.mdx (1)

120-133: 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Align the endpoint table with the registry scope.

Line 120 states that /models/load accepts TTS, Whisper, and LLM model IDs. The Manual Model Operations table at Lines 105-106 still describes /models/load and /models/unload as TTS-only. Change both descriptions to registered-model operations.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/content/docs/developer/model-management.mdx` around lines 120 - 133,
Update the Manual Model Operations table entries for /models/load and
/models/unload to describe operations on registered models rather than TTS-only
models, matching the registry scope documented in the surrounding
model-management section.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@backend/routes/models.py`:
- Around line 85-86: Explicit empty model names are being treated as omitted,
incorrectly triggering the default Qwen load/unload paths. In
backend/routes/models.py lines 85-86 and 117-118, change both requested_name
checks to distinguish None from an empty string; in
backend/tests/test_models_load_by_name.py lines 89-96 and 130-134, add
empty-string cases asserting 400 responses and no named or Qwen load/unload
calls.

---

Outside diff comments:
In `@docs/content/docs/developer/model-management.mdx`:
- Around line 120-133: Update the Manual Model Operations table entries for
/models/load and /models/unload to describe operations on registered models
rather than TTS-only models, matching the registry scope documented in the
surrounding model-management section.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 4cffc99d-cf00-430e-89b7-15ff23228460

📥 Commits

Reviewing files that changed from the base of the PR and between 51f49de and 41aa57b.

📒 Files selected for processing (4)
  • backend/models.py
  • backend/routes/models.py
  • backend/tests/test_models_load_by_name.py
  • docs/content/docs/developer/model-management.mdx

Comment thread backend/routes/models.py Resolved
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.

POST /models/load has no model_name param — always loads Qwen 1.7B regardless of installed engine (Kokoro, etc.)

1 participant