Repository navigation
Python: Implement annotation-based context compaction - #4469
Merged
Eduard van Valkenburg (eavanvalkenburg) merged 15 commits intoMar 11, 2026
Merged
Eduard van Valkenburg (eavanvalkenburg) merged 15 commits into
Eduard van Valkenburg (eavanvalkenburg) merged 15 commits into
Conversation
Contributor
Eduard van Valkenburg (eavanvalkenburg)
force-pushed
the
python_compaction
branch
2 times, most recently
from
March 5, 2026 12:24
2dbf300 to
d46b5fc
Compare
3 of 4 tasks
Eduard van Valkenburg (eavanvalkenburg)
force-pushed
the
python_compaction
branch
2 times, most recently
from
March 11, 2026 07:39
ebb3f99 to
fa5d060
Compare
Eduard van Valkenburg (eavanvalkenburg)
marked this pull request as ready for review
March 11, 2026 08:32
Contributor
There was a problem hiding this comment.
Pull request overview
Implements Python annotation-based context compaction primitives and strategies, wires in-run compaction through BaseChatClient, and adds tests + samples demonstrating usage.
Changes:
- Add new compaction core module with grouping annotations, token counting, built-in strategies, and a
CompactionProvider. - Integrate optional compaction into
BaseChatClient/Agentand update history provider to optionally skip excluded messages. - Add comprehensive compaction and function-loop tests plus new compaction samples and ADR rollout note.
Reviewed changes
Copilot reviewed 19 out of 20 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| python/uv.lock | Updates locked dependency versions. |
| python/samples/02-agents/compaction/README.md | Documents new compaction samples and how to run them. |
| python/samples/02-agents/compaction/basics.py | Demonstrates individual built-in compaction strategies. |
| python/samples/02-agents/compaction/advanced.py | Demonstrates composed compaction under a token budget. |
| python/samples/02-agents/compaction/custom.py | Demonstrates a custom strategy implementing the compaction protocol. |
| python/samples/02-agents/compaction/compaction_provider.py | Demonstrates provider-based compaction in an agent pipeline with history. |
| python/samples/02-agents/compaction/tiktoken_tokenizer.py | Demonstrates a TokenizerProtocol implementation using tiktoken. |
| python/packages/core/tests/openai/test_openai_responses_client.py | Adds regression test for parsing reasoning + function calls into one message. |
| python/packages/core/tests/core/test_types.py | Adds roundtrip serialization coverage for compaction annotations. |
| python/packages/core/tests/core/test_function_invocation_logic.py | Adds tests ensuring compaction is applied within function-call loops and under token budgets. |
| python/packages/core/tests/core/test_compaction.py | Adds extensive tests for grouping, strategies, apply/projection, and provider behavior. |
| python/packages/core/tests/core/test_clients.py | Adds tests ensuring BaseChatClient applies compaction before inner calls (streaming + non-streaming). |
| python/packages/core/agent_framework/openai/_responses_client.py | Adjusts OpenAI Responses request shaping for function calls. |
| python/packages/core/agent_framework/_types.py | Ensures additional_properties are normalized to plain dicts for annotation persistence. |
| python/packages/core/agent_framework/_sessions.py | Adds skip_excluded option to InMemoryHistoryProvider to omit excluded messages on load. |
| python/packages/core/agent_framework/_compaction.py | Adds the compaction implementation (annotations, strategies, provider, utilities). |
| python/packages/core/agent_framework/_clients.py | Wires optional compaction into BaseChatClient and agent factory. |
| python/packages/core/agent_framework/_agents.py | Plumbs compaction strategy + tokenizer through Agent and into the client. |
| python/packages/core/agent_framework/init.py | Re-exports compaction APIs from the top-level package. |
| docs/decisions/0019-python-context-compaction-strategy.md | Adds rollout/phase note for the implementation plan. |
Chris (crickman)
approved these changes
Mar 11, 2026
Eduard van Valkenburg (eavanvalkenburg)
enabled auto-merge
March 11, 2026 16:08
Dmytro Struk (dmytrostruk)
approved these changes
Mar 11, 2026
Eduard van Valkenburg (eavanvalkenburg)
removed this pull request from the merge queue due to a manual request
Mar 11, 2026
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Add ToolResultCompactionStrategy that collapses older tool-call groups into short summary messages (e.g. [Tool calls: get_weather]) while keeping the most recent groups verbatim. This mirrors the .NET ToolResultCompactionStrategy from PR microsoft#4533. Add CompactionProvider as a context-provider that auto-applies compaction before each agent turn and stores compacted history in session state after each turn. Includes tests and samples for both features. Co-authored-by: Copilot <[email protected]>
…p_excluded - ToolResultCompactionStrategy collapses older tool-call groups into [Tool results: func_name: result] summaries with bidirectional tracing (same pattern as SummarizationStrategy). - CompactionProvider as BaseContextProvider with separate before_strategy and after_strategy parameters. before_strategy compacts loaded context; after_strategy compacts stored history via history_source_id. - InMemoryHistoryProvider gains skip_excluded flag to filter out messages marked as excluded by compaction strategies. - Tests, samples, and exports updated. Co-authored-by: Copilot <[email protected]>
… annotations SummarizationStrategy was not calling annotate_message_groups after inserting its summary message, so the summary lacked core group annotations (id, kind, index, has_reasoning, _excluded). Added the missing call. ToolResultCompactionStrategy already had it. Added tests verifying both strategies produce fully annotated summaries. Co-authored-by: Copilot <[email protected]>
Eduard van Valkenburg (eavanvalkenburg)
force-pushed
the
python_compaction
branch
from
March 11, 2026 19:10
0fb9621 to
8a3ce72
Compare
Eduard van Valkenburg (eavanvalkenburg)
enabled auto-merge
March 11, 2026 19:11
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 28 out of 29 changed files in this pull request and generated 2 comments.
You can also share your feedback on Copilot code review. Take the survey.
Dmytro Struk (dmytrostruk)
approved these changes
Mar 11, 2026
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.
Summary
packages/core/agent_framework/_compaction.pyBaseChatClientsamples/02-agents/compaction