Python: Preserve messages when replacing chat history from itself - #14465
Open
黄耀鑫(Huang Yaoxin) (YaoxinHuang) wants to merge 2 commits into
Open
黄耀鑫(Huang Yaoxin) (YaoxinHuang) wants to merge 2 commits into
黄耀鑫(Huang Yaoxin) (YaoxinHuang) wants to merge 2 commits into
Conversation
黄耀鑫(Huang Yaoxin) (YaoxinHuang)
requested a review
from a team
as a code owner
September 20, 2026 15:15
黄耀鑫(Huang Yaoxin) (YaoxinHuang)
had a problem deploying
to
github-app-auth
September 20, 2026 15:16 — with
GitHub Actions
Failure
黄耀鑫(Huang Yaoxin) (YaoxinHuang)
had a problem deploying
to
github-app-auth
September 20, 2026 15:16 — with
GitHub Actions
Failure
Copilot started reviewing on behalf of
黄耀鑫(Huang Yaoxin) (YaoxinHuang)
September 20, 2026 15:16
View session
Contributor
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
Reviewed changes address the reported replacement behavior with regression coverage.
Review effort: Lite
Findings: None
What changed in this PR
Updates ChatHistory.replace to preserve messages when replacing history from itself or related iterables.
Changes:
- Materializes replacement messages before clearing history.
- Adds regression tests for histories, lists, iterators, and filtered generators.
| File | Description |
|---|---|
python/tests/unit/contents/test_chat_history.py |
Adds regression coverage for self-referential replacements. |
python/semantic_kernel/contents/chat_history.py |
Safely materializes replacement messages while preserving list identity. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation and Context
ChatHistory.replaceclears its messages before consuming the replacement iterable. Passing the history itself, its messages list, or a generator filtering the history therefore silently empties it. For example, removing assistant messages withhistory.replace(m for m in history if m.role != AuthorRole.ASSISTANT)also loses the system and user messages.Description
Materialize the iterable before clearing the history, while preserving the existing messages list. Add regression coverage for the history, its backing list, an iterator, and a filtered generator.
Validated the contents unit suite, repository pre-commit hooks for both changed files, scoped mypy, and a wheel build. The full optional-provider test matrix was not run.
Contribution Checklist