sgl-project/sglang · error · ValueError

External ngram corpus path does not exist: {path}

Error message

External ngram corpus path does not exist: {path}

What it means

iter_external_corpus_chunks validates that the given path is an existing regular file before streaming a JSONL corpus for the C++ ngram speculative backend. If Path(path).is_file() is False it raises this ValueError, protecting the tokenizer-loading path from missing or mis-typed corpus files.

Source

Thrown at python/sglang/srt/speculative/cpp_ngram/external_corpus.py:18

import json
from collections.abc import Iterator
from pathlib import Path

# Must match SuffixAutomaton::kSeparatorToken in suffix_automaton.h.
SEPARATOR_TOKEN = -(2**31)

# Default chunk size for streaming tokenized documents into the SAM.
DEFAULT_CHUNK_SIZE = 4096


def iter_external_corpus_chunks(
    path: str, tokenizer, max_tokens: int, chunk_size: int = DEFAULT_CHUNK_SIZE
) -> Iterator[list[int]]:
    """Chunk documents and yield fixed-size token chunks from a JSONL corpus file."""
    corpus_path = Path(path)
    if not corpus_path.is_file():
        raise ValueError(f"External ngram corpus path does not exist: {path}")
    if tokenizer is None:
        raise ValueError("A tokenizer is required to load an external ngram corpus.")
    if max_tokens <= 0:
        raise ValueError("External ngram corpus max tokens must be positive.")

    total_tokens = 0
    has_previous_doc = False
    with corpus_path.open("r", encoding="utf-8") as f:
        for line_no, line in enumerate(f, start=1):
            if not line.strip():
                continue

            try:
                record = json.loads(line)
            except json.JSONDecodeError as e:
                raise ValueError(
                    f"Invalid JSON in external ngram corpus at line {line_no}: {e.msg}"
                ) from e

View on GitHub (pinned to 0132848349)

Solutions

  1. Verify the path exists and is a file: os.path.isfile(path)
  2. Use an absolute path (os.path.abspath / Path.resolve()) so it is independent of CWD
  3. If running in Docker/k8s, confirm the corpus file is actually mounted into the container

Example fix

# before
manager.add_external_corpus("data/corpus.jsonl", tokenizer=tok, max_tokens=100_000)
# after
from pathlib import Path
p = Path("data/corpus.jsonl").resolve()
assert p.is_file(), f"corpus missing: {p}"
manager.add_external_corpus(str(p), tokenizer=tok, max_tokens=100_000)
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path
corpus = Path(path).resolve()
if not corpus.is_file():
    raise FileNotFoundError(f"corpus not found: {corpus}")
add_external_corpus(str(corpus), tokenizer=tok, max_tokens=n)

Try / catch

try:
    add_external_corpus(path, tok, n)
except ValueError as e:
    if "does not exist" in str(e):
        log.warning("corpus missing, skipping: %s", path)
    else:
        raise

Prevention

When it happens

Trigger: Calling add_external_corpus or constructing the corpus loader with a path that does not exist, points to a directory, or is misspelled; also triggered directly in tests test_external_corpus_iterator_streams_documents / test_external_corpus_iterator_rejects_oversized_corpus.

Common situations: Relative path resolved against a different working directory, mount missing in a container, typo in the corpus file argument, or passing a directory instead of the .jsonl file.

Related errors


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