Skip to main content

llm-router-utils

Lightweight extraction of sglang's reasoning parser, tool-call parser, and chat template rendering, for use in custom router services and lightweight LLM applications.

This library does not include any inference engine code. It only provides the "frontend" message processing pipeline: OpenAIServingChat._process_messages and its dependencies.

Upstream source: sglang release/v0.5.17.

Installation

pip install llm-router-utils

CPU-only torch (optional, smaller on Linux)

This library depends on xgrammar, which declares torch>=1.10.0. By default pip pulls the CUDA-enabled torch wheel from PyPI (~502 MB on Linux x86_64). Routers don't use torch for GPU ops — only a lazy torch.version.hip / torch.npu.is_available() probe that returns False when torch is absent — so CPU-only torch is sufficient.

To install CPU-only torch, install it from the PyTorch CPU index before this package:

pip install torch --index-url https://download.pytorch.org/whl/cpu
pip install llm-router-utils

The second command sees torch already satisfied and skips the CUDA download. On Linux x86_64 this saves ~319 MB (502 → 183). On Windows there is no size difference (both ~116 MB), so this step is only useful on Linux.

Usage

from llm_router_utils.sglang.srt.configs.model_config import ModelConfig
from llm_router_utils.sglang.srt.managers.tokenizer_manager import TokenizerManager
from llm_router_utils.sglang.srt.parser.template_manager import TemplateManager
from llm_router_utils.sglang.srt.entrypoints.openai.serving_chat import OpenAIServingChat
from llm_router_utils.sglang.srt.server_args import ServerArgs, PortArgs

server_args = ServerArgs(model_path="Qwen/Qwen3-32B", tool_call_parser="qwen3_coder")
port_args = PortArgs()
tokenizer_manager = TokenizerManager(server_args, port_args)
template_manager = TemplateManager()
template_manager.initialize_templates(
    tokenizer_manager=tokenizer_manager,
    model_path=server_args.model_path,
    chat_template=server_args.chat_template,
)
openai_serving_chat = OpenAIServingChat(tokenizer_manager, template_manager)
result = openai_serving_chat._process_messages(request, is_multimodal=False)

What's included

Migrated modules under llm_router_utils/sglang/srt/ (paths preserved from upstream):

Module Description
parser/ Conversation templates (~50 model families), ReasoningParser + detectors, harmony/inkling, jinja utils, template detection, TemplateManager with TokenizerLike Protocol
function_call/ FunctionCallParser + 33 detectors (hermes, glm, deepseek, qwen, kimi, mistral, …), JsonArrayParser, schema utils
entrypoints/openai/ protocol.py (~1900 lines, full OpenAI types), slimmed serving_chat.py (only _process_messages chain), serving_base.py, chat_encoding.py, encoding_dsv32/dsv4.py, sse_utils.py, usage_processor.py, utils.py
managers/ Slimmed TokenizerManager (uses upstream get_tokenizer for byte-parity incl. SGLANG_PATCH_TOKENIZER), slimmed io_struct.py, embed_types.py stub
configs/ Slimmed ModelConfig (uses upstream get_config; exposes hf_config/is_multimodal/get_default_sampling_params/context_length), model_config_parser_registry.py
tokenizer/ tiktoken_tokenizer.py
disaggregation/ kv_events.py — KV cache event structs (EventBatch, KVCacheEvent, StorageMedium, BlockStored, BlockRemoved, AllBlocksCleared, KVEventBatch)
mem_cache/ utils.py — pure-Python SHA256 hash helpers, byte-identical to sglang's C++ extension
observability/ metrics_collector.py — data classes only: QueueCount, SchedulerStats, compute_routing_key_stats. Heavy *MetricsCollector classes stripped
utils/hf_transformers/ Restored common.py/config.py/tokenizer.py/mistral_utils.py — upstream get_tokenizer/get_config. Slimmed hf_transformers_patches.py (torch-free only). patch_tokenizer.py verbatim
connector/ (stub) create_remote_connector raises NotImplementedError
Top-level slimmed files environ.py (env var registry) · server_args.py (device/revision/tokenizer_backend + PortArgs.init_new) · srt/utils/common.py (ImageData/VideoData/read_system_prompt_from_file + hf helpers) · sglang/utils.py (convert_json_schema_to_str/is_in_ci/TypeBasedDispatcher/LazyImport)

What's NOT included

Inference engine code is intentionally stripped: schedulers, model loaders, layer implementations, CUDA/Triton kernels, sampling, constrained decoding, speculative decoding, LoRA runtime, distributed runtime, KV cache manager, HTTP server, multimodal processing, observability, and all CLI/launch scripts.

Version Mapping

The mapping between this repo's releases and upstream sglang versions, so users can find the release that matches a given sglang version:

This repo Upstream sglang
v0.2.2 v0.5.16
v0.3.0 v0.5.17

See HOW_TO_UPGRADE.md for the upgrade workflow.

License

Apache 2.0, adapted from sglang.

This is a derivative work of sglang. The upstream copyright notice is retained in LICENSE and NOTICE per Apache 2.0 §4(b)/§4(c). Retained source files keep their upstream copyright headers; slimmed files carry both the upstream header and a "Derivative work" notice. See HOW_TO_UPGRADE.md for the trimming rules.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_router_utils-0.3.0.tar.gz (255.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_router_utils-0.3.0-py3-none-any.whl (317.4 kB view details)

Uploaded Python 3

File details

Details for the file llm_router_utils-0.3.0.tar.gz.

File metadata

  • Download URL: llm_router_utils-0.3.0.tar.gz
  • Upload date:
  • Size: 255.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for llm_router_utils-0.3.0.tar.gz
Algorithm Hash digest
SHA256 d011f578d5ef4bb0c49ef513fd2602faeaf8577d1f79e2765e34f978126cb3bb
MD5 15641e673f53a9c9007f6299dbad228d
BLAKE2b-256 25a74583faa011727ff5fb01525c64f0ea82c43e0235df5e7d72ceb9a6663dd3

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_router_utils-0.3.0.tar.gz:

Publisher: publish.yml on wxsms/llm-router-utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llm_router_utils-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for llm_router_utils-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d395289ea249d42567b7659a2583ebcba8aafc845eae7abb984d319e8e46e1ea
MD5 5b91c01714b08f243e3ba2e3aba41e9a
BLAKE2b-256 408fbd5de373ca58a24c8cb4aa0437334e2a514e4aa0efb07f60cb0eb023c4b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_router_utils-0.3.0-py3-none-any.whl:

Publisher: publish.yml on wxsms/llm-router-utils

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page