Has the model finished thinking? Which tokens the engine should read to decide, on a new turn and on a resumed call

PR #4979 Β· review thread C84 Β· code pinned at 26d9d01

Before
Both paths run the same spliced prompt through the model. They only differed in which tokens they used to compute the reasoning_ended flag. Native computed it from that spliced prompt, which still holds the model's old turns. Dynamo computed it from the chat template's render, before the splice. So they gave different answers in both cases below.
This PR
Commit 7e29dcc adds one helper, reasoning_ended. Both paths now run it on the template's tokens only.
Status
Case A (a new turn): fixed by this PR. Cases B and C (a resumed call): open, follow-up in #5177. In B, the change made native give Dynamo's wrong answer. In C, no value of the flag is right. No run today reaches either case (see the box at the end), so the review agreed to the follow-up.

Nine pages on this stack (#4905, #4979 and #5115), one question each. This one: which tokens decide whether the model is done thinking.

Background. A thinking model writes <think>…</think> and then its answer. Some calls force the answer into a shape: JSON for response_format, or a tool call for a structural tag. A grammar does that: at each step it blocks tokens that would break the shape. The thinking must not be blocked, so the grammar needs to know when thinking is over. Dotted words: hover or tap.
The value this page is about: reasoning_ended, one bool per call. The function is correct. The whole issue is which token ids it is given.

So the answer depends entirely on which tokens you hand the rule. Qwen3's chat template drops the thinking of every assistant turn that comes before the latest user message, so on a new user turn the template's tokens hold no <think> from earlier turns. (It keeps the thinking after a tool result, inside one user turn; see the note under the table.) Both paths run a spliced prompt that keeps them; the model sees the same tokens on both. Before this PR, native's vLLM serving computed the flag from that spliced prompt (old L617, vLLM serving.py). Dynamo read it from the template. The example below is one Qwen3 chat (thinking on, deepseek_r1 parser), as two cases.

One chat, two cases: what each path told the engine

How to read the pills: each pill is the reasoning_ended value the engine gets for turn 2. "Should be" is the correct value: True only if turn 2's own thinking is already written. The other pills are four points in time:

  1. Main, before #4979. Only native exists.
  2. At review: #4979 at 5c7fa69, when C84 was posted. Native is unchanged from main; Dynamo is new.
  3. Now: #4979 after the author's C84 fix, 7e29dcc (head 26d9d01). Both paths use the template's tokens.
  4. Proposed: read the template plus the saved tokens on the worker. Moved to the follow-up issue #5177; not written yet.

So: case A was wrong on native in β‘  and β‘‘, and is fixed in β‘’. Case B was right on native in β‘  and β‘‘, wrong on Dynamo in β‘‘, and is wrong on both in β‘’: the fix for A broke B on native. β‘£ makes B right. Case C is wrong in every state, β‘£ included: no value of the flag can resume a half-written answer. That is why the fix is one issue, not a one-line change here.

in the template's tokens in the spliced prompt, not the template saved before a checkpoint cut βœ“ matchesβœ— doesn't A Β· Turn 2 starts (asks for JSON): the model has not thought about turn 2 yet user: Paris temp? <think>check units</think> 18Β°C. user: Tokyo, as JSON assistant: β–Œ turn 1's thinking: Qwen3's template drops it (a new user message came after it); the spliced prompt keeps it is_reasoning_end(template's tokens) β†’ False. is_reasoning_end(spliced prompt) β†’ True: it finds turn 1's </think>. SHOULD BEβ‘  MAINβ‘‘ AT REVIEWβ‘’ NOW (7e29dcc)β‘£ PROPOSED nativenativeDynamoboth pathsboth paths False True βœ— True βœ— False βœ“ False βœ“ fixed False βœ“ same B Β· Turn 2 (tools, tool_choice: auto) thought, then a checkpoint cut it; it resumes turn 2 template, as in A <think>use the tool</think> βœ‚ cut resumes: <tool_call>…</tool_call> turn 2's own thinking, already written and saved; the resumed prompt ends with it is_reasoning_end(template only) β†’ False. is_reasoning_end(template + saved tokens) β†’ True: it finds turn 2's </think>. SHOULD BEβ‘  MAINβ‘‘ AT REVIEWβ‘’ NOW (7e29dcc)β‘£ PROPOSED nativenativeDynamoboth pathsboth paths True True βœ“ True βœ“ False βœ— False βœ— broke True βœ“ C Β· Same as B, but the cut lands partway into the answer (here: inside the tool call) turn 2 template, as in A <think>…</think><tool_call>{"name": "wea βœ‚ cut resumes: ther", …}</tool_call> The grammar always restarts from its first state, but the answer is already half written. No flag value fixes that. True: a JSON grammar would demand a fresh "{" and corrupt it; a tool-call grammar never sees <tool_call> again, so it checks nothing. False: the grammar waits for a </think> that never comes, so it checks nothing either. SHOULD BEβ‘  MAINβ‘‘ AT REVIEWβ‘’ NOW (7e29dcc)β‘£ PROPOSED nativenativeDynamoboth pathsboth paths none works True βœ— True βœ— False βœ— False βœ— True βœ— β‘  main, native only Β· β‘‘ #4979 at 5c7fa69, when C84 was posted Β· β‘’ #4979 after 7e29dcc Β· β‘£ C91's first ask, now in #5177

What True and False actually do to the reply. The flag only changes anything when the call has a grammar. Without one, the model writes the same reply either way.

Case and its grammarWith FalseWith True
A: turn 2 has response_format. The reply must be {"city": …, "temp": …}. βœ“ what should happen. The grammar is off while the model writes <think>which units for Tokyo…</think>. Once it sees </think>, the grammar turns on and forces the JSON. βœ— old native. The grammar is on from the first token, and the first token must start the JSON. <think> is blocked, so turn 2's reply has no thinking: just {"city": "Tokyo", …}. Training then learns from a reply the model was not allowed to think before.
B: resumed turn 2 with tools and tool_choice: auto. Its structural tag lets the model write anything until <tool_call>; after that, the arguments must match the tool's schema. βœ— now, on both paths. The grammar waits for a </think> in the new tokens. The model wrote that before the cut, so it never writes another, and the grammar never turns on. The tool call is written with nothing checking it. Usually it is still valid, but nothing guarantees it, and a bad one fails to parse. βœ“ old native, and the proposed fix. The grammar is on from the first resumed token. The model writes <tool_call>, and from there its arguments are forced to match the schema.
C: like B, but the cut lands partway into the answer (a half-written JSON, or inside a <tool_call>). βœ— The grammar waits for a </think> that never comes, so nothing checks the rest of the answer. βœ— The grammar restarts from its first state. A JSON grammar demands a fresh { after {"city": "To and corrupts the reply; Gym never cuts these calls, so this cannot happen today. A tool-call grammar never sees <tool_call> again and checks nothing.

Two notes. (1) In case A with only a structural tag (tools, auto), a wrong True does little harm: the tag allows free text before <tool_call>, <think> included. The harm is with response_format and a forced tool call, whose grammar starts at the first token. (2) After a tool result, still in the same user turn, Qwen3's template keeps the previous step's thinking. There the template's tokens also end in an old </think>, so the flag is True on both paths. That is how plain vLLM behaves too, and it is outside this PR.

What the author did (7e29dcc). reasoning_ended runs once on the template's tokens. Dynamo passes the result on. Native puts it in extra_args. Then take_reasoning_ended hands it to the engine in place of vLLM's own value. The test reasoning_not_ended_by_an_earlier_turn is case A: both paths hand the engine False. The review also suggested never cutting constrained calls. The author did not, for two good reasons. Gym already skips cutting a call with response_format or a forced tool. And a rule on the worker would stop every call with tools from being cut, since vLLM turns the structural tag on for all of them. That leaves case B: a call with tools and tool_choice: auto, which Gym still cuts.

C84 β€” render_pool.pyauthor's reply

What would fix B and C β€” proposed in #5177, not in the PR

Nothing below is implemented. The author asked to do the resumed case in a follow-up, and the review agreed (reply C91): no run today reaches it, and case C means the fix needs a small design, not one line. #5177 tracks it. For case B, the worker already has what it needs: the flag only matters when the engine has a reasoning parser, so the worker has that parser in exactly the cases where it matters.

Where it would land: RL only. start_capture (runs on the worker on both paths), the Dynamo adapter's generate call, plus one test. No Gym change: Gym already decides which calls to cut.

# in start_capture, on the worker, after the restore is resolved β€” case B values template_ids = [ …"user: Tokyo?"…, …"assistant:" ] # no <think> anywhere saved_ids = [ "<think>", "use", "the", "tool", "</think>" ] # turn 2's own tokens, before the cut if call_resumes_a_cut and engine_has_reasoning_parser: ended = parser.is_reasoning_end(template_ids + saved_ids) # last one is </think> β†’ True # native: request._template_reasoning_ended = ended # Dynamo: the adapter passes ended instead of wire["reasoning_ended"] # test: a deepseek_r1 call with tools, cut right after its </think>; # both paths must hand the engine True.

Case A does not change: a new turn has no saved tokens, so it still reads the template alone and gets False. Case C needs a choice instead of a value. JSON-grammar calls must stay uncut, as Gym's generation_cut_regenerate_reason already does. For a structural-tag call, either do not cut it once its <tool_call> has started, or accept that the rest of that call is unchecked. Tests: one parity case per cut point (inside the thinking, right after </think>, inside a tool call) with an engine reasoning parser and a grammar.

follow-up issue #5177reply C91 (opens for others once the review is published)

Do today's runs hit any of this? No, none of the four states. vLLM only reads reasoning_ended when the engine has a reasoning parser (structured_outputs_config.reasoning_parser, set from vllm_kwargs.reasoning_parser). Without one, should_fill_bitmask returns True and the grammar starts at the first token whatever the flag says. Every recipe here, Nano3 and Super3 included, sets its reasoning parser only under http_server_serving_chat_kwargs. That one splits the reply into thinking and answer after generation, and NeMo RL never passes it to the engine. The Gym structured-outputs data puts the JSON schema in the prompt text, not in response_format, and its tool rows use tool_choice: auto. So this is a bug waiting for the first run that sets an engine reasoning parser and sends a grammar. Case B also needs tools with a parser that uses a structural tag (hermes, qwen3_engine, …), and a checkpoint that cuts the call right after its </think>. One catch: even True only helps if the cut lands before the tool call starts. Cut in the middle of <tool_call>, and the restarted grammar never sees the tag's start, so it checks nothing on either answer. The real choice is not about a big bug. It is whether this PR should change native's behavior for case B (before: True, now: False) or leave that for a follow-up.