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.

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 + 34 detectors (hermes, glm, deepseek, qwen, kimi, mistral, muse-glimmer, …), 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
v0.4.0 v0.5.18
v0.5.0 v0.5.19
v0.5.1 v0.5.19

See DEVELOPMENT.md for the development workflow (upgrades, checks, releases).

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 DEVELOPMENT.md for the trimming rules.

Metadata

Release files for llm-router-utils 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llm-router-utils 0.5.1
File Size Uploaded Provenance
llm_router_utils-0.5.1.tar.gz 288.7 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-router-utils 0.5.1
File Interpreter ABI Platform
llm_router_utils-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 642.5 kB

Release files / llm_router_utils-0.5.1.tar.gz

Download URL llm_router_utils-0.5.1.tar.gz
Size 288.7 kB
Tags Source
SHA-256 checksum
How to use checksums
436ea85147d7b97b16cf3ef1d4686cc3996b12c4c4ada1b531c19935b101dd80
BLAKE2b-256 checksum
How to use checksums
4faa0247586a4ebbbb40cf2cb5cd9a23f4e0821df06cec4cbce61a3e68b410f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 11, 2026.

Transparency log

Release files / llm_router_utils-0.5.1-py3-none-any.whl

Download URL llm_router_utils-0.5.1-py3-none-any.whl
Size 353.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
013ba2687f51b3f239efdad4d4c8c37b492d59a76939c2f9cf5ada2472ca902e
BLAKE2b-256 checksum
How to use checksums
726ac491737361020ed7dd8a68f2beb349ea75cba8aa366b9e3ac0b9d492a9ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 11, 2026.

Transparency log

Security for llm-router-utils 0.5.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for llm_router_utils-0.5.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 11, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page