Skip to content

Python: Fix duplicate system message from instructions - #5051

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
hashwnath:fix/duplicate-system-message-5049
Apr 2, 2026
Merged

Python: Fix duplicate system message from instructions#5051
Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
hashwnath:fix/duplicate-system-message-5049

Conversation

@hashwnath

Copy link
Copy Markdown
Contributor

Summary

Fixes #5049

  • Root cause: prepend_instructions_to_messages() in _types.py blindly prepends instruction messages without checking if identical messages already exist at the head of the list. When instructions are injected by multiple layers (e.g., Agent passes instructions in options, and the chat client also prepends them), duplicate system messages are sent to the API.

  • Fix: Added deduplication logic to prepend_instructions_to_messages() that skips instructions which are already present as leading messages with matching role and text content. This is a centralized fix that benefits all chat clients (OpenAI, Ollama, Anthropic, etc.).

  • Changes:

Test plan

  • All 7 new tests pass
  • All 252 existing test_types.py tests pass (no regressions)
  • All existing test_prepare_options_with_instructions tests pass
  • ruff check and ruff format pass on all changed files
  • CI pipeline passes

Add deduplication to `prepend_instructions_to_messages()` to skip
instructions that are already present as leading messages with the
same role and text. This prevents duplicate system messages when
instructions are injected by multiple layers (e.g. Agent + chat client).

Fixes microsoft#5049
@markwallace-microsoft Mark Wallace (markwallace-microsoft) added the python Usage: [Issues, PRs], Target: Python label Apr 2, 2026
@markwallace-microsoft

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _types.py10829091%58, 67–68, 122, 127, 146, 148, 152, 156, 158, 160, 162, 180, 184, 210, 232, 237, 242, 246, 276, 687–688, 847–848, 1233, 1305, 1340, 1360, 1370, 1422, 1554–1556, 1746, 1849–1854, 1879, 1967, 1973–1975, 1980, 2071, 2083, 2106, 2361, 2385, 2480, 2729, 2935, 3004, 3015, 3017–3021, 3023, 3026–3034, 3044, 3248–3250, 3253–3255, 3259, 3264, 3268, 3352–3354, 3383, 3460–3464
TOTAL27001318788% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
5348 20 💤 0 ❌ 0 🔥 1m 25s ⏱️

Merged via the queue into microsoft:main with commit 7e8e9e3 Apr 2, 2026
32 checks passed
Ben Thomas (alliscode) pushed a commit to alliscode/agent-framework that referenced this pull request Apr 3, 2026
… (microsoft#5051)

Add deduplication to `prepend_instructions_to_messages()` to skip
instructions that are already present as leading messages with the
same role and text. This prevents duplicate system messages when
instructions are injected by multiple layers (e.g. Agent + chat client).

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

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: OpenAIChatClient: System message from instructions is duplicated in the messages sent to OpenAI API

4 participants