Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

serve: validate stop in Responses and count_tokens; tests for stop in --batch and after a thinking budget (#454)

Niko12210ad6e8ac5458
diff --git a/serve/test_parallel.py b/serve/test_parallel.py
index a970fffae..6e1400123 100644
--- a/serve/test_parallel.py
+++ b/serve/test_parallel.py
@@ -234,6 +234,18 @@ class ParallelService(unittest.TestCase):
self.assertEqual(self.engine.batch, 2)
self.assertEqual(self.get("/v1/status")["concurrency"]["serving"], 2)
+ def test_stop_strings_in_a_batch_slot(self):
+ """#454: a stop string cuts the answer in --batch mode too, and the slot is freed for the next request."""
+ self.start(2)
+ body = {"messages": [{"role": "user", "content": "hi LONGREPLY"}], "max_tokens": 64, "reasoning_effort": "none",
+ "stop": ["la la"]}
+ req = urllib.request.Request(self.base + "/v1/chat/completions", data=json.dumps(body).encode(),
+ headers={"Content-Type": "application/json"})
+ with urllib.request.urlopen(req, timeout=60) as r:
+ c = json.loads(r.read().decode())["choices"][0]
+ self.assertEqual((c["message"]["content"], c["finish_reason"]), ("ok, ", "stop"))
+ self.assertEqual(self.chat("again")["choices"][0]["message"]["content"], "ok, done.")
+
def test_engine_turns_batching_off(self):
self.start(4, fit=0)
self.assertEqual(self.engine.batch, 0)
diff --git a/serve/test_server.py b/serve/test_server.py
index 266df8e92..77b1df996 100644
--- a/serve/test_server.py
+++ b/serve/test_server.py
@@ -2134,6 +2134,13 @@ class ThinkingBudget(unittest.TestCase):
self.assertEqual(b["usage"]["completion_tokens"], 20 + len(extra) + len(ThinkingEngine.ANSWER) + 1)
self.assertEqual(b["usage"]["prompt_tokens"], len(first))
+ def test_a_stop_string_is_found_in_the_answer_after_the_wrap_up(self):
+ """#454: the continuation after the thinking budget is cut at a stop string too."""
+ code, b = self.openai(reasoning_budget_tokens=20, stop=["answer is"])
+ self.assertEqual(code, 200, b)
+ msg = b["choices"][0]["message"]
+ self.assertEqual((msg["content"], b["choices"][0]["finish_reason"]), ("The ", "stop"))
+
def test_anthropic_stream(self):
from serve.server import REASONING_WRAP_UP
code, raw = self.post("/v1/messages", {"model": "m", "max_tokens": 400, "stream": True,