sgl-project/sglang · error · ImportError

The fastokens package is required when --tokenizer-backend=f

Error message

The fastokens package is required when --tokenizer-backend=fastokens. Install it with: pip install 'sglang[fastokens]'

What it means

--tokenizer-backend=fastokens was requested (in get_tokenizer/get_processor), but the optional fastokens package is not installed, so it cannot patch transformers. Install it via the sglang[fastokens] extra.

Source

Thrown at python/sglang/srt/utils/hf_transformers/tokenizer.py:460


# ---------------------------------------------------------------------------
# Public entry point
# ---------------------------------------------------------------------------


_fastokens_patched = False


def _ensure_fastokens_patched():
    """Monkey-patch transformers to use the fastokens backend (once)."""
    global _fastokens_patched
    if _fastokens_patched:
        return
    try:
        import fastokens
    except ImportError:
        raise ImportError(
            "The fastokens package is required when --tokenizer-backend=fastokens. "
            "Install it with: pip install 'sglang[fastokens]'"
        ) from None

    fastokens.patch_transformers()
    _fastokens_patched = True
    logger.info("fastokens backend enabled - transformers patched successfully")


def get_tokenizer(
    tokenizer_name: str,
    *args,
    tokenizer_mode: str = "auto",
    trust_remote_code: bool = False,
    tokenizer_revision: Optional[str] = None,
    tokenizer_backend: str = "huggingface",
    **kwargs,
) -> Union[PreTrainedTokenizer, PreTrainedTokenizerFast]:

View on GitHub (pinned to 0132848349)

Solutions

  1. pip install 'sglang[fastokens]'
  2. Or drop --tokenizer-backend fastokens and use the default HF tokenizer backend

Example fix

# before: command fails with ImportError
# after
pip install 'sglang[fastokens]'
Defensive patterns

Strategy: validation

Validate before calling

if tokenizer_backend == 'fastokens':
    import importlib.util
    assert importlib.util.find_spec('fastokens') is not None, 'pip install sglang[fastokens]'

Prevention

When it happens

Trigger: Passing --tokenizer-backend fastokens in an environment where 'import fastokens' fails.

Common situations: Using the flag after seeing it in docs/benchmarks without installing the optional dependency.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/62aa5416e7f05465. Report an issue: GitHub.