fix: resolve schema mismatch between hot and cold memory paths#145
Open
yaowubarbara wants to merge 1 commit intolangchain-ai:mainfrom
Open
fix: resolve schema mismatch between hot and cold memory paths#145yaowubarbara wants to merge 1 commit intolangchain-ai:mainfrom
yaowubarbara wants to merge 1 commit intolangchain-ai:mainfrom
Conversation
The manage_memory_tool (hot path) writes memories as {"content": <value>}
while MemoryStoreManager (cold path) writes as {"kind": ..., "content": {...}}.
When both paths coexist, the cold path crashes with KeyError when reading
hot path data, and the search tool presents nested/inconsistent formats.
Changes:
- Add _parse_store_value() in extraction.py to handle both formats when
reading from the store, preventing KeyError on missing "kind" key
- Add _normalize_memory_value() in tools.py to unwrap the kind/content
envelope in search results for consistent LLM presentation
- Add 13 unit tests covering both helpers with various edge cases
Fixes langchain-ai#138, fixes langchain-ai#140
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
MemoryStoreManagercrashes withKeyError: 'kind'when reading memories written bycreate_manage_memory_toolcreate_search_memory_toolreturns nested/inconsistent format for memories written byMemoryStoreManagerProblem
The two memory write paths use incompatible value schemas:
create_manage_memory_tool(hot){"content": <str_or_dict>}MemoryStoreManager(cold){"kind": "Memory", "content": {...}}When both paths coexist (which is the recommended pattern per docs), the cold path crashes on
item.value["kind"]because the hot path never writes akindkey.Solution
Backward-compatible read-side fix — no changes to write formats, no breaking changes.
_parse_store_value()inextraction.py: Normalizes both formats into a(kind, content)tuple. Ifkindis missing, defaults to"Memory"and wraps bare strings into the expected dict format._normalize_memory_value()intools.py: Unwraps thekind/contentenvelope in search results so the LLM always sees a consistent, flat value regardless of which code path wrote the memory.Both helpers are used in all 4 affected code locations (2 in
ainvoke, 2 ininvoke).Tests
Added 13 unit tests in
tests/test_schema_compat.pycovering: