Repository navigation
feat(dspy): support portable and provider-native compaction - #10240
Draft
isaacbmiller wants to merge 3 commits into
Draft
isaacbmiller wants to merge 3 commits into
isaacbmiller wants to merge 3 commits into
Conversation
Amp-Thread-ID: https://ampcode.com/threads/T-01a00b6b-f68a-763c-aac4-e5dbeea05f0f Co-authored-by: Amp <amp@ampcode.com>
Unify summary-based History compaction with replayable provider-native compaction parts, including safe history inspection and provider checks. Co-authored-by: Amp <amp@ampcode.com> Amp-Thread-ID: https://ampcode.com/threads/T-01a00b6b-f68a-763c-aac4-e5dbeea05f0f
Allow History.compaction to hold either a portable text summary or a provider-native LMCompactionPart and replay each through the correct LM path. Co-authored-by: Amp <amp@ampcode.com> Amp-Thread-ID: https://ampcode.com/threads/T-01a00b6b-f68a-763c-aac4-e5dbeea05f0f
This branch has not been deployed
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.
📝 Changes Description
This PR gives DSPy one compacted-history shape:
compactionis top-level state, soHistory's normal signature-field filtering cannot discard it. Its two values preserve the distinction between portable summaries and provider-owned continuation state:Portable text compaction
Adapters render a string before retained turns as ordinary user context:
This follows the common non-native scheme of replacing an old prefix with a user summary and retaining a recent suffix. It can produce adjacent user messages when the retained suffix begins with a user turn; providers and frameworks normally merge or accept that shape.
Provider-native compaction
Provider compaction is returned as
LMCompactionParton typed LM responses and can be placed directly intoHistory:The adapter inserts the typed part at the compacted-history boundary. The LM client then restores the provider's actual wire representation:
The same OpenAI-shaped
context_management=[...]setting works for both providers: LiteLLM maps it to Anthropic's nativecompact_20260112edit.Additional behavior:
LMResponses,History, andLMHistoryEntry.messagespreserve provider compaction losslessly.inspect_history()displays readable summaries and labels opaque state without printing encrypted content.History(compaction=None, messages=...)keeps the previous serialized shape._compact_if_neededpolicy.Why one union field?
Both values occupy the same semantic slot: a replacement for the omitted history prefix. A union makes it impossible to set a portable summary and native state simultaneously.
They intentionally render differently. A string is model-readable, provider-independent application context. An
LMCompactionPartis provider-owned state and may be opaque. Converting a string intoLMCompactionPartwould invent a provider identity; flattening native state into a string would lose or leak OpenAI's encrypted payload.Ownership remains separated:
Historystores the compacted prefix and retained signature-shaped suffix.References
HumanMessage("Here is a summary...")followed by preserved recent messages.CompactionPart: precedent for retaining provider-specific compaction as a typed history part rather than flattening it into ordinary text.✅ Contributor Checklist
{label}(dspy): {message}🧪 Validation
openai/gpt-5.3-codex: the first call emitted opaque native compaction, the latestLMCompactionPartwas assigned toHistory.compaction, and a subsequentPredictcall recovered the compacted codewordCOBALT. The encrypted payload's SHA-256 matched between the first response and second provider request, proving lossless replay.uv run --frozen pytest --deno tests/adapters -q— 280 passeduv run --frozen pytest --deno tests/core/test_types.py tests/clients/test_inspect_global_history.py tests/clients/test_disk_serialization.py -q— 62 passeduv run --frozen pytest --deno tests/clients/test_lm.py -q— 83 passed, 8 skippedgit diff --checkThe typed direct-call API used to obtain and continue an
LMResponseis experimental. Provider compaction is enabled through the upstreamcontext_managementAPI.