Skip to content

fix: resolve schema mismatch between hot and cold memory paths#145

Open
yaowubarbara wants to merge 1 commit intolangchain-ai:mainfrom
yaowubarbara:fix/schema-mismatch-138-140
Open

fix: resolve schema mismatch between hot and cold memory paths#145
yaowubarbara wants to merge 1 commit intolangchain-ai:mainfrom
yaowubarbara:fix/schema-mismatch-138-140

Conversation

@yaowubarbara
Copy link

Summary

Problem

The two memory write paths use incompatible value schemas:

Path Writes
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 a kind key.

Solution

Backward-compatible read-side fix — no changes to write formats, no breaking changes.

  • _parse_store_value() in extraction.py: Normalizes both formats into a (kind, content) tuple. If kind is missing, defaults to "Memory" and wraps bare strings into the expected dict format.
  • _normalize_memory_value() in tools.py: Unwraps the kind/content envelope 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 in invoke).

Tests

Added 13 unit tests in tests/test_schema_compat.py covering:

  • Cold path format (with and without custom schemas)
  • Hot path format (string content, dict content, numeric edge case)
  • Edge cases (empty dict, unknown keys, partial envelope)
  • Normalization passthrough for non-dict values

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant