serve: accept the three thinking-budget field names (pi's thinkingTokenBudgetField) as aliases

pi's models.schema.json only allows the compat.thinkingTokenBudgetField values
thinking_token_budget (vLLM), thinking_budget (Qwen/DashScope/SGLang) and
thinking_budget_tokens (llama.cpp), but the #123 budget was only readable from
reasoning_budget_tokens, so a pi client with any of those three configured sent
a budget the server silently ignored (the model thought its full length instead
of being wrapped up at N tokens).

The request entry (_post, after begin_request, before dispatch) now aliases
whichever of the three names the request sends to reasoning_budget_tokens, on the
three chat routes: the request's own reasoning_budget_tokens wins over all of them,
and if a client sends several aliases, the first of thinking_token_budget /
thinking_budget / thinking_budget_tokens does (deterministic).  The value takes the
existing whole-number validation (a bad alias is the same 400).  Docs: DETAILS.md's
budget bullet names the three aliases.

Rebased onto e88ee39d: do_POST's body moved to _post and the req parse now happens
after reserve_inference, but the insertion point (begin_request, before dispatch)
and the budget's read path (Service.reasoning_budget at request time) are
unchanged, so the patch applies verbatim.

Checked on e88ee39d + this commit (the /home/hsz/Strata checkout, Python 3.13.5,
Linux x86-64, 2x RTX 2080 Ti, Qwen3.8-Flash-Next IQ3_S): serve.test_server
.ThinkingBudget's 4 new alias tests - red before (on e88ee39d the alias-wrap test
and the bad-alias-400 test fail because the aliases are silently ignored; the two
precedence tests pass since the native field already wins when the aliases are
ignored) / green after (23 OK); the full serve suite with CI's command
(.venv/bin/python -m unittest discover -s serve -p 'test_*.py', 885 OK, 14
skipped); and live requests on :4000 (strata serve --profile, the patched tree) -
with a 20-token budget each of the three names wraps the thinking at 20 (wrap-up
marker, answer after, finish stop) exactly like the native field, whose response
shape is unchanged, and a bad alias (1.5) is the same 400 naming
reasoning_budget_tokens.
This commit is contained in:
shizhx committed 2026-10-20 11:22:09 +08:00
1 parent e88ee39d1d
commit 2b3364653c
3 files changed
+49 -1

No files matched your search

+5 -1
View File
@@ -892,7 +892,11 @@ print(r.choices[0].message.content)
thinking at N tokens: when it gets there the server ends it with a short wrap-up line and `</think>`, and the model
answers from there (the engine continues from what it already holds, so nothing is read again). The wrap-up is
part of the thinking the client sees and counts as output tokens. `"reasoning_budget_tokens": N` in
`strata-<model>.json` sets it for every request; a request's own value wins, and `0` means no budget. Off by default;
`strata-<model>.json` sets it for every request; a request's own value wins, and `0` means no budget. A request may
also send one of the other providers' names for the same budget - `"thinking_token_budget": N` (vLLM),
`"thinking_budget": N` (Qwen) or `"thinking_budget_tokens": N` (llama.cpp), what pi's `thinkingTokenBudgetField`
sends; `reasoning_budget_tokens` wins over them, and if several of the aliases are sent, the first of the three does.
Off by default;
Anthropic's `"thinking": {"budget_tokens": N}` chooses the level, as above, and since 0.1.45 is also the budget (a
`reasoning_budget_tokens` in the same request wins).
- **max_tokens counts the thinking, so the answer keeps its room (0.1.45).** A client that caps the whole reply (the
+9
View File
@@ -6253,6 +6253,15 @@ def make_handler(svc: Service):
return
if path in ("/v1/chat/completions", "/v1/messages", "/v1/responses"):
self.record = svc.begin_request(path, req)
# thinking_token_budget (vLLM), thinking_budget (Qwen) and thinking_budget_tokens (llama.cpp):
# other providers' names for #123's thinking budget (pi's compat.thinkingTokenBudgetField
# sends one of them). The request's own reasoning_budget_tokens wins; if a client sends
# several of the aliases, the first of the three above does.
if req.get("reasoning_budget_tokens") is None:
for alias in ("thinking_token_budget", "thinking_budget", "thinking_budget_tokens"):
if req.get(alias) is not None:
req["reasoning_budget_tokens"] = req.pop(alias)
break
if path == "/v1/responses":
self._responses(req)
elif path.startswith("/slots/"):
+35
View File
@@ -4068,6 +4068,41 @@ 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_the_budget_alias_field_names(self):
"""pi's thinkingTokenBudgetField can be thinking_token_budget (vLLM), thinking_budget (Qwen)
or thinking_budget_tokens (llama.cpp): all three are honoured like reasoning_budget_tokens."""
from serve.server import REASONING_WRAP_UP
wrap = REASONING_WRAP_UP.split("</think>")[0]
for field in ("thinking_token_budget", "thinking_budget", "thinking_budget_tokens"):
code, b = self.openai(**{field: 20})
self.assertEqual(code, 200, (field, b))
msg = b["choices"][0]["message"]
self.assertEqual(msg["reasoning_content"], ThinkingEngine.THOUGHT[:20] + wrap, field)
self.assertEqual(msg["content"], ThinkingEngine.ANSWER, field)
self.assertEqual(b["choices"][0]["finish_reason"], "stop", field)
self.assertEqual(len(self.engine.prompts), 6) # the wrap-up continuation, once per alias
def test_the_request_s_own_budget_wins_over_the_aliases(self):
code, b = self.openai(reasoning_budget_tokens=10_000, thinking_token_budget=20,
thinking_budget=20, thinking_budget_tokens=20)
self.assertEqual(code, 200, b)
self.assertEqual(b["choices"][0]["message"]["reasoning_content"], ThinkingEngine.THOUGHT)
self.assertEqual(len(self.engine.prompts), 1)
def test_the_alias_field_order_wins(self):
"""Several aliases at once: the first of thinking_token_budget / thinking_budget /
thinking_budget_tokens does, so a client that sends a mix gets a deterministic budget."""
code, b = self.openai(thinking_budget=10_000, thinking_budget_tokens=20)
self.assertEqual(code, 200, b)
self.assertEqual(b["choices"][0]["message"]["reasoning_content"], ThinkingEngine.THOUGHT)
self.assertEqual(len(self.engine.prompts), 1)
def test_a_bad_alias_is_a_400(self):
for field in ("thinking_token_budget", "thinking_budget", "thinking_budget_tokens"):
for bad in (1.5, "20", True):
code, b = self.openai(**{field: bad})
self.assertEqual(code, 400, (field, bad, b))
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"])