Prerequisites
- Install your extension and a compatible SGLang version in the same Python 3.10+ environment.
- Declare the SGLang version range tested by your extension in its package dependencies.
- Keep the backend factory and detector independent of GPU initialization and model loading.
Keep one owner for the executable
Only thesglang distribution should publish a console script named sglang. Your extension registers package metadata under sglang.serve_backends; it must not publish another sglang script.
This prevents installation order from replacing the command and ensures uninstalling an extension does not remove the core executable. You can retain a project-specific executable as a compatibility alias:
Register a backend factory
Add a zero-argument factory to your extension’spyproject.toml:
my_runtime, becomes an accepted --model-type value. Choose a distinctive name. auto and SGLang’s in-tree backend names are reserved.
Implement the backend
Createmy_sglang_runtime/sglang_backend.py:
supports_model() and launch_runtime() with your extension’s lightweight metadata check and blocking server launcher. A real run() call should block for the server lifetime. It must also honor -h and --help without launching a server; argparse does this automatically.
Among serve backend entry points, explicit selection imports only the selected provider. Automatic selection loads installed backend factories and invokes their detectors, so importing this module and calling detect() must not initialize accelerators, import model weights, or start workers.
Handle forwarded arguments
SGLang removes--model-type and normalizes a positional Hugging Face model ID or local model directory before dispatch. For example:
Support config-only runtimes
SGLang requires a model path by default. If your runtime resolves its model and parallelism settings from a configuration file, disable that validation:--model-type unless your detector can identify the backend from the remaining arguments.
Understand automatic routing
The default--model-type auto follows these rules:
- Backends without a detector remain explicit-only.
- One
MATCHselects that backend. - Multiple matches fail and require an explicit
--model-type. UNKNOWNand detector failures do not claim the request.- No matches preserve the existing LLM fallback.
Maintain compatibility
Declare the API version implemented by your extension as a literal. Do not copy SGLang’s current version constant at runtime; a fixed value lets a future SGLang release detect an older plugin contract. SGLang rejects incompatible, duplicate, and reserved backend registrations with an actionable error. The public extension contract consists of:ServeRequest: normalized backend arguments and the optional model pathServeBackend: the runner, optional detector, and model-path requirementServeBackendDetection:MATCH,NO_MATCH, orUNKNOWN
sglang package.