Repository navigation
fix(think_scrubber): recover final answer from unclosed <think> block #53391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
samaidev
wants to merge
1
commit into
NousResearch:main
Choose a base branch
from
samaidev:fix/think-scrubber-recover-unclosed-final-answer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -192,6 +192,61 @@ def test_flush_on_empty_scrubber(self) -> None: | |
| s = StreamingThinkScrubber() | ||
| assert s.flush() == "" | ||
|
|
||
| def test_flush_recovers_final_answer_after_newline_in_unclosed_block(self) -> None: | ||
| """Unclosed <think> with reasoning + newline + final answer. | ||
|
|
||
| Some OpenAI-compatible LLM gateways (e.g. relays fronting | ||
| MiniMax-M1 / Step-3.7-Flash / Intern-S2-Preview) inline the | ||
| model's reasoning AND the final answer inside ``delta.content`` | ||
| wrapped in a single ``<think>`` open tag with NO matching | ||
| ``</think>`` close. The model intends: | ||
|
|
||
| reasoning = "The user is asking for 2+2. The answer is 4." | ||
| final_answer = "4" | ||
|
|
||
| …and streams it as a single content sequence with the answer | ||
| on the line after the reasoning prose. flush() should recover | ||
| the final answer rather than discarding it. | ||
| """ | ||
| s = StreamingThinkScrubber() | ||
| deltas = ["<think>The user is asking for 2+2. The answer is 4.\n4"] | ||
| assert _drive(s, deltas) == "4" | ||
|
|
||
| def test_flush_recovers_final_answer_split_across_deltas(self) -> None: | ||
| """Same recovery, but reasoning and answer arrive in separate deltas.""" | ||
| s = StreamingThinkScrubber() | ||
| deltas = [ | ||
| "<think>The user is asking for 2+2.", | ||
| " The answer is 4.\n", | ||
| "4", | ||
| ] | ||
| assert _drive(s, deltas) == "4" | ||
|
|
||
| def test_flush_recovers_final_answer_multiline_reasoning(self) -> None: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add the inverse regression here: an unclosed multiline reasoning-only trace must still return empty. The current tests establish recovery examples but do not protect the scrubber's existing no-reasoning-leak guarantee. |
||
| """Multi-line reasoning followed by the final answer. | ||
|
|
||
| The recovery picks the LAST newline, so multi-line reasoning | ||
| prose is correctly stripped and only the final answer is | ||
| emitted. | ||
| """ | ||
| s = StreamingThinkScrubber() | ||
| deltas = [ | ||
| "<think>Let me think about this.\n", | ||
| "2+2 is definitely 4.\n", | ||
| "I'm confident the answer is 4.\n", | ||
| "4", | ||
| ] | ||
| assert _drive(s, deltas) == "4" | ||
|
|
||
| def test_flush_opt_out_recovers_unclosed_final_answer(self) -> None: | ||
| """Plugins can opt out of the recovery by setting | ||
| ``recover_unclosed_final_answer=False`` on the scrubber instance. | ||
| """ | ||
| s = StreamingThinkScrubber() | ||
| s.recover_unclosed_final_answer = False | ||
| deltas = ["<think>reasoning\nfinal answer"] | ||
| assert _drive(s, deltas) == "" | ||
|
|
||
|
|
||
| class TestRealisticStreaming: | ||
| """Character-by-character streaming must work as well as larger chunks.""" | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A newline does not establish that the following text is a final answer: an interrupted ordinary multiline `` trace has the identical shape. This would expose its last reasoning line, contrary to the existing strict non-leak contract. Please gate recovery on a signal that can distinguish this gateway format, or retain generic discard.