[Bug] Reasoning models leak <think> chain-of-thought into the Service API answer of Chat / Completion / Agent apps
Self Checks
- I have read the Contributing Guide and Language Policy.
- This is only for bug report, if you would like to ask a question, please head to Discussions.
- I have searched for existing issues, including closed ones.
- I confirm that I am using English to submit this report.
- Please do not modify this template :) and fill in all the required fields.
Dify version
1.17.1 (self-built from the release tag) — the same code path is present on main.
Cloud or Self Hosted
Self Hosted (Docker)
Steps to reproduce
- Create a Chat, Completion, or Agent app (the "easy UI" app types, i.e. not a Chatflow/Workflow app).
- Select a reasoning model that emits chain-of-thought inside the message content — e.g. DeepSeek
deepseek-flash(V4.1 Flash) through the officiallanggenius/deepseekplugin, or any Qwen/GLM model that wraps reasoning in<think>. - Make sure reasoning/thinking is enabled for that model in the app's model settings.
- Call the app through the Service API (
POST /v1/chat-messages,response_mode=blockingorstreaming) and inspectanswer. - (Agent apps) Repeat with a Function Calling agent so the run goes through
QueueAgentMessageEvent.
✔️ Expected Behavior
answer contains only the model's final answer. Reasoning is either dropped or delivered on a separate channel. This is exactly what already happens for the other surfaces:
- Workflow LLM nodes have the node-level "Enable reasoning tag separation" setting (
reasoning_format = "separated"). - Chatflow reasoning leaking into
answerwas reported in #37184 and fixed. reasoning_contentbeing empty while reasoning sits in the text was reported in #36584 and fixed.
❌ Actual Behavior
For easy-UI apps there is no equivalent of reasoning_format, and the reasoning text is streamed straight into the user-visible answer.
Observed on a self-hosted 1.17.1 instance with deepseek-flash through the official langgenius/deepseek plugin:
answerstarts with<think>\n<!--dify-deepseek-reasoning-->…</think>followed by the real answer.- The number of leaked characters is not small: a single Agent app run returned ~63k characters of raw chain-of-thought in the response stream.
- The same content is written to the
messagesrow, so it is persisted and replayed in later turns. - Because the tag is a plain prefix, any client that expects structured output (
response_format=json_object) receives<think>…</think>{...}and fails to parse it.
Root cause
The reasoning text never gets separated for easy-UI apps:
The official DeepSeek plugin wraps reasoning into
contentas<think>\n<!--dify-deepseek-reasoning-->…</think>and additionally stores it inopaque_body["reasoning_content"](plugins/models/deepseek/models/llm/llm.py).Workflow LLM nodes call
graphon.nodes.llm.reasoning.split_reasoning()because the node carriesreasoning_format.core/app/apps/advanced_chat/generate_task_pipeline.pyalso convertsQueueReasoningChunkEventintoReasoningChunkStreamResponse(added with #37184).core/app/task_pipeline/easy_ui_based_generate_task_pipeline.py— the single output pipeline for chat, completion and agent apps — has neither. It appends_chunk_delta_text(chunk)directly tollm_result.message.contentand re-emits it verbatim:case QueueLLMChunkEvent()→current_content += delta_text+message_to_stream_response(answer=delta_text)case QueueAgentMessageEvent()→_agent_message_to_stream_response(answer=delta_text)case QueueStopEvent() | QueueMessageEndEvent()→ takes the runner'sllm_resultas-is, so blocking responses and the stored message keep the tags.
core/agent/fc_agent_runner.py(lines ~220-225) likewise concatenateschunk.delta.message.contentinto the finalresponsewithout stripping.
So the fix that landed for Chatflow/Worflow was not applied to the easy-UI surfaces, and users of Chat/Completion/Agent apps have no setting to work around it.
Impact
- Raw chain-of-thought is exposed to every Service API client and end user of the app.
- Stored messages and conversation history are polluted, which also inflates context for later turns.
- Structured-output apps (
response_format=json_object) break outright. - Turning reasoning off is the only current workaround, which defeats the purpose of using a reasoning model.
Notes
This is distinct from #38033 (which asked for a new toggle on the workflow Agent node and was closed as not planned): here the plumbing already exists (QueueReasoningChunkEvent + ReasoningChunkStreamResponse) and the only thing missing is that the easy-UI pipeline never uses it. The same reasoning text can be split off with no new user-facing setting.
I have a minimal fix ready and will link it here.
Source: langgenius/dify