Python: preserve instruction order in prepend_instructions_to_messages dedup by ManoharPaturi · Pull Request #8353 · microsoft/agent-framework · GitHub
Skip to content

Python: preserve instruction order in prepend_instructions_to_messages dedup - #8353

Merged
Evan Mattson (moonbox3) merged 3 commits into
microsoft:mainfrom
ManoharPaturi:fix/prepend-instructions-order
Sep 15, 2026
Merged

Evan Mattson (moonbox3) merged 3 commits into
microsoft:mainfrom
ManoharPaturi:fix/prepend-instructions-order

Conversation

@ManoharPaturi

@ManoharPaturi Manohar Paturi (ManoharPaturi) commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Motivation & Context

prepend_instructions_to_messages() inverts the order of the instruction list when only part of it is already present as leading messages. The dedup added in #5051 prepends the non-matching remainder in front of the matched instructions, so a chat client using ["First", "Second"] instructions with a leading system "First" message ends up with the system prompt silently reordered to Second, First. Reported in #8352.

Description & Review Guide

  • What are the major changes? The dedup now skips only the matching prefix and inserts the remaining instructions directly after it, preserving the configured instruction order. Previously covered cases (full dedup, no dedup) produce byte-identical output.
  • What is the impact of these changes? Instruction order is preserved regardless of how much of it is already present; no behavior change for existing green cases.
  • What do you want reviewers to focus on? The prefix-skip logic in _types.py and the new ordering test.

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
  • This is not a breaking change.

…pend_instructions_to_messages

When only a prefix of the instruction list is already present as leading
messages, the previous dedup logic collected the non-matching remainder
and prepended it in front of the matched messages. For instructions
["First", "Second"] with an existing leading system message "First",
the result became ["Second", "First", ...] - inverting the relative
order of the instructions.

Deduplicate only the matching prefix and insert the remaining
instructions immediately after it, so the instruction order is
preserved. Behavior for the previously tested cases (no match, full
match, single instruction) is unchanged.

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@moonbox3

Copy link
Copy Markdown
Contributor

@ManoharPaturi

Copy link
Copy Markdown
Contributor Author

Updated the PR description to the standardized template. Thanks!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good—thanks!

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 14, 2026
@ManoharPaturi

Copy link
Copy Markdown
Contributor Author

Merged via the queue into microsoft:main with commit 624f0cc Sep 15, 2026
40 checks passed

This branch was previously deployed

1 inactive deployment
github-app-auth — 8586d4b7 Deployed Sep 15, 2026 by moonbox3 via add_label #22814
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

4 participants