Heuristic:AI QL Tuui Think Tag Separation
| Knowledge Sources | |
|---|---|
| Domains | LLM, Streaming, Parsing |
| Last Updated | 2026-10-24 20:38 GMT |
Overview
Separate reasoning content enclosed in <think>...</think> tags from regular content during streaming, accumulating each into distinct fields for display.
Description
Some LLMs (e.g., Qwen, DeepSeek) emit reasoning traces wrapped in <think> / </think> tags within the streamed content. The think-tag separation pattern accumulates content as it arrives; when a closing </think> tag is found, it extracts the text between the open and close tags into reasoning_content and keeps the remainder as content. Only the first occurrence of the closing tag is processed, as models typically fail to function properly with multiple closing tags. The open tag may be absent (in which case everything before the close tag is treated as reasoning).
Usage
Apply this heuristic when parsing streamed LLM responses from models that emit reasoning content in think tags. The symptom is raw <think> markup appearing in the chat UI instead of being separated into a reasoning panel.
The Insight (Rule of Thumb)
- Action: Accumulate raw content; on encountering
</think>, split intoreasoning_content(between tags) andcontent(after tags). - Value: Only the first
</think>occurrence is processed.THINK_OPENandTHINK_CLOSEconstants hold the tag strings. - Trade-off: Models that emit multiple think blocks will have only the first separated; subsequent think tags appear as raw content.
- Edge case: If
THINK_OPENis absent, reasoning content starts from index 0.
Reasoning
Reasoning models interleave chain-of-thought with the final answer. Displaying raw tags in the UI is confusing to users. Separating into two fields allows the UI to render reasoning in a collapsible panel and the answer in the main content area. The single-occurrence constraint is documented in the code comment: "the model typically fails to function properly when multiple THINK_CLOSE instances appear."
Code Evidence
Think tag constants and separation logic from `src/renderer/composables/chatCompletions.ts:55-56,448-464`:
const THINK_OPEN = '<think>'
const THINK_CLOSE = '</think>'
const parseMixedContent = (chunk: string, target: AssistantMessage) => {
// Only cases with a single occurrence of THINK_CLOSE in the response are considered,
// as the model typically fails to function properly when multiple THINK_CLOSE instances appear.
const data = (target.content ?? '') + chunk
const closeIdx = data.indexOf(THINK_CLOSE)
if (closeIdx === -1) {
target.content += chunk
} else {
const openIdx = data.indexOf(THINK_OPEN)
const divider = closeIdx + THINK_CLOSE.length
const afterThink = data.substring(divider)
const startIdx = openIdx < 0 ? 0 : openIdx + THINK_OPEN.length
target.reasoning_content += data.substring(startIdx, closeIdx).trim()
target.content = afterThink.trimStart()
}
}