Python: Fix prepend_instructions_to_messages inverting instruction order on partial dedup by Sanjays2402 · Pull Request #8360 · microsoft/agent-framework · GitHub
Skip to content

Python: Fix prepend_instructions_to_messages inverting instruction order on partial dedup - #8360

Closed
Sanjay Santhanam (Sanjays2402) wants to merge 1 commit into
microsoft:mainfrom
Sanjays2402:fix/instruction-order-dedup
Closed

Sanjay Santhanam (Sanjays2402) wants to merge 1 commit into
microsoft:mainfrom
Sanjays2402:fix/instruction-order-dedup

Conversation

@Sanjays2402

Copy link
Copy Markdown

Motivation & Context

prepend_instructions_to_messages gains a partial-dedup path from #5051: instructions already present as leading messages (same role and text) are skipped to avoid duplicate system messages when instructions are injected by multiple layers (e.g. Agent + chat client). But the remaining instructions were then prepended in front of the whole message list, so on a partial match they landed ahead of the skipped duplicates and the instruction order got inverted:

prepend_instructions_to_messages(
    [Message("system", ["First"])], ["First", "Second"])
# -> ["Second", "First"]  (wrong; should be ["First", "Second"])

Description & Review Guide

  • What are the major changes? In prepend_instructions_to_messages (python/packages/core/agent_framework/_types.py), the non-duplicated instruction messages are now inserted right after the leading matched messages instead of in front of the whole list. This preserves the caller-supplied instruction order no matter how much of it is already present as leading messages. Two regression tests added in python/packages/core/tests/core/test_types.py (test_prepend_instructions_partial_dedup_preserves_order, test_prepend_instructions_partial_dedup_multiline).
  • What is the impact of these changes? Only the partial-dedup ordering changes. Full dedup, no-match prepend, and custom-role behavior are unchanged (existing tests in the same region cover those).
  • What do you want reviewers to focus on? The insertion point choice: remainder goes after the leading positionally-matched prefix, which keeps the instructions contiguous and in order.

Related Issue

Fixes #8352

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change.

…ons_to_messages

The positional dedup from microsoft#5051 skipped instructions already present as
leading messages but then prepended the remaining ones in front of the
whole message list.  When only part of the instructions were already
present, the remaining instructions landed before the skipped ones,
silently reordering the system prompt:

    prepend_instructions_to_messages(
        [Message("system", ["First"])], ["First", "Second"])
    -> ["Second", "First"]   # wrong

Insert the remaining instruction messages after the leading matched
messages instead, so the original instruction order is preserved
regardless of how much of it is already present:

    -> ["First", "Second"]   # right

Fixes microsoft#8352.

Tests: added test_prepend_instructions_partial_dedup_preserves_order and
test_prepend_instructions_partial_dedup_multiline to
tests/core/test_types.py; all 310 tests in test_types.py and 32 in
test_clients.py pass, ruff check and ruff format clean.
Copilot AI balanced review requested due to automatic review settings September 13, 2026 23:28
@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Sep 13, 2026
@github-actions github-actions Bot changed the title Fix prepend_instructions_to_messages inverting instruction order on partial dedup Python: Fix prepend_instructions_to_messages inverting instruction order on partial dedup Sep 13, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A moderate deduplication-ordering issue remains, and a regression test is needed.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

This PR fixes instruction ordering during partial deduplication in the Python message helper.

Changes:

  • Inserts remaining instructions after the matched leading prefix.
  • Adds regression tests for partial-deduplication ordering.
File summaries
File Summary
python/packages/core/agent_framework/_types.py Updates instruction insertion logic; a moderate issue remains where later matching instructions may fall outside the contiguous block.
python/packages/core/tests/core/test_types.py Adds partial-deduplication ordering tests.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Lite (auto)

Note

Copilot is running an experiment and ran this review at Lite.


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 2001 to +2002
instruction_messages = [Message(role, [instr]) for instr in deduplicated]
return [*instruction_messages, *messages]
return [*messages[:insert_at], *instruction_messages, *messages[insert_at:]]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please have a look at this when you can thanks

@moonbox3

Copy link
Copy Markdown
Contributor

This branch was previously deployed

1 inactive deployment
github-app-auth — f301017c Deployed Sep 13, 2026 by Sanjays2402 via limit_open_prs #1291
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: prepend_instructions_to_messages inverts instruction order on partial dedup

3 participants