fix: merge multiple filter operators for same key#4559
Merged
kartik-mem0 merged 1 commit intomainfrom Mar 26, 2026
Merged
Conversation
When a metadata filter has multiple operators on the same key (e.g., created_at with both gte and lte), only the last operator was preserved because the loop overwrote result[key] on each iteration. Use setdefault to merge operators into the existing dict instead. Fixes both sync (Memory) and async (AsyncMemory) implementations. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
kartik-mem0
approved these changes
Mar 26, 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.
Description
Fixes a bug where metadata filters with multiple operators on the same key silently drop all but the last operator. For example, a date range filter like
{"created_at": {"gte": 1000, "lte": 2000}}would lose thegtecondition and only applylte.Root cause: In
_process_metadata_filters→process_condition, the loop over operators overwritesresult[key]with a new dict on each iteration instead of merging into the existing one:The bug exists in both the sync
Memoryclass and the asyncAsyncMemoryclass — this PR fixes both.Note: PR #3953 attempted this fix but was closed due to unsigned CLA and only addressed one of the two locations.
Fixes #3952
Type of change
How Has This Been Tested?
Added 3 new unit tests in
tests/test_memory.pyunderTestProcessMetadataFiltersMerge:test_multiple_operators_same_key_merged— Core regression test: verifies{"created_at": {"gte": 1000, "lte": 2000}}produces a filter with both operators preserved.test_single_operator_still_works— Ensures single-operator filters (the common case) are unaffected by the change.test_multiple_keys_with_multiple_operators— Verifies multiple keys each with multiple operators all merge correctly.All 31 tests in
test_memory.pypass:Checklist:
Maintainer Checklist
🤖 Generated with Claude Code