fix(api): harden /v1/completions thinking_budget (validation, stream parity, tokenizer detection) on top of #1844#1821
Merged
Conversation
d6b4ac2 to
161eb75
Compare
The thinking_budget extension is accepted on /v1/chat/completions and mapped from budget_tokens on /v1/messages, but /v1/completions silently dropped it: CompletionRequest had no such field and neither completion path passed it to the engine. Raw completions are a natural fit for the budget — a prompt ending with an open think tag (e.g. "<think>\n") already passes the scheduler's needs_think_prefix gate. Add the field to CompletionRequest and thread it through the streaming and non-streaming completion paths via the existing _resolve_thinking_budget helper, so request-level and model-settings budgets behave exactly like chat.
…ith non-stream Two review findings on the completions budget path: - thinking_budget accepted any int; a negative value has no semantics anywhere in the enforcement chain and was silently treated as exhausted. Reject it at the API boundary (ge=0) on both OpenAI surfaces. - When a raw prompt opens a thinking block, the scheduler prepends its synthetic think opener to the first streamed chunk (chat streams rely on it to rebuild the reasoning block), so /v1/completions stream returned a marker the non-streaming path never exposes. Strip it once at the completions presentation layer: raw completions are a pure continuation of the prompt on both paths. Also replace the substring-count wiring guard with structural AST checks (handler -> engine call -> thinking_budget keyword), which survive reformatting and wrappers.
jundot#1844 (merged upstream) forwards thinking_budget on /v1/completions via a `**gen_kwargs` dict-unpack instead of an inline `thinking_budget=_resolve_thinking_budget(...)` keyword. After rebasing this branch on top of it, the source-level wiring guard only recognized the inline form and failed. Teach the AST check to also accept the dict-unpack pattern: the handler sets `gen_kwargs["thinking_budget"]` from the resolved value, then unpacks the dict into the engine call. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
161eb75 to
a033d37
Compare
Owner
|
Thanks for the follow-up. I checked the completions wiring and the stream/non-stream parity path against the scheduler's think-prefix detection, and the tokenizer-backed guard looks correct. The targeted thinking-budget tests and the broader completion/thinking/budget selection pass locally. This looks good to me, and I'm going to merge it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #1844. That PR already forwards
thinking_budgeton/v1/completions; this one hardens the remaining edges around that path.Raw completions can use a thinking budget when the prompt already opens a thinking block, for example by ending with
<think>\n. The remaining issues were: negative budgets were accepted, streaming completions could leak the scheduler's synthetic<think>\nopener, and the stream strip guard needed to mirror the scheduler's token-level detection instead of relying on raw text.Changes
thinking_budgetvalues on chat and completions.<think>\nopener from streamed raw completions so streaming and non-streaming responses match.</think>markers.Tests
Adds coverage for completions parsing/forwarding, negative-budget validation, stream prefix stripping, tokenizer-backed prompt detection, prompt-id reuse, disabled-thinking patterns, and the structural wiring guards for both completion paths.
Validation: targeted thinking-budget tests pass; broader
completion or thinking or budgetselection was re-run during review.