sgl-project/sglang · error · Exception

Wrong type of stop in sampling parameters.

Error message

Wrong type of stop in sampling parameters.

What it means

Raised in find_stop (speculative-generation lookahead) when sampling_params.stop is neither a string nor a list — the code only handles those two shapes. It indicates malformed stop configuration passed to a gen statement.

Source

Thrown at python/sglang/lang/interpreter.py:570

                sampling_params.max_new_tokens, self.num_api_spec_tokens
            )
            sampling_params.stop = None
            self.speculated_text, meta_info = self.backend.generate(
                self, sampling_params=sampling_params
            )

        def find_stop():
            if isinstance(stop, str):
                return self.speculated_text.find(stop)
            elif isinstance(stop, (tuple, list)):
                pos = -1
                for stop_str in stop:
                    stop_pos = self.speculated_text.find(stop_str)
                    if stop_pos != -1 and (pos == -1 or stop_pos < pos):
                        pos = stop_pos
                return pos
            else:
                raise Exception("Wrong type of stop in sampling parameters.")

        if stop is None:
            if len(self.speculated_text) < max_new_tokens:
                regen()
            comp = self.speculated_text[:max_new_tokens]
            self.speculated_text = self.speculated_text[max_new_tokens:]
        elif isinstance(stop, (str, list, tuple)):
            if self.speculated_text == "":
                regen()
            stop_pos = find_stop()
            if stop_pos == -1:
                stop_pos = min(
                    sampling_params.max_new_tokens,
                    len(self.speculated_text),
                )
            comp = self.speculated_text[:stop_pos]
            self.speculated_text = self.speculated_text[stop_pos:]
        else:

View on GitHub (pinned to 0132848349)

Solutions

  1. Normalize stop to a plain str or list[str] before passing sampling_params
  2. Validate/cast incoming stop values (e.g. list(stop) for tuples) in your param-building code
  3. Remove the stop key entirely if you don't need stop strings

Example fix

# before
sp = {"stop": ("\n\n", "User:")}  # tuple -> raises
# after
sp = {"stop": ["\n\n", "User:"]}  # list[str]
Defensive patterns

Strategy: validation

Validate before calling

def normalize_stop(sp: dict) -> dict:
    stop = sp.get("stop")
    if isinstance(stop, (list, tuple)):
        sp["stop"] = [str(s) for s in stop]
    elif stop is not None and not isinstance(stop, str):
        del sp["stop"]  # or raise
    return sp

Type guard

def valid_stop(v) -> bool:
    return v is None or isinstance(v, str) or (isinstance(v, list) and all(isinstance(s, str) for s in v))

Try / catch

try:
    ...gen with sampling_params...
except Exception as e:
    if "Wrong type of stop" in str(e):
        sampling_params["stop"] = list(sampling_params["stop"])
    else:
        raise

Prevention

When it happens

Trigger: Setting sampling_params={'stop': ...} with an int, tuple, dict, or other type; frameworks building sampling params dynamically producing a non-str/non-list stop value.

Common situations: Passing stop_words as a tuple or numpy array instead of list; config files parsed into unexpected types; copying OpenAI-style params where stop can be str|list|null but the value got wrapped incorrectly.

Related errors


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