Skip to content

fix: handle missing opening <think> tag for deepseek-r1-671b#1034

Open
MaxwellCalkin wants to merge 1 commit intoItzCrazyKns:masterfrom
MaxwellCalkin:fix/handle-missing-think-open-tag
Open

fix: handle missing opening <think> tag for deepseek-r1-671b#1034
MaxwellCalkin wants to merge 1 commit intoItzCrazyKns:masterfrom
MaxwellCalkin:fix/handle-missing-think-open-tag

Conversation

@MaxwellCalkin
Copy link

@MaxwellCalkin MaxwellCalkin commented Mar 8, 2026

Summary

Fixes #810.

deepseek-r1-671b updated its chat template and now only emits a closing </think> tag at the end of its thinking output, without an opening <think> tag. The existing thinking-tag logic in useChat.tsx handled the reverse case (opening <think> without closing </think>, which occurs during streaming) but did not handle this case. As a result, the model's thinking content leaked into the main answer instead of being rendered in the ThinkBox component.

Changes

In src/lib/hooks/useChat.tsx, the thinking tag detection in the sections useMemo now:

  1. Always counts both opening and closing think tags (previously only counted them inside an if (processedText.includes('<think>')) guard)
  2. Prepends <think> when </think> is found without a matching opening tag, so the markdown-to-jsx parser can route the content to the ThinkBox component

This ensures models that omit the opening <think> tag (like deepseek-r1-671b) still have their thinking content rendered correctly in the collapsible ThinkBox, while the existing streaming behavior (closing tag added when opening tag is present but closing is missing) is preserved.

Test plan

  • Test with deepseek-r1-671b via custom OpenAI — thinking content should appear in the ThinkBox, not in the main answer
  • Test with deepseek-r1-0528 (which uses both <think> and </think>) — should continue working as before
  • Test with non-thinking models — no change in behavior (no think tags present)
  • Test streaming behavior — incomplete <think> without </think> should still get auto-closed

Summary by cubic

Fixes #810. Ensures models that only emit a closing (like deepseek-r1-671b) render their thinking in the ThinkBox instead of the main answer.

  • Bug Fixes
    • In src/lib/hooks/useChat.tsx, always count both <think> and </think> tags during parsing.
    • When </think> appears without a matching <think>, prepend <think> so the markdown parser routes content to the ThinkBox.
    • Preserve existing streaming behavior that auto-closes an open <think> without a closing tag.

Written for commit b688355. Summary will update on new commits.

deepseek-r1-671b only emits a closing think tag without an opening
think tag. The existing logic handled the reverse case (opening without
closing, during streaming) but not this one, causing thinking content to
leak into the main answer instead of being rendered in the ThinkBox.

Prepend think opening tag when think closing tag is found without a matching opening tag.

Fixes ItzCrazyKns#810
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

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.

deepseek-r1-671b only uses </think> at the end. This is not accounted for.

1 participant