Skip to content

fix(agent): keep system messages first - #39372

Open
zcxGGmu wants to merge 2 commits into
langgenius:mainfrom
zcxGGmu:fix/qwen-system-message-order
Open

fix(agent): keep system messages first#39372
zcxGGmu wants to merge 2 commits into
langgenius:mainfrom
zcxGGmu:fix/qwen-system-message-order

Conversation

@zcxGGmu

@zcxGGmu zcxGGmu commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Fixes System message must be at the beginning #39275
  • Ensure Dify Agent sends all system messages before user, assistant, and tool history when adapting pydantic-ai requests for plugin-daemon LLM calls.
  • Preserve existing message order within system and non-system groups while keeping runtime instruction messages in the leading system block.
  • Add a regression test for request payload ordering with prior history and a current system prompt.

Testing

  • uv run --project dify-agent --extra server pytest dify-agent/tests/local/dify_agent/adapters/llm/test_model.py::DifyLLMAdapterModelTests::test_request_sends_all_system_messages_before_history -q
  • uv run --project dify-agent --extra server pytest dify-agent/tests/local/dify_agent/adapters/llm/test_model.py -q
  • uv run --project dify-agent --extra server pytest dify-agent/tests/local/dify_agent/adapters/llm -q
  • uv run --project dify-agent --extra server python -m ruff format --check dify-agent/src/dify_agent/adapters/llm/model.py dify-agent/tests/local/dify_agent/adapters/llm/test_model.py
  • uv run --project dify-agent --extra server python -m ruff check dify-agent/src/dify_agent/adapters/llm/model.py dify-agent/tests/local/dify_agent/adapters/llm/test_model.py
  • uv run --project . --extra server basedpyright --level error src/dify_agent/adapters/llm/model.py tests/local/dify_agent/adapters/llm/test_model.py
  • git diff --check

Notes

  • Full uv run --project . --extra server basedpyright --level error src examples tests currently reports existing errors outside the touched LLM adapter files.

@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jul 21, 2026
@wuyingfeng1998

Copy link
Copy Markdown

Please review this as soon as possible, as this issue is significantly affecting usability.

@zcxGGmu

zcxGGmu commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for flagging the impact. The current checks are passing, and this is awaiting maintainer review.

@wuyingfeng1998

Copy link
Copy Markdown

Please review this as soon as possible, as this issue is significantly affecting usability.

@crazywoola @laipz8200

@HandSonic

Copy link
Copy Markdown
Contributor

Thanks for the initial fix direction! I believe sorting alone doesn't fully resolve #39275: this change can still emit multiple consecutive system messages (history system prompts + runtime instructions are both serialized as SystemPromptMessage), and the Qwen3.5/3.6 chat template guard that produces the 400 rejects any system message that is not exactly the first one — including a second adjacent one (see vllm-project/vllm#41114). Since the provider plugin and SDK pass prompt_messages through 1:1 without merging, the caller must merge everything into a single leading system message. I've opened #39704 with that approach and regression tests for the multi-system case — happy to fold it into this PR instead if you prefer.

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Jul 28, 2026
@zcxGGmu

zcxGGmu commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

Updated this PR to merge all system content into one leading message while preserving non-system message order. Added coverage for runtime instructions and system prompts accumulated across history.

Tested: uv run --project dify-agent --python 3.12 --extra server pytest dify-agent/tests/local/dify_agent/adapters/llm/test_model.py -q

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System message must be at the beginning

4 participants