-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: add default for memory extraction trigger #2811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
opieter-aws
merged 1 commit into
strands-agents:main
from
opieter-aws:opieter-aws/python-extraction-config-divergence
Jun 16, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
106 changes: 106 additions & 0 deletions
106
strands-py/src/strands/memory/extraction/resolve_extraction_config.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| """Resolves a store's ``extraction`` setting into a concrete config. | ||
|
|
||
| The single place the ``bool | ExtractionConfig`` shorthand is interpreted and | ||
| per-store defaults are applied, so the :class:`~strands.memory.memory_manager.MemoryManager` | ||
| and :class:`~strands.memory.extraction.coordinator.ExtractionCoordinator` never | ||
| re-apply defaults or normalize shapes themselves. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
|
|
||
| from ..types import MemoryStore, _has_method | ||
| from .model_extractor import ModelExtractor | ||
| from .triggers import IntervalTrigger | ||
| from .types import ( | ||
| DEFAULT_MEMORY_MESSAGE_FILTER, | ||
| ExtractionConfig, | ||
| ExtractionTrigger, | ||
| Extractor, | ||
| MemoryMessageFilter, | ||
| ) | ||
|
|
||
| # Default cadence when an ``ExtractionConfig`` omits its ``trigger``: extract every N turns. | ||
| DEFAULT_EXTRACTION_TRIGGER_TURNS = 5 | ||
|
|
||
|
|
||
| @dataclass | ||
| class _ResolvedExtractionConfig: | ||
| """An :class:`ExtractionConfig` with every field resolved to a concrete value. | ||
|
|
||
| Produced by :func:`_resolve_extraction_config` so the ``MemoryManager`` and | ||
| ``ExtractionCoordinator`` never have to re-apply defaults or normalize shapes. | ||
|
|
||
| Attributes: | ||
| triggers: Normalized to a list (a single trigger is wrapped). Never empty | ||
| for a resolved config (an explicit empty list is left empty for the | ||
| manager to reject). | ||
| extractor: The extractor that distills facts client-side and stores them | ||
| via the store's ``add`` method, or ``None`` to use the store's | ||
| ``add_messages`` method (server-side extraction). | ||
| filter: The content-block filter applied before extraction. | ||
| """ | ||
|
|
||
| triggers: list[ExtractionTrigger] | ||
| extractor: Extractor | None | ||
| filter: MemoryMessageFilter | ||
|
|
||
|
|
||
| def _resolve_extraction_config( | ||
| extraction: bool | ExtractionConfig | None, | ||
| store: MemoryStore, | ||
| ) -> _ResolvedExtractionConfig | None: | ||
| """Resolve a store's ``extraction`` setting into a :class:`_ResolvedExtractionConfig`. | ||
|
|
||
| The single place the ``bool | ExtractionConfig`` shorthand is interpreted: | ||
| ``False``/``None`` is off (returns ``None``), ``True`` enables all defaults, an | ||
| :class:`ExtractionConfig` defaults its unset fields. The defaults are: | ||
|
|
||
| - **triggers**: every :data:`DEFAULT_EXTRACTION_TRIGGER_TURNS` turns. An | ||
| explicit empty list is left empty for the ``MemoryManager`` to reject. | ||
| - **extractor**: chosen from the methods the store implements. A store that | ||
| implements only ``add`` cannot extract server-side, so it defaults to a | ||
| :class:`~strands.memory.extraction.model_extractor.ModelExtractor` that | ||
| distills facts client-side (via model calls) and stores each one through | ||
| ``add``. A store that implements ``add_messages`` supports server-side | ||
| extraction, so it defaults to no extractor: the manager hands raw messages | ||
| to ``add_messages`` and the backend extracts them itself, with no model call. | ||
| - **filter**: :data:`DEFAULT_MEMORY_MESSAGE_FILTER`. | ||
|
|
||
| Args: | ||
| extraction: The store's ``extraction`` setting. | ||
| store: The store, inspected for the write methods it implements to pick the | ||
| default extractor. | ||
|
|
||
| Returns: | ||
| The resolved config, or ``None`` when extraction is disabled. | ||
| """ | ||
| if not extraction: | ||
| return None | ||
| config = ExtractionConfig() if extraction is True else extraction | ||
|
|
||
| triggers: list[ExtractionTrigger] | ||
| if config.trigger is None: | ||
| triggers = [IntervalTrigger(turns=DEFAULT_EXTRACTION_TRIGGER_TURNS)] | ||
| elif isinstance(config.trigger, list): | ||
| triggers = config.trigger | ||
| else: | ||
| triggers = [config.trigger] | ||
|
|
||
| extractor = config.extractor | ||
| if extractor is None: | ||
| # Pick the default extractor from the store's write methods: | ||
| # - implements only ``add``: it cannot extract server-side, so default to a | ||
| # ModelExtractor that distills facts client-side and stores each via ``add``. | ||
| # - implements ``add_messages`` (whether or not it also implements ``add``): extract | ||
| # server-side. Leave the extractor None so raw messages go straight to | ||
| # ``add_messages`` with no model call. | ||
| if _has_method(store, "add") and not _has_method(store, "add_messages"): | ||
| extractor = ModelExtractor() | ||
|
|
||
| return _ResolvedExtractionConfig( | ||
| triggers=triggers, | ||
| extractor=extractor, | ||
| filter=config.filter or DEFAULT_MEMORY_MESSAGE_FILTER, | ||
| ) | ||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.