Skip to content

fix: strip all reasoning blocks in removeReasoningContent, not just the first - #243

Open
Ayush7614 wants to merge 1 commit into
AtomicBot-ai:mainfrom
Ayush7614:fix/reasoning-strip-all-think-blocks
Open

fix: strip all reasoning blocks in removeReasoningContent, not just the first#243
Ayush7614 wants to merge 1 commit into
AtomicBot-ai:mainfrom
Ayush7614:fix/reasoning-strip-all-think-blocks

Conversation

@Ayush7614

Copy link
Copy Markdown
Contributor

What

removeReasoningContent (used by useTokensCount to compute what gets sent to the model) only removed the first complete thinking… response block and completely ignored unterminated blocks:

  • A model can emit several reasoning spans across one turn — only the first was stripped, the rest leaked.
  • A stream cut off mid-reasoning leaves an unterminated thinking… block with no closing tag — it was never removed at all.

Both cases meant reasoning content leaked into the token count and, potentially, into the prompt sent to the model.

Fix

  • Strip every complete thinking… response block (global replace) instead of the first match.
  • Strip a trailing unterminated thinking… block.
  • Apply the same two rules to the DeepSeek <|channel|>analysis<|message|>…<|start|>assistant<|channel|>final<|message|> format.

Verification

  • New test suite (web-app/src/utils/__tests__/reasoning.test.ts, 12 tests) covering single / multiple / unterminated blocks for both formats plus extractReasoningFromMessage edge cases.
  • tsc -b clean, eslint 0 errors, full utils suite 67/67 pass.

…he first

removeReasoningContent only removed the first complete  thinking… response
block and ignored unterminated blocks. A model can emit several reasoning
spans across one turn, and a stream cut off mid-reasoning leaves an
unterminated block — both cases leaked reasoning into the token count and,
potentially, the prompt sent to the model.

Now:
- strips every complete  thinking… response block (global replace)
- strips a trailing unterminated  thinking block
- applies the same treatment to the DeepSeek <|channel|>analysis…final format

Adds a test suite covering single/multiple/unterminated blocks for both
formats, plus extractReasoningFromMessage edge cases.
@Ayush7614
Ayush7614 requested a review from Vect0rM as a code owner August 19, 2026 10:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant