Skip to content

fix: close unclosed think tags around agent tool calls - #41764

Merged
crazywoola merged 2 commits into
langgenius:mainfrom
Souravrajvi0:feat/close-unclosed-think-tags-65e5
Sep 4, 2026
Merged

crazywoola merged 2 commits into
langgenius:mainfrom
Souravrajvi0:feat/close-unclosed-think-tags-65e5

Conversation

@Souravrajvi0

Copy link
Copy Markdown
Contributor

Fixes #41558

Summary

Reasoning models (GLM-5.3 and similar) wrap chain-of-thought in <think> tags. A tool call often interrupts generation before </think> is emitted, so a later <think> block or the answer stays nested in the still-open tag. Markdown then turns that into an unclosed <details data-think>, and the reply is rendered as thinking.

This PR adds a fallback that inserts the missing close tag:

  • Workflow Agent node: close dangling <think> when a tool log (or link) interrupts the stream, when a new <think> starts after real content, and at stream end.
  • Markdown preprocessor: the same nested-open / trailing-close repair so already stored or LLM tagged output still renders as valid think blocks.

Screenshots

Before After
Reply nested in an unclosed thinking block when </think> is dropped around a tool call Think blocks close at the tool-call / nested-open boundary so the answer renders outside thinking

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and vp staged (frontend) to appease the lint gods

Reasoning models often omit </think> when a tool call interrupts the stream, so a later think block or the answer is nested in thinking HTML. Close dangling tags on tool logs, nested opens, and stream end, and apply the same fallback in markdown preprocessing.
@github-actions github-actions Bot added the web This relates to changes on the web. label Sep 3, 2026
@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 62.79% 62.79% +0.00%
Strict coverage 62.39% 62.39% +0.00%
Typed symbols 44,066 44,080 +14
Untyped symbols 26,285 26,289 +4
Modules 3315 3316 +1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

web This relates to changes on the web.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Workflow miss </think>

2 participants