Skip to content

fix(tongyi): strip historical thinking for qwen3.8-max - #4213

Merged
crazywoola merged 1 commit into
langgenius:mainfrom
ZhXZhao:bugfix/tongyi-think-leak
Nov 6, 2026
Merged

crazywoola merged 1 commit into
langgenius:mainfrom
ZhXZhao:bugfix/tongyi-think-leak

Conversation

@ZhXZhao

@ZhXZhao ZhXZhao commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #4212.
Related to langgenius/dify#44964; this PR addresses the confirmed Tongyi outbound-history reinjection path, not every reported stray-tag or pseudo-tool-call symptom.

For qwen3.8-max, preserve_thinking=False does not remove reasoning stored as ordinary assistant content. This change:

  • Derives strip_history_thinking from the effective parameters after normalization, restricted to model == "qwen3.8-max" and preserve_thinking is False.
  • Passes the policy explicitly through both MultiModalConversation.call and Generation.call history conversion paths. It does not depend on the current turn's enable_thinking.
  • Removes complete existing <think>\n...\n</think> protocol blocks from assistant strings, including multiple blocks interleaved with answer text, without trimming block-external whitespace.
  • Preserves structured tool calls, IDs, names, arguments, tool-result mapping, and the existing single-space fallback for empty assistant content. Original input messages and other roles are unchanged.
  • Leaves dify-agent, current-turn thinking capability, response/stream handling, and other Tongyi models unchanged.
  • Adds final-SDK-message assertions and explicitly opt-in live tests; bumps Tongyi from 0.2.26 to 0.2.27.

Explicit compatibility trade-off

Unmarked historical thinking and legitimate assistant prose with the exact same formatting cannot be reliably distinguished. Under this proposal, a complete, non-nested <think>\n...\n</think> example in assistant prose, including inside a Markdown code fence, is also removed from the next outbound request. This does not alter the original output or stored source history, but can remove a legitimate example from the next model's context. Maintainer review of this trade-off is requested; this is not presented as lossless provenance detection.

Inline <think>example</think>, normal tag mentions, incomplete/orphan tags, different-case/attribute variants, and escaped tags are retained. Matching does not cross another literal think tag to complete a malformed block. No orphan-tag cleanup or output-side filtering is introduced. Reliable source distinction would require trusted provenance or structured reasoning; merely adding a new marker would leave existing unmarked history unresolved.

This is separate from #3277, fixed by #3282: that issue was response-chunk content loss, whereas this PR changes outbound assistant history.

Release Notes

  • Prevent complete historical thinking blocks from being replayed as ordinary assistant text for qwen3.8-max when preserved thinking is disabled, including when current-turn thinking is off.
  • Preserve answer text outside those blocks, structured tool calls, and source history.
  • Compatibility note: exact-format <think>\n...\n</think> examples in assistant history are interpreted as thinking blocks and removed from subsequent requests, even inside code fences. Ordinary inline tag mentions and incomplete tags are unchanged.

Change Type

  • Documentation / non-plugin change
  • Non-LLM plugin (tools, extensions, datasource, etc.)
  • LLM plugin

Screenshots / Videos

Provider request-conversion change; no UI modification. The following reflects assertions at the SDK call boundary:

Before After
assistant.content = "<think>\nHistorical reasoning\n</think>Final answer" assistant.content = "Final answer"
Thinking-only assistant message with structured tool calls Same tool calls retained; content uses existing " " placeholder
Current turn enable_thinking=False still replays historical reasoning Historical protocol blocks removed independently of current thinking
Exact-format protocol example inside assistant prose/code fence Example removed from outbound history: documented compatibility limitation

For the registered vision route, the text is wrapped in [{"text": ...}]; the same policy applies before wrapping.

LLM Plugin Checklist

Areas affected by this change (check all that apply)
  • Message flow (system messages, user ↔ assistant turn-taking)
  • Tool interaction flow (multi-round usage, Agent App and Agent Node)
  • Multimodal input (images, PDFs, audio, video, etc.)
  • Multimodal output (images, audio, video, etc.)
  • Structured output (JSON, XML, etc.)
  • Token consumption metrics
  • Other LLM functionality (reasoning, grounding, prompt caching, etc.)
  • New models / model parameter fixes

The multimodal endpoint's assistant-text conversion is covered; image/video payload behavior is not changed. Tool-flow verification is at the plugin/API boundary, not a full Agent App/Agent Node UI run.

Version

  • Bumped top-level version in manifest.yaml (not the one under meta)
  • dify_plugin>=0.3.0,<0.11.0 is declared in pyproject.toml and locked in uv.lock (or kept in requirements.txt for legacy plugins without uv.lock) — SDK docs

Version: 0.2.26 → 0.2.27; meta.version remains unchanged. Dependencies are intentionally unchanged: the existing declaration is dify_plugin>=0.10.2 (without the template's upper bound), and the lockfile/test environment uses 0.10.2.

Testing

  • Local deployment — Dify version: not run as a full deployment
  • SaaS (cloud.dify.ai)
  • Standalone plugin: Python 3.12.9, dify_plugin==0.10.2, dashscope==1.27.4.
  • Offline Tongyi suite: 327 passed, 39 skipped, two dependency deprecation warnings. The 39 skips are 35 existing live invocation cases plus 4 opt-in live cases added here.
  • Added regression coverage: 101 offline cases across both SDK call routes. Captures final messages, normalized parameters, thinking on/off, tools and associated results, interleaved blocks, protocol ambiguity, unchanged non-target models, original-object preservation, and current reasoning output.
  • Test-first evidence: the initial regression matrix had 46 failures / 53 passes before the implementation, failing on expected outbound-history content assertions, and passed after it.
  • Real DashScope: 4 passed for fresh vs synthetic-history context × current thinking off/on. Each runs one tool-call → local marker result → final-answer round trip. The reasoning-enabled cases retain current reasoning output. Only synthetic data and a side-effect-free local marker tool are used.
  • Ruff for the added test file; git diff --check.

Commands from the repository root:

PYTHONPATH="$PWD/models/tongyi" env -u DASHSCOPE_API_KEY models/tongyi/.venv/bin/python -c 'import dify_plugin, pytest; raise SystemExit(pytest.main(["models/tongyi/tests", "-q", "--tb=short"]))'

RUN_TONGYI_THINKING_HISTORY_LIVE=1 models/tongyi/.venv/bin/python -c 'import dify_plugin, pytest; raise SystemExit(pytest.main(["models/tongyi/tests/test_llm_thinking_history.py", "-k", "live", "-q", "--tb=short", "-x"]))'

uvx --offline ruff check models/tongyi/tests/test_llm_thinking_history.py
git diff --check

The live command additionally requires DASHSCOPE_API_KEY in the environment and makes real billable API calls; live cases are skipped unless explicitly opted in. Importing dify_plugin first preserves the production gevent patch ordering, and PYTHONPATH lets the existing import-order subprocess test resolve the plugin package from the repository root.

Not verified: full Dify Agent App UI/database thought persistence, the original production conversation, or whether all reported stray tags/pseudo-tool outputs disappear. No such end-to-end claim is made. These are new-context vs synthetic-history comparisons on the patched plugin, not a production before/after experiment.

AI-assisted implementation and validation; executed test results and known limitations are reported above.

Fixes langgenius#4212. Limit cleanup to normalized preserve_thinking=false and preserve structured tools and source history. Document unmarked-protocol ambiguity in regression tests.
@crazywoola
crazywoola deployed to models/tongyi November 6, 2026 12:27 — with GitHub Actions Active
@crazywoola
crazywoola merged commit 18663b9 into langgenius:main Nov 6, 2026
3 checks passed

This branch was successfully deployed

1 active deployment
models/tongyi — 302f886c Deployed Nov 6, 2026 by crazywoola via test (models/tongyi) #2977
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tongyi qwen3.8-max replays thinking despite preserve_thinking=false

2 participants