{"record":{"id":"55251ccf4b8dc475","repo":"sgl-project/sglang","slug":"wrong-type-of-stop-in-sampling-parameters","errorCode":null,"errorMessage":"Wrong type of stop in sampling parameters.","messagePattern":"Wrong type of stop in sampling parameters\\.","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"python/sglang/lang/interpreter.py","lineNumber":570,"sourceCode":"                sampling_params.max_new_tokens, self.num_api_spec_tokens\n            )\n            sampling_params.stop = None\n            self.speculated_text, meta_info = self.backend.generate(\n                self, sampling_params=sampling_params\n            )\n\n        def find_stop():\n            if isinstance(stop, str):\n                return self.speculated_text.find(stop)\n            elif isinstance(stop, (tuple, list)):\n                pos = -1\n                for stop_str in stop:\n                    stop_pos = self.speculated_text.find(stop_str)\n                    if stop_pos != -1 and (pos == -1 or stop_pos < pos):\n                        pos = stop_pos\n                return pos\n            else:\n                raise Exception(\"Wrong type of stop in sampling parameters.\")\n\n        if stop is None:\n            if len(self.speculated_text) < max_new_tokens:\n                regen()\n            comp = self.speculated_text[:max_new_tokens]\n            self.speculated_text = self.speculated_text[max_new_tokens:]\n        elif isinstance(stop, (str, list, tuple)):\n            if self.speculated_text == \"\":\n                regen()\n            stop_pos = find_stop()\n            if stop_pos == -1:\n                stop_pos = min(\n                    sampling_params.max_new_tokens,\n                    len(self.speculated_text),\n                )\n            comp = self.speculated_text[:stop_pos]\n            self.speculated_text = self.speculated_text[stop_pos:]\n        else:","sourceCodeStart":552,"sourceCodeEnd":588,"githubUrl":"https://github.com/sgl-project/sglang/blob/0132848349585cfe6aae51c4941cbae872505f8a/python/sglang/lang/interpreter.py#L552-L588","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Normalize stop to a plain str or list[str] before passing sampling_params","Validate/cast incoming stop values (e.g. list(stop) for tuples) in your param-building code","Remove the stop key entirely if you don't need stop strings"],"exampleFix":"# before\nsp = {\"stop\": (\"\\n\\n\", \"User:\")}  # tuple -> raises\n# after\nsp = {\"stop\": [\"\\n\\n\", \"User:\"]}  # list[str]","handlingStrategy":"validation","validationCode":"def normalize_stop(sp: dict) -> dict:\n    stop = sp.get(\"stop\")\n    if isinstance(stop, (list, tuple)):\n        sp[\"stop\"] = [str(s) for s in stop]\n    elif stop is not None and not isinstance(stop, str):\n        del sp[\"stop\"]  # or raise\n    return sp","typeGuard":"def valid_stop(v) -> bool:\n    return v is None or isinstance(v, str) or (isinstance(v, list) and all(isinstance(s, str) for s in v))","tryCatchPattern":"try:\n    ...gen with sampling_params...\nexcept Exception as e:\n    if \"Wrong type of stop\" in str(e):\n        sampling_params[\"stop\"] = list(sampling_params[\"stop\"])\n    else:\n        raise","preventionTips":["Always build stop as list[str] in param dicts","Validate sampling params at the edge of your app (API handlers, config loaders)","Convert tuples/arrays from user input to list before passing"],"tags":["sampling-params","validation","stop-sequences"],"backgroundTag":"invalid-parameter-type","analyzedSha":"0132848349585cfe6aae51c4941cbae872505f8a","analyzedAt":"2026-08-28T05:10:05.995Z","schemaVersion":2},"datasetVersion":"2026-08-28T06:17:29.519Z"}