#43331·dify

[Bug] Reasoning models leak <think> chain-of-thought into the Service API answer of Chat / Completion / Agent apps

Author: rogerwusCreated Oct 2, 2026Updated Oct 2, 2026
Labelsreview: low

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

  1. Create a Chat, Completion, or Agent app (the "easy UI" app types, i.e. not a Chatflow/Workflow app).
  2. Select a reasoning model that emits chain-of-thought inside the message content — e.g. DeepSeek deepseek-flash (V4.1 Flash) through the official langgenius/deepseek plugin, or any Qwen/GLM model that wraps reasoning in <think>.
  3. Make sure reasoning/thinking is enabled for that model in the app's model settings.
  4. Call the app through the Service API (POST /v1/chat-messages, response_mode=blocking or streaming) and inspect answer.
  5. (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 answer was reported in #37184 and fixed.
  • reasoning_content being 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:

  • answer starts 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 messages row, 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:

  1. The official DeepSeek plugin wraps reasoning into content as <think>\n<!--dify-deepseek-reasoning-->…</think> and additionally stores it in opaque_body["reasoning_content"] (plugins/models/deepseek/models/llm/llm.py).

  2. Workflow LLM nodes call graphon.nodes.llm.reasoning.split_reasoning() because the node carries reasoning_format. core/app/apps/advanced_chat/generate_task_pipeline.py also converts QueueReasoningChunkEvent into ReasoningChunkStreamResponse (added with #37184).

  3. 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 to llm_result.message.content and 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's llm_result as-is, so blocking responses and the stored message keep the tags.

    core/agent/fc_agent_runner.py (lines ~220-225) likewise concatenates chunk.delta.message.content into the final response without 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.