Skip to content

Commit 2828b32

Browse files
committed
add tests for reasoning budget enforcement
1 parent 9c82848 commit 2828b32

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
import pytest
3+
4+
from utils import ServerPreset, ServerProcess
5+
6+
server: ServerProcess
7+
8+
9+
@pytest.fixture(autouse=True)
10+
def create_server():
11+
global server
12+
server = ServerPreset.tinyllama2()
13+
server.jinja = True
14+
server.reasoning_budget = 1
15+
server.chat_template_file = "../../../models/templates/deepseek-ai-DeepSeek-R1-Distill-Qwen-32B.jinja"
16+
server.reasoning_force_close_message = "!!!ABORT REASONING"
17+
server.start()
18+
yield
19+
server.stop()
20+
21+
22+
def test_reasoning_budget_forces_close():
23+
"""Ensure reasoning budget triggers forced close injection with end tag."""
24+
res = server.make_request("POST", "/v1/chat/completions", data={
25+
"model": server.model_alias or "test",
26+
"messages": [
27+
{"role": "user", "content": "Tell me a short story."},
28+
],
29+
"max_tokens": 32,
30+
})
31+
32+
assert res.status_code == 200
33+
body = res.body
34+
assert "choices" in body and body["choices"], "no choices returned"
35+
36+
message = body["choices"][0]["message"]
37+
reasoning_content = message.get("reasoning_content", "")
38+
39+
assert server.reasoning_force_close_message in reasoning_content, "reasoning force close message not found in reasoning content"
40+
41+
def test_reasoning_custom_budget():
42+
"""Ensure reasoning budget triggers forced close injection with end tag."""
43+
res = server.make_request("POST", "/v1/chat/completions", data={
44+
"model": server.model_alias or "test",
45+
"messages": [
46+
{"role": "user", "content": "Tell me a short story."},
47+
],
48+
"max_tokens": 32,
49+
"thinking_budget_tokens": 5
50+
})
51+
52+
assert res.status_code == 200
53+
body = res.body
54+
assert "choices" in body and body["choices"], "no choices returned"
55+
56+
message = body["choices"][0]["message"]
57+
reasoning_content = message.get("reasoning_content", "")
58+
59+
reasoning_before_abort = reasoning_content.split(server.reasoning_force_close_message)[0]
60+
assert len(reasoning_before_abort.split()) > 1, "reasoning content too short before force close"
61+
62+
assert server.reasoning_force_close_message in reasoning_content, "reasoning force close message not found in reasoning content"

tools/server/tests/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class ServerProcess:
9595
jinja: bool | None = None
9696
reasoning_format: Literal['deepseek', 'none', 'nothink'] | None = None
9797
reasoning_budget: int | None = None
98+
reasoning_force_close_message: str | None = None
9899
chat_template: str | None = None
99100
chat_template_file: str | None = None
100101
server_path: str | None = None
@@ -222,6 +223,8 @@ def start(self, timeout_seconds: int | None = DEFAULT_HTTP_TIMEOUT) -> None:
222223
server_args.extend(("--reasoning-format", self.reasoning_format))
223224
if self.reasoning_budget is not None:
224225
server_args.extend(("--reasoning-budget", self.reasoning_budget))
226+
if self.reasoning_force_close_message is not None:
227+
server_args.extend(("--reasoning-force-close-message", self.reasoning_force_close_message))
225228
if self.chat_template:
226229
server_args.extend(["--chat-template", self.chat_template])
227230
if self.chat_template_file:

0 commit comments

Comments
 (0)