Reasoning Tag Filtering#
Dify detects and handles <think>ā¦</think> reasoning tags emitted by chain-of-thought models (e.g., DeepSeek-R1, QwQ) across several layers of the stack. The behavior differs significantly between Workflow/Chatflow components ā which expose a configurable reasoning_format ā and Easy-UI apps (Chat, Completion, Agent), which have no tag separation: <think> blocks pass through as raw answer text in Service API responses.
Modes and Scope#
The reasoning_format setting is available on Workflow LLM nodes and Workflow Agent nodes only. Easy-UI apps have no equivalent.
| Mode | Behavior | Default |
|---|---|---|
tagged | Tags pass through unchanged; downstream nodes see raw model output | ā Yes |
separated | Tags stripped from answer stream; reasoning content surfaces as reasoning_content field | No |
tagged is the default in both LLMNodeData and AgentNodeData to preserve backward compatibility. The UI toggle lives in reasoning-format-config.tsx, and the type is declared as reasoning_format?: 'tagged' | 'separated' on LLMNodeType .
Knowledge-base summaries always use split_reasoning(..., "separated") in ParagraphIndexProcessor.generate_summary_from_inputs(), independently of the Workflow setting. <think> blocks are removed from the returned summary, and the extracted reasoning is discarded. Filtering handles case-insensitive tags, tag attributes, multiple blocks, and trailing unclosed blocks; output containing only reasoning yields an empty summary. The original LLMUsage object is preserved.
LLM Node: Post-Stream Tag Splitting#
The core splitting logic is graphon.nodes.llm.reasoning.split_reasoning ā an external library function invoked after the stream completes . It runs a case-insensitive regex over the full completed text:
separatedmode: All<think>blocks are stripped from the answer; their inner content is returned asreasoning_contentonLLMResultand propagated throughModelInvokeCompletedEvent.taggedmode: Text is returned unchanged;reasoning_contentis empty.
During streaming, both modes forward raw chunks ā including <think> tags ā to the frontend so the live "thinking" panel continues to work. The post-stream split only affects values exposed to downstream workflow nodes .
The reasoning_content output variable is declared in panel-output-section.tsx, making it wireable to downstream nodes regardless of mode.
Agent Node: Streaming State Machine#
Agent nodes use ThinkStreamState from think_tags.py via message_transformer.py . The tracker is stateful across chunks, correctly reassembling tags split across multiple streamed segments (e.g., "<thi" + "nk>ā¦</think>"). Added in PR #38055.
close_if_open() is called at three points to prevent unclosed tags from leaking into subsequent content:
- Before emitting a tool log or link message.
- Before emitting a nested
<think>tag (when real content has appeared since the prior open tag). - At stream completion.
In separated mode, text chunks are routed through ThinkStreamState: clean text emits as StreamChunkEvent, detected reasoning as StreamReasoningEvent. After stream completion, text_filter.finalize() flushes any buffered partial tags.
Event and Pipeline Architecture#
Reasoning content travels on its own event channel, never mixed with the answer stream :
QueueReasoningChunkEventā internal queue event; carriesreasoning: str,from_node_id, andis_final.ReasoningChunkStreamResponseā SSE wrapper sent to clients; parallel totext_chunk, emitted only inseparatedmode.- Both
advanced_chatandworkflowpipeline files implement_handle_reasoning_chunk_event, persisting terminalreasoning_contentper LLM node ID intask_state.metadata.reasoning[node_id]. Seegenerate_task_pipeline.py.
LLM/Agent node
āā raw chunks āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāŗ frontend (live thinking panel)
āā [post-stream or per-chunk split]
āā answer text ā QueueLLMChunkEvent ā text_chunk SSE
āā reasoning ā QueueReasoningChunkEvent ā reasoning_chunk SSE (separated mode only)
Easy-UI Apps (No Tag Separation)#
EasyUIBasedGenerateTaskPipeline ā used by Chat, Completion, and Agent apps ā does not split or strip <think> tags . LLM chunks are extracted as raw text and appended to the accumulating answer; <think> blocks appear verbatim in the final stored answer and in Service API responses . The blocking response path takes its answer directly from accumulated message text with no tag filtering .
Easy-UI Agent apps use a separate AgentThoughtStreamResponse path for structured tool-thinking events, but this is unrelated to <think> tag parsing.
Frontend Tag Rendering#
markdown-utils.ts applies closeUnclosedThinkTags before converting <think> blocks to <details data-think> markdown elements. This repair step ensures previously stored or tagged-mode output renders as valid thinking collapsibles even when the closing </think> was dropped .
Special Case: Structured Output Parsing#
DeepSeek-R1 and similar models sometimes wrap JSON output in <think>\n\n</think>\n. The structured output parser in structured_output.py handles this: when the response begins with <think> and json_repair.loads() returns a list of candidates, the parser removes the leading reasoning block(s) and reparses the remaining answer text. This keeps JSON objects inside the reasoning from being selected as the structured output. The reparsed result is only used if it still yields a dict or list; otherwise the original candidates are kept .
Key Source Files#
| File | Role |
|---|---|
api/core/workflow/nodes/llm/node.py | Invokes split_reasoning, emits ModelInvokeCompletedEvent with reasoning_content |
api/core/rag/index_processor/processor/paragraph_index_processor.py | Strips reasoning from knowledge-base summaries using split_reasoning(..., "separated") |
api/core/workflow/nodes/agent/message_transformer.py | Stream-level ThinkStreamState for Workflow Agent nodes |
api/core/workflow/nodes/agent/think_tags.py | ThinkStreamState class and tag normalization helpers |
api/core/app/entities/queue_entities.py | QueueReasoningChunkEvent definition |
api/core/app/entities/task_entities.py | ReasoningChunkStreamResponse definition |
api/core/app/apps/advanced_chat/generate_task_pipeline.py | Reasoning persistence and SSE forwarding |
api/core/app/task_pipeline/easy_ui_based_generate_task_pipeline.py | Easy-UI pipeline (no tag filtering) |
api/core/llm_generator/output_parser/structured_output.py | Leading reasoning block handling for structured output |
web/app/components/workflow/nodes/llm/components/reasoning-format-config.tsx | UI mode toggle |
web/app/components/base/markdown/markdown-utils.ts | Frontend closeUnclosedThinkTags preprocessing |
Related PRs: #23313 ā LLM node reasoning format Ā· #38055 ā Agent node reasoning tag separation Ā· #37460 ā Chatflow streaming thinking panel Ā· #37828 ā CLI/workflow run preview for separated mode