Skip to content

fix: merge multiple filter operators for same key#4559

Merged
kartik-mem0 merged 1 commit intomainfrom
fix/filter-merge-same-key-3952
Mar 26, 2026
Merged

fix: merge multiple filter operators for same key#4559
kartik-mem0 merged 1 commit intomainfrom
fix/filter-merge-same-key-3952

Conversation

@utkarsh240799
Copy link
Copy Markdown
Contributor

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 the gte condition and only apply lte.

Root cause: In _process_metadata_filtersprocess_condition, the loop over operators overwrites result[key] with a new dict on each iteration instead of merging into the existing one:

# Before (buggy): overwrites on each iteration
result[key] = {operator_map[operator]: value}

# After (fixed): merges into existing dict
result.setdefault(key, {})[operator_map[operator]] = value

The bug exists in both the sync Memory class and the async AsyncMemory class — 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

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (does not change functionality, e.g. code style improvements, linting)
  • Documentation update

How Has This Been Tested?

Added 3 new unit tests in tests/test_memory.py under TestProcessMetadataFiltersMerge:

  1. test_multiple_operators_same_key_merged — Core regression test: verifies {"created_at": {"gte": 1000, "lte": 2000}} produces a filter with both operators preserved.
  2. test_single_operator_still_works — Ensures single-operator filters (the common case) are unaffected by the change.
  3. test_multiple_keys_with_multiple_operators — Verifies multiple keys each with multiple operators all merge correctly.

All 31 tests in test_memory.py pass:

$ python -m pytest tests/test_memory.py -v
============================== 31 passed in 0.49s ===============================
  • Unit Test
  • Test Script (please provide)

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Maintainer Checklist

🤖 Generated with Claude Code

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 kartik-mem0 merged commit 3ac4e04 into main Mar 26, 2026
8 checks passed
@kartik-mem0 kartik-mem0 deleted the fix/filter-merge-same-key-3952 branch March 26, 2026 14:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

merge filters for same key

2 participants